8.4.8
Examples of Method Declarations
CLASSES
8.4.8.1
Example: Overriding
In the example:
class Point {
int x = 0, y = 0;
void move(int dx, int dy) { x += dx; y += dy; }
}
class SlowPoint extends Point {
int xLimit, yLimit;
void move(int dx, int dy) {
super.move(limit(dx, xLimit), limit(dy, yLimit));
}
static int limit(int d, int limit) {
return d > limit ? limit : d < limit ? limit : d;
}
}
the class
SlowPoint
overrides the declarations of method
move
of class
Point
with its own
move
method, which limits the distance that the point can move on
each invocation of the method. When the
move
method is invoked for an instance
of class
SlowPoint
, the overriding definition in class
SlowPoint
will always be
called, even if the reference to the
SlowPoint
object is taken from a variable
whose type is
Point
.
8.4.8.2
Example: Overloading, Overriding, and Hiding
In the example:
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; }
}
168
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