Protected member access from different packages in java – a curiosity [duplicate]

protected allows access from subclasses and from other classes in the same package. That’s why any Derived class instance can access the protected method in Base.

The other line creates a Base instance (not a Derived instance!!). And access to protected methods of that instance is only allowed from objects of the same package.


display();

-> allowed, because the caller, an instance of Derived has access to protected members and fields of its subclasses, even if they’re in different packages

new Derived().display();

-> allowed, because you call the method on an instance of Derived and that instance has access to the protected methods of its subclasses

new Base().display();

-> not allowed because the caller’s (the this instance) class is not defined in the same package like the Base class, so this can’t access the protected method. And it doesn’t matter – as we see – that the current subclasses a class from that package. That backdoor is closed 😉

Leave a Comment