Protected behavior of an element after its inheritance.

I have some doubts about the secure identifier. In the first chapter of K.Sierra's Sun Certified Java Programmer's Guide, I found the following information:

"When a subclass-outer-package inherits a protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass, except for subclasses of the subclass."

I presented a sample code that reflects the above statement, and this is absolutely clear to me.

// Parent class package package1; import package2.Child; public class Parent { protected int i = 5; } // Child class package package2; import package1.Parent; public class Child extends Parent { // variable 'i' inherited } package package2; public class Neighbour { public void protectedTesting(){ Child child = new Child(); System.out.println(child.i); // no access } } 

I started experimenting and made a small change - moved Neighbor to package1. And there is access to the variable "i", which for me is a bit surprising, since it does not correspond to the statement "becomes private for any code outside the subclass"

Neighboring class after change:

 package package1; import package2.Child; public class Neighbour { public void protectedTesting(){ Child child = new Child(); System.out.println(child.i); // access! } } 

Please explain this to me. Thanks.

+5
java inheritance protected scjp
source share
4 answers

In short, protected is a closed package, and also visible to subclasses. Even JLS is vague in this matter ( JLS Β§6.6.2 ):

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

Indicates that outside the package, only subclasses can access protected members. This means that you can also access the variable inside the package. This is poorly worded, but nonetheless, true protected members have package-level visibility as well as subclass-level visibility.

See also:

  • This related question
  • Java Trail for Access Control
+5
source share

And there is access to the variable "i", which for me is a bit surprising, since it does not correspond to the statement "becomes private for any code outside the subclass"

-> But you moved the Neighbour class to package1 , which is true according to "Protected members can be accessed by classes in same package"

"When a subclass-outer-package inherits a protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass, except for subclasses of the subclass."

-> The inner package is still protected and is not private to all classes within the package.

+3
source share

The truth is not in the Sun Certified Java Programmer’s Study Guide, but in the Java Language Specification

6.6.2. Secure Access Information

Access to the protected member or constructor of an object can be obtained from outside the package, in which it is declared only by the code that is responsible for the implementation of this object.

+1
source share

protected visibility enables packet-level visibility. Inheritance lets you treat your Child object as an instance of Parent . Since i of Parent declared in the same package, it is available from Neighbour .

 package package1; import package2.Child; public class Neighbour { public void protectedTesting() { Parent neighboured = new Child(); System.out.println(neighboured.i); // access } } 
+1
source share

All Articles