hi! i have having problem in understanding protected accesibitlity SuperclassA contains a protected member varA in the default package(ie package name given) the subclassB is in another package and extends SuperclassA still it can't access varA using a reference of SuperclassA.
why is varA accesible in SubclassB through obj3 and not through obj1 and obj2. the compiler gives error tht varA has protected access in SuperclassA. madhur.
This is a perfect example to understand the protected access. I guess varA is declared with protected in base classs. The compile type of the variable used in the access expression for the protected variable must be the same as the type of class in which the access expression for the protected member occurs. The same or one of its subclasses. Let's see the examples: The type of the class in which the access expressions for the protected varA occurs is SuclassB. i=obj3.varA; //OK The compile type of obj3 is SubclassB, thus OK i=obj1.varA; ERROR Because the compile type of obj1 is SubclassA i=obj2.varA; ERROR Because the compile type of obj2 is SubclassA In the JLS 6.6.2 section you can read a more formal explanation. [ September 22, 2002: Message edited by: Jose Botella ]
SCJP2. Please Indent your code using UBB Code
posted 17 years ago
Hello Madhur: I will try to explain it in different way. Though Jose's explaination is perfect. When you are saying in subclassB: int i = varA; You are saying that assign the value of varA to i. You are mentioning varA as if it is another instance variable in subclassB. When compiler does not find it in subclassB, it tries to look for it higher up in hierarchy. It finds it in superclassA and it is protected. So it is available in subclassB. And value get assigned. However, think for a second that hierarch is of 5 levels and protected i is reassigned in each level from 1 to 5 so that lowest in hierarchy has i defined as 5. Now if your subclassB was sub class of this lowest level class, the i in subclassB will get 5 automatically by saying: int i = varA; // i get 5 because that is immediately avaiable to subclassB. Now let us say that in subclassB you want i to get value from second top class in hierarchy. So you are creating an object of that class (call it class2) by saying: class2 obj2 = new class2(); to assign the value to i, you are saying: i = obj2.i; remember that i is defined in subclassB so it class level is subclassB. While obj2.i is member of class2, so its class level is class2. You can not take a member of higher class level and put it's value in lower class level member. So in nutshell, the protected members are available in a derived class from immediate super class. You can not get a value of specific super class. Hope this helps. Barkat
posted 17 years ago
Also: int i = varA; Within an instance method, constructor, variable or block instance initializer is the same as int i = this.varA; The compile time of this is the inmediate enclosing class, SubclassB, thus it's OK [ September 23, 2002: Message edited by: Jose Botella ]