The Java Program: AccessField.java

  1 // AccessField.java -- Accessing members using super (from Spec, 2nd, §15.11.2)
  2 
  3 interface I { int x = 0; }
  4 class T1 implements I { int x = 1; }
  5 class T2 extends T1 { int x = 2; }
  6 class T3 extends T2 {
  7    int x = 3;
  8    void test() {
  9       System.out.println("x    =  "+x);
 10       System.out.println("super.x   =  "+super.x);
 11       System.out.println("((T2)this).x= "+((T2)this).x);
 12       System.out.println("((T1)this).x= "+((T1)this).x);
 13       System.out.println("((I)this).x = "+((I)this).x);
 14       if (this instanceof T4) {
 15          System.out.println("((T4)this).x= "+((T4)this).x);
 16       }
 17    }
 18 }
 19 class T4 extends T3 {
 20    int x = 4;
 21 }
 22 
 23 class AccessField {
 24    public static void main(String[] args) {
 25       System.out.println ("T3 object");
 26       new T3().test();         // 3,2,2,1,0
 27       System.out.println ("\n(T3) T4 object");
 28       ((T3) new T4()).test();  // 3,2,2,1,0,4
 29    }
 30 }