Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this protected member is not visible in the subclass [duplicate]

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?

like image 367
Abu Muhammad Avatar asked Aug 18 '15 15:08

Abu Muhammad


People also ask

How to access the protected members of a class in another class?

We can access protected members of a class in its subclass if both are present in the same package. System.out.println ("Its "+year+" !!"); Case 4: Accessing protected members in another class in a different package We cannot access the protected members of a class in a class (non-subclass) that is present in a different package.

Why are protected methods not visible to other classes in Java?

Because protected is visible to the class itself (like private) and its subclasses. It is not public. protected instance methods are visible to the class itself, and to instances of subclasses, but not to static methods in subclasses.

What does the protected modifier in a class mean?

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. This is the reason why you can't directly call the method inside the main method on the vehicle object.

What are public private and protected sections in C++?

A class in C++ has public, private and protected sections which contain the corresponding class members. Protected members in a class are similar to private members as they cannot be accessed from outside the class.


1 Answers

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.

like image 160
sstan Avatar answered Nov 15 '22 03:11

sstan