I am reading the SCP for
Java 6 by S&B. Good book, like it.
Small question from the first chapter.
It talks about the protected/default variable and method declaration. Here is the piece of code:
----
package certification;
public class Parent{
protected int x=9;
}
--
package other;
import certification.Parent;
class Child extends Parent {//inheriting the parent
public void testIt () {
System.out.println("x is " + x); // call 1
}
public static void main (
String[] arg) {
System.out.println("x is" + x); //call 2
}
}
======
My question. call to x from call 1 is fine, but it fails to compile due to call 2. My assumption is since it was declared protected, it sh be visible in the subclass thru inheritence all the time.? Am I doing somethign wrong.? It works if use the reference of the child class in the main method.