What is the difference between public, protected, package-private and private in Java?
The question's answer cannot answer my question. It's not a duplicate question. I totally know the diagram of access modifier. I've cited the java doc: The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
However, it cannot answer my following question:
I have a Java project with the following file structure:
src
└── A
├── P1.java
└── P2.java
P1.java
is inside package A
, while P2.java
is at the same level as A
.
Here is the code for P1.java
:
package A;
public class P1 {
public int a1;
protected int a2;
}
And here is the code for P2.java
:
import A.P1;
public class P2 extends P1 {
public static void main(String[] args) {
P1 p = new P1();
System.out.println(p.a1);
System.out.println(p.a2); // error?
}
}
When I run this, I get the following error:
java: a2 has protected access in A.P1
However, I recall that a protected variable should be accessible in a subclass, even if they are not in the same package.
Why is this happening? Since P2
is a subclass of P1
, it should be able to access a2
.
This is happening because you are accessing the protected member using the object of the superclass, which is not possible. The accessibility of protected member is only possible with the object of the subclass itself.
Try:
import A.P1;
public class P2 extends P1 {
public static void main(String[] args) {
P2 p = new P2();
System.out.println(p.a1);
System.out.println(p.a2);
}
}