how (((GrandParent)this).x)) able to print value of x of GP class, how after inheritance object is created in heap. how it store grandparent nd parent data .
class GrandParent
{
int x = 34;
}
class Parent extends GrandParent
{
int x =87;
}
class Child extends Parent
{
int x =536;
void show()
{
System.out.println("value of local x "+x);
System.out.println("value of Parent x "+super.x);
System.out.println("value of GrandParent x "+(((GrandParent)this).x));
}
public static void main(String... s)
{
Child c1 = new Child();
c1.show();
}
}
output
value of local x 536
value of Parent x 87
value of GrandParent x 34