Java Reference
In-Depth Information
Click here to view code image
class Test {
public static void main(String[] args) {
RealPoint rp = new RealPoint();
Point p = rp;
rp.move(1.71828f, 4.14159f);
p.move(1, -1);
show(p.x, p.y);
show(rp.x, rp.y);
show(p.getX(), p.getY());
show(rp.getX(), rp.getY());
}
static void show(int x, int y) {
System.out.println("(" + x + ", " + y + ")");
}
static void show(float x, float y) {
System.out.println("(" + x + ", " + y + ")");
}
}
The output from this program is:
(0, 0)
(2.7182798, 3.14159)
(2, 3)
(2, 3)
The first line of output illustrates the fact that an instance of RealPoint actually contains
the two integer fields declared in class Point ; it is just that their names are hidden from
code that occurs within the declaration of class RealPoint (and those of any subclasses
it might have). When a reference to an instance of class RealPoint in a variable of type
Point is used to access the field x , the integer field x declared in class Point is accessed.
The fact that its value is zero indicates that the method invocation p.move(1, -1) did not
invoke the method move of class Point ; instead, it invoked the overriding method move
of class RealPoint .
The second line of output shows that the field access rp.x refers to the field x declared
in class RealPoint . This field is of type float , and this second line of output accordingly
displays floating-point values. Incidentally, this also illustrates the fact that the meth-
od name show is overloaded; the types of the arguments in the method invocation dic-
tate which of the two definitions will be invoked.
The last two lines of output show that the method invocations p.getX() and rp.getX()
each invoke the getX method declared in class RealPoint . Indeed, there is no way to in-
Search WWH ::




Custom Search