G
giuseppe.on.usenet
/* file: ~/dir1/Parent.java */
package dir1;
public class Parent {
protected int var = 1;
}
/* end Parent.java */
/* file: ~/dir2/Child.java */
package dir2;
import dir1.Parent;
class Child extends Parent {
public void test() {
System.out.println("var is " + var);
}
public static void main(String[] args) {
Child c = new Child();
c.test();
}
}
/* end Child.java */
/* file: ~/dir2/GrandChild.java */
package dir2;
public class GrandChild extends Child {
public void test2() {
System.out.println("super.var is " + super.var);
System.out.println("var is " + var);
}
public static void main(String[] args) {
GrandChild gc = new GrandChild();
gc.var = 2;
Child c = new Child();
gc.test2();
}
}
/* end GrandChild.java */
When I run dir2.Child, I get:
var is 1
which is what I was expecting.
When I run dir2.GrandChild, I get:
super.var is 2
var is 2
These last two variables have the same value, although I only modified
gc.var in GrandChild.java. Why?
Thank you.
package dir1;
public class Parent {
protected int var = 1;
}
/* end Parent.java */
/* file: ~/dir2/Child.java */
package dir2;
import dir1.Parent;
class Child extends Parent {
public void test() {
System.out.println("var is " + var);
}
public static void main(String[] args) {
Child c = new Child();
c.test();
}
}
/* end Child.java */
/* file: ~/dir2/GrandChild.java */
package dir2;
public class GrandChild extends Child {
public void test2() {
System.out.println("super.var is " + super.var);
System.out.println("var is " + var);
}
public static void main(String[] args) {
GrandChild gc = new GrandChild();
gc.var = 2;
Child c = new Child();
gc.test2();
}
}
/* end GrandChild.java */
When I run dir2.Child, I get:
var is 1
which is what I was expecting.
When I run dir2.GrandChild, I get:
super.var is 2
var is 2
These last two variables have the same value, although I only modified
gc.var in GrandChild.java. Why?
Thank you.