Java Reference
In-Depth Information
but this equivalence does not hold true for method invocation. This is demonstrated
by the example:
class T1 {
String s() { return "1"; }
}
class T2 extends T1 {
String s() { return "2"; }
}
class T3 extends T2 {
String s() { return "3"; }
void test() {
System.out.println("s()=\t\t" + s());
System.out.println("super.s()=\t" + super.s());
System.out.println("((T2)this).s()=\t" + ((T2)this).s());
System.out.println("((T1)this).s()=\t" + ((T1)this).s());
}
}
class Test2 {
public static void main(String[] args) {
T3 t3 = new T3();
t3.test();
}
}
which produces the output:
s()= 3
super.s()= 2
((T2)this).s()= 3
((T1)this).s()= 3
The casts to types
T1
and
T2
do not change the method that is invoked, because the
instance method to be invoked is chosen according to the run-time class of the object
referred to by
this
. A cast does not change the class of an object; it only checks that the
class is compatible with the specified type.
15.12.4.5. Create Frame, Synchronize, Transfer Control
A method
m
in some class
S
has been identified as the one to be invoked.
Now a new
activation frame
is created, containing the target reference (if any) and the ar-
gument values (if any), as well as enough space for the local variables and stack for the
method to be invoked and any other bookkeeping information that may be required by