...What I tought is , Sub can not see the protectedVar because protectedVar behaves like a privte varible in class Tester(even though they are in the same package) , so cmpiler should give the error when Sub trying to access it
One thing for sure is that once Tester inherited protectedVar, its SubClass Sub can no longer access protectedVar in its own class. But why Sub can access it in Tester class is really a puzzle.
One thing to ask boyet.
To my understanding of your post, you said the Sub class can access the protected protectedVar, is that true? If it is true, could you please tell me why the Jcompiler doesn't agree? If it is not true, could you explain to me why Sub.protectedVar is a valid statement in the original code.
Does anybody try to run the codes and then make some comments?Please don't just make quote of some book! Use JCompiler to discuss it.
�The reason behind the restriction is this: Each subclass inherits the contract of the superclass and expands that contract in some way. Suppose that one subclass, as part of its expanded contract, places constraints on the values of protected members of the superclass. If a different subclass could access the protected members of objects of the first subclass then it could manipulate them in a way that would break the first subclass�s contract -- and this should not be permissible.�
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton