Java Reference
In-Depth Information
void move(float dx, float dy) { x += dx; y += dy; }
public String toString() { return "("+x+","+y+")"; }
}
Here, the class
Point
has two members that are methods with the same name,
move
. The
overloaded
move
method of class
Point
chosen for any particular method invocation is
determined at compile time by the overloading resolution procedure given in §
15.12
.
In total, the members of the class
Point
are the
float
instance variables
x
and
y
declared
in
Point
, the two declared
move
methods, the declared
toString
method, and the members
hashCode
. Note that
Point
does not inherit the
toString
method of class
Object
because that
method is overridden by the declaration of the
toString
method in class
Point
.
Example 8.4.9-2. Overloading, Overriding, and Hiding
class Point {
int x = 0, y = 0;
void move(int dx, int dy) { x += dx; y += dy; }
int color;
}
class RealPoint extends Point {
float x = 0.0f, y = 0.0f;
void move(int dx, int dy) { move((float)dx, (float)dy); }
void move(float dx, float dy) { x += dx; y += dy; }
}
Here, the class
RealPoint
hides the declarations of the
int
instance variables
x
and
y
of
class
Point
with its own
float
instance variables
x
and
y
, and overrides the method
move
of class
Point
with its own
move
method. It also overloads the name
move
with another
method with a different signature (§
8.4.2
).
In this example, the members of the class
RealPoint
include the instance variable
color
inherited from the class
Point
, the
float
instance variables
x
and
y
declared in
RealPoint
,
and the two
move
methods declared in
RealPoint
.
Which of these overloaded
move
methods of class
RealPoint
will be chosen for any par-
ticular method invocation will be determined at compile time by the overloading res-
This following program is an extended variation of the preceding program: