public
, then it may be accessed by any Java code that can access the package in which it is declared. If a class or interface type is not declared public
, then it may be accessed only from within the package in which it is declared.
public
, then access is permitted. All members of interfaces are implicitly public
.
protected
, then access is permitted only when one of the following is true:
private
, then access is permitted only when it occurs from within the class in which it is declared.
public
and Non-public
Classespublic
modifier, access to the class declaration is limited to
the package in which it is declared (§6.6). In the example:
package points;
public class Point { public int x, y; public void move(int dx, int dy) { x += dx; y += dy; } }two classes are declared in the compilation unit. The class
class PointList { Point next, prev; }
Point
is available outside the package points
, while the class PointList
is available for access only
within the package. Thus a compilation unit in another package can access
points.Point
, either by using its fully qualified name:
package pointsUser;
class Test { public static void main(String[] args) { points.Point p = new points.Point(); System.out.println(p.x + " " + p.y); } }or by using a single-type-import declaration (§7.5.1) that mentions the fully qualfied name, so that the simple name may be used thereafter:
package pointsUser;
import points.Point;
class Test { public static void main(String[] args) { Point p = new Point(); System.out.println(p.x + " " + p.y); } }However, this compilation unit cannot use or import
points.PointList
, which
is not declared public
and is therefore inaccessible outside package points
.
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. 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.
public
Fields, Methods, and Constructorspublic
class member or constructor is accessible throughout the package
where it is declared and from any other package that has access to the package in
which it is declared. For example, in the compilation unit:
package points;
public class Point {the
int x, y;
public void move(int dx, int dy) { x += dx; y += dy; moves++; }
public static int moves = 0;
}
public
class Point
has as public
members the move
method and the moves
field. These public
members are accessible to any other package that has access
to package points
. The fields x
and y
are not public
and therefore are accessible
only from within the package points
.