10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

EXPRESSIONS Accessing Superclass Members using super 15.11.2<br />

}<br />

int x = 3;<br />

void test() {<br />

System.out.println("x=\t\t"+x);<br />

System.out.println("super.x=\t\t"+super.x);<br />

System.out.println("((T2)this).x=\t"+((T2)this).x);<br />

System.out.println("((T1)this).x=\t"+((T1)this).x);<br />

System.out.println("((I)this).x=\t"+((I)this).x);<br />

}<br />

class Test {<br />

public static void main(String[] args) {<br />

new T3().test();<br />

}<br />

}<br />

which produces the output:<br />

x= 3<br />

super.x= 2<br />

((T2)this).x=2<br />

((T1)this).x=1<br />

((I)this).x= 0<br />

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

((T2)this).x<br />

Suppose that a field access expression T.super.name appears within class C,<br />

and the immediate superclass of the class denoted by T is a class whose fully qualified<br />

name is S. <strong>The</strong>n T.super.name is treated exactly as if it had been the<br />

expression ((S)T.this).name.<br />

Thus the expression T.super.name can access the field named name that is<br />

visible in the class named by S , even if that field is hidden by a declaration of a<br />

field named name in the class named by T.<br />

It is a compile-time error if the current class is not an inner class of class T or<br />

T itself.<br />

DRAFT<br />

439

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!