What is happening between static and protected modifiers of int b?
- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
The reason behind the restriction is this: Each subclass inherits the contract of the superclass and expands that contract in someway. 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 class then it could manipulate them in a way that would break the first class�s contract�and this should not be permissible.
Protected static members can be accessed in any extended class...This is allowed because a subclass can�t modify the contract of its static members as it can only hide them, not override them�hence there is no danger of another class violating that contract.
SCJP2. Please Indent your code using UBB Code
Download a copy of <a href="http://www.javarules.com" target="_blank" rel="nofollow"><i>"Mastering The Fundamentals of The Java Programming Language"</i></a>
Why should the Test subclass be allowed to access the protected static field of the Derived class?
SCJP2. Please Indent your code using UBB Code
Consider Paul's rocket mass heater. |