Why can we not define a class as protected?
I know that we can't, but why? There should be some specific reason.
Why can we not define a class as protected?
I know that we can't, but why? There should be some specific reason.
Defining a field protected makes that field accessible inside the package as well as outside the package through inheritance only (Only inside the child class).
So If we are allowed to make a class protected then we can access it inside the package very easily but for accessing that class outside of the package we first need to extend that entity in which this class is defined which is its package.
And since a package can not be extended (can be imported), defining a class protected will again make it package-private which is similar to defining it as default which we can already do. Therefore there is no benefit of defining a class private it will only make things ambiguous.
For more information read Why an outer Java class can’t be private or protected
if a outer class is declared by protected, I think you want the class can be only accessed from same package and its subclass but different packages. However, there is no possible to create subclasses for a protected class, because when you write "class Dog extends Animal", because of protected "Animal" only can be accessed by its subclass, obviously, "Dog" is not "Animal" subclass.
So protected outer class is same with (default) outer class!
What makes sense to this question is that, JVM is written in C (Sun JVM) and C++(oracle JVM) so during compilation, we are going to create .class files out of our java file and if we declare a class with Protected keyword then it will not be accessed by JVM.
The answer why protected class will not be accessed by JVM is that, since protected fields are accessible within same package or to diffrent package through inheritance only and JVM is not written in a way so that it will inherit will class. Hope this satisfies this question :)
Similarly, A top level class can't be private. Explanation as below:
So what will happen if we will define a class private, that class will only be accessible within the entity in which it is defined which in our case is its package?
So defining private access to the class will make it accessible inside the same package which default keyword already do for us, Therefore there is no benefit of defining a class private it will only make things ambiguous.
protected means that the member can be accessed by any class in the same package and by sub classes even if they are in another packages.
Example:
package a;
class parent{
protected void p();
}
package b;
import a.p;
class child extends parent{
//you can access method which is protected in the parent in the child
}
class another extends child {
//here you can not access the protected method
}
The protected modifier is allowed on an inner class. But still an instance of this inner class cannot be constructed from within a class extending the outer class. Only when the constructing code is within the same package it is allowed by the compiler. But what difference does the protected modifier then make with respect to the default accessibility? So, from my point of view, the protected modifier is not at all allowed on top-level classes, and it does not make sense on embedded classes.