I am trying to find proof for the statement - keyword super is the reference to parent class just like keyword this is the reference to current class.
I am trying multilevel Inheritance in Java A->B->C: class A is grand parent, class B is parent, class C is child.
I have a variable X declared in all three classes with values respectively (A:x=100,B:x=200,C:x=300)
In the child class constructor I am printing values. However the casting isn't working for super keyword whereas it's working for this keyword.
((A)super).x is not working, but ((A)this).x is working.
class A {
int x = 100;
}
class B extends A {
int x = 200;
}
public class C extends B {
int x = 300;
public C () {
System.out.println(this.x); //OP = 300
System.out.println(super.x); // OP = 200
System.out.println(((A)this).x);// OP = 100
System.out.println(((A)super).x); // Giving Compile time Error.. Why?
B reftoB = new B();
System.out.println(((A)reftoB).x); // OP = 100
}
public static void main(String[] args) {
C t1= new C();
}
}
I expect the output of System.out.println(((A)super).x) is 100, but it is giving a compile time error.
So my question is if super is a reference to the parent class then why isn't type casting working on it?