I have three class
A
B
C
a
class A
class C
super
package testee;
import java.util.Scanner;
public class Testee {
public static void main(String[] args) {
new C();
}
}
class A{
int a=10;
A(){
System.out.println(a);
}
}
class B extends A{
int a=13;
B(){
System.out.println(a);
}
}
class C extends B{
int a=21;
C(){
System.out.println(super.a);
}
}
System.out.println(((A)this).a);
Having fields with the same name multiple times in a inheritance hierarchie is called "hidden fields".
Access to fields (and static methods) is based on the (static) type of the reference used. So that means if you cast the reference(here this) the type you want (in that case A) and access the field you get the field that belongs to A.
Would work the same way if you assign C to a variable. If the variable is of type A, you will get A.a.
Anyway: please, do not use hidden fields in production code