15.11.4
Runtime Evaluation of Method Invocation
EXPRESSIONS
15.11.4.10
Example: Method Invocation using
super
An overridden instance method of a superclass may be accessed by using the key
word
super
to access the members of the immediate superclass, bypassing any
overriding declaration in the class that contains the method invocation.
When accessing an instance variable,
super
means the same as a cast of
this
( 15.10.2), 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.print("((T2)this).s()=\t");
System.out.println(((T2)this).s());
System.out.print("((T1)this).s()=\t");
System.out.println(((T1)this).s());
}
}
class Test {
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 be
this
. A cast does not change the class of an object; it only
checks that the class is compatible with the specified type.
340
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved