Java Reference
In-Depth Information
It is a compile-time error if the forms using the keyword
super
appear in the declaration of
class
Object
, since
Object
has no superclass.
If 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
this.
name
in
the body of class S
. Thus it can access the field
name
that is visible in class
S
, even if that
field is hidden by a declaration of a field
name
in class
C
.
If a field access expression
T
.super.
name
appears within class
C
, and the immediate super-
class of the class denoted by
T
is a class whose fully qualified name is
S
, then
T
.super.
name
is treated exactly as if it had been the expression
this.
name
in the body of class S
. Thus it can
access the field
name
that is visible in class
S
, even if that field is hidden by a declaration
of a field
name
in class
T
.
It is a compile-time error if the current class is not an inner class of class
T
or
T
itself.
Example 15.11.2-1. The
super
Expression
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();
}
}
This program produces the output:
x= 3
super.x= 2
((T2)this).x= 2
((T1)this).x= 1