If none of the access modifiers public
, protected
, or private
are specified, a
class member or constructor is accessible throughout the package that contains the
declaration of the class in which the class member is declared, but the class member or constructor is not accessible in any other package. If a public
class has a
method or constructor with default access, then this method or constructor is not
accessible to or inherited by a subclass declared outside this package.
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 same signature (§8.4.2) and return type, but because the original move
method
is not accessible 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.