Java Reference
In-Depth Information
package points;
public class Point {
public int x, y;
void move(int dx, int dy) { x += dx; y += dy; }
public void moveAlso(int dx, int dy) { move(dx, dy); }
}
then a subclass in another package may declare an unrelated
move
method, with the
cessible from package
morepoints
,
super
may not be used:
package morepoints;
public class PlusPoint extends points.Point {
public void move(int dx, int dy) {
super.move(dx, dy); // compile-time error
moveAlso(dx, dy);
}
}
Because
move
of
Point
is not overridden by
move
in
PlusPoint
, the method
moveAlso
in
Point
never calls the method
move
in
PlusPoint
. Thus if you delete the
super.move
call
from
PlusPoint
and execute the test program:
import points.Point;
import morepoints.PlusPoint;
class Test {
public static void main(String[] args) {
PlusPoint pp = new PlusPoint();
pp.move(1, 1);
}
}
it terminates normally. If
move
of
Point
were overridden by
move
in
PlusPoint
, then this
program would recurse infinitely, until a
StackOverflowError
occurred.
Example 6.6-5. Access to
private
Fields, Methods, and Constructors
A
private
class member or constructor is accessible only within the body of the top
level class (§
7.6
) that encloses the declaration of the member or constructor. It is not
inherited by subclasses. In the example: