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);
Please explain this to me. Thanks.
java inheritance protected scjp
MrKiller21
source share