I am referring Khalid Mughal for my
SCJP preparation. On page no. 117 (chapter 4. Declaration and Access control) i came across the explaination of protected Members where it says....
A subclass in another package can only access protected members in the superclass
via references of its own type or a subtype. There is a example given for protected modifier...
package A; public class SuperclassA
{
protected int superclassVarA;
protected void superclassMethodA()
{/*..........*/}
}
package B; public class SubclassB extends SuperclassA
{
SuperclassA objRefA = new SubclassB();
SubclassB objRefB = new SubclassB();
void subclassMethodB()
{
objRefA.superclassMethodA();
//OK objRefB.superclassVarA;
//NOT OK //My doubt is WHY objRefA.superclassVarA is NOT OK }
}
Further explaination is given that, "Access to protected members of the superclass would also be permitted via any references of subclasses of SubclassB. The above restriction helps to ensure that subclasses in packages different from their superclass can only access protected members of the superclass in their part of inheritance hierarchy." Why i can't access the protected member of superclass by using reference variable of type superclass, since it belongs to the same inheritance hierarchy Can someone help me out on this topic....
TIA
Manish Sachdev
[ August 30, 2003: Message edited by: Manish Sachdev ]
[ September 01, 2003: Message edited by: Manish Sachdev ]