Java Reference
In-Depth Information
This method is then invoked whenever the target object for an invocation of clear is
a ColoredPoint . Even the method move in Point invokes the clear method of class Co-
loredPoint when the class of this is ColoredPoint , as shown by the output of this test pro-
gram:
Click here to view code image
class Test1 {
public static void main(String[] args) {
Point p = new Point();
System.out.println("p.move(20,20):");
p.move(20, 20);
ColoredPoint cp = new ColoredPoint();
System.out.println("cp.move(20,20):");
cp.move(20, 20);
p = new ColoredPoint();
System.out.println("p.move(20,20), p colored:");
p.move(20, 20);
}
}
which is:
p.move(20,20):
Point clear
cp.move(20,20):
ColoredPoint clear
Point clear
p.move(20,20), p colored:
ColoredPoint clear
Point clear
Overriding is sometimes called “late-bound self-reference”; in this example it means
that the reference to clear in the body of Point.move (which is really syntactic shorthand
for this.clear ) invokes a method chosen “late” (at run time, based on the run-time class
of the object referenced by this ) rather than a method chosen “early” (at compile time,
based only on the type of this ). This provides the programmer a powerful way of ex-
tending abstractions and is a key idea in object-oriented programming.
Example 15.12.4.4-2. Method Invocation Using super
An overridden instance method of a superclass may be accessed by using the keyword
super to access the members of the immediate superclass, bypassing any overriding
declaration in the class that contains the method invocation.
Search WWH ::




Custom Search