Hello again :o)
Here is a final explanation for this problem:
Consider the following example:
super class:
package a;
public class First {
protected String protectedProperty = null;
protected void protectedFunction() {
System.out.println("From protected function.");
}
public First() {
protectedProperty = new String("It is working.");
}
}
subclass:
package b;
import a.First;
public class Second extends First {
public static void main(String args[]) {
First f = new First();
Second s = new Second();
// f.protectedFunction(); - compilation error
s.protectedFunction();
// System.out.println(f.protectedProperty); - compilation error
System.out.println(s.protectedProperty);
}
}
The commented lines in the class definition of Second are compilation errors because You are in package b. As You know, there are at least four access modifiers in Java: public, private, protected and 'package access'. In package b You can access only public fields of classes belonging to package a. However You can access protected fields also, but only if You inherit a subclass in package b => then You can access protected fields, BUT ONLY IF YOU ACCESS THESE FIELDS AS THE FIELDS OF THE SUBCLASS. You can not access them through the object f (from the example), as in this case You have tried to access them through a different object, then the subclass itself.
Have a nice day!
Gabriel :o)
|