15.10.2 Accessing Superclass Members using super

The special form using the keyword super is valid only in an instance method or constructor, or in the initializer of an instance variable of a class; these are exactly the same situations in which the keyword this may be used (§15.7.2). The form involving super may not be used anywhere in the class Object, since Object has no superclass; if super appears in class Object, then a compile-time error results.

Suppose that a field access expression super.name appears within class C, and the immediate superclass of C is class S. Then super.name is treated exactly as if it had been the expression ((S)this).name; thus, it refers to the field named name of the current object, but with the current object viewed as an instance of the superclass. Thus it can access the field named name that is visible in class S, even if that field is hidden by a declaration of a field named name in class C.

The use of super is demonstrated by the following example:


interface I { int x = 0; }
class T1 implements I { int x = 1; }
class T2 extends T1 { int x = 2; }
class T3 extends T2 {
	int x = 3;
	void test() {
		System.out.println("x=\t\t"+x);
		System.out.println("super.x=\t\t"+super.x);
		System.out.println("((T2)this).x=\t"+((T2)this).x);
		System.out.println("((T1)this).x=\t"+((T1)this).x);
		System.out.println("((I)this).x=\t"+((I)this).x);
	}
}
class Test {
	public static void main(String[] args) {
		new T3().test();
	}
}

which produces the output:


x=					3
super.x=					2
((T2)this).x=					2
((T1)this).x=					1
((I)this).x=					0

Within class T3, the expression super.x is treated exactly as if it were:

((T2)this).x