Protected access modifier

89 views Asked by At

JLS 6.6.2 gives us the following restriction for the package-access of protected members.

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

What did they mean responsible for implementation. Couldn't you get an example?

1

There are 1 answers

6
Eran On

It means that you can't access a protected super-class member of a different instance of the same class.

package one;
public class A {protected int b;}

package two;
public class B extends A {

    public void someMethod (A other)
    {
        b = 5; // allowed
        other.b = 5; // not allowed
    }
}