5

I have a problem understanding protected members inheritance and visibility.

I know it is visible in the same package and subclasses.

But in the following code it is not visible in a subclass.

A.java

package a; public class A { public static void main(String[] args) { } protected void run() { } } 

B.java

package b; import a.A; public class B extends A { public static void main(String[] args) { B b = new B(); b.run(); // this works fine } } 

C.java

package b; import a.A; public class C extends A{ // it will not work also if extends B public static void main(String[] args) { B b = new B(); b.run(); // this is the problem; not visible } } 

Why b.run() in the last class is invisible?

1
  • What is the access modifier of run() ? Commented Aug 18, 2015 at 15:20

1 Answer 1

7

This is because class C can see A's protected methods from within its own inheritance tree. But it's not allowed to access A's protected methods for another class (B) from a different inheritance tree. C is not part of B's inheritance tree (by this, I mean that's it's not a parent of B), therefore the behavior is normal.

EDIT: Added documentation reference as requested

6.6.2.1. Access to a protected Member:

If the access is by a field access expression E.Id, or a method invocation expression E.Id(...), or a method reference expression E :: Id, where E is a Primary expression (§15.8), then the access is permitted if and only if the type of E is S or a subclass of S.

Applying the above to this case, because the variable b is not an instance of C or a subclass of C, access to the protected method b.run() is not allowed.

Also addressing Codebender's comment about the packages. Note that, if the C class would have been defined in the same package as the A class, where the protected run() method is defined, then the above rule wouldn't apply, and you would be able to access the method as shown in your code.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you put an official doc link explaining this? Since B and C are in the same package, and protected fields can be accessed in the same package, this seems a bit weird.
What about C extends B. It is the same problem.
Added more specific details from the documentation. Should clarify things for you hopefully.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.