I am having a little trouble understanding the protected access modifier in java (or the design behind it). I thought it meant package access and access through objects that inherit the class containing an abstract member.
I wrote the following sample code. I see that the commented out line produces a compilation error if uncommented. Why can I access pro through a Second object in Second but not through a First object in Second?
package first;
public class First {
protected void pro(){
System.out.println("Can see protected method");
}
}
package first;
public class InFirst {
public static void main(String[] args){
First fst = new First();
fst.pro();
}
}
package second;
import first.First;
public class Second extends First {
public static void main(String[] args){
First fst = new First();
// fst.pro();
Second sec = new Second();
sec.pro();
}
}
Best Answer
The webpage @MadProgrammer linked gives a decent explanation:
This means the protected member must be accessed directly through either the class it is defined in or a subclass of said class while also being within the appropriate package. It does not necessarily mean you can access the protected member through an instance of said class created within a subclass of said class. The focus is on the packages involved.
Here are your examples:
Attempting to access member in question from which package? first
Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes,
First
is defined inpackage first
, so theprotected
member is accessible fromFirst
inpackage first
.Attempting to access member in question from which package? second
Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? No,
First
is defined inpackage first
, soprotected
makes the member inaccessible fromFirst
inpackage second
.Attempting to access member in question from which package? second
Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes,
Second
, which is defined inpackage second
, inherits the member fromFirst
, so theprotected
member is accessible fromSecond
inpackage second
.More examples for clarity:
Attempting to access member in question from which package? first
Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes,
Second
inherits the member fromFirst
, which is defined inpackage first
, so theprotected
member is accessible fromSecond
inpackage first
.Attempting to access member in question from which package? first
Is the (sub)class, which contains said member, or its parent class, which it inherits the member from, defined within that same package? Yes,
Third
inherits the member fromSecond
, which inherits it fromFirst
where the member is defined (package first
), so theprotected
member is accessible fromThird
inpackage first
.