I am having following classes
public class InheritTest{
Parent p = new Child();
Child c = (Child)p;
void someMethod(){
System.out.println(p.getX()+" "+c.getX());
}
public static void main (String [] args){
InheritTest it = new InheritTest();
it.someMethod();
}
class Child extends Parent{
int x = 1;
int getX(){
return this.x;
}
}
}
And
public class Parent{
int x = 0;
int getX(){
return x;
}
}
When I call, p.getX() and c.getX(), it prints 1 & 1
but when I call p.x and c.x, it prints 0 & 1
how this multi-level inheritance works ? Please help me understand this.