For examples of access control, consider the two compilation units:
package points;
class PointVec { Point[] vec; }
package points;
public class Point { protected int x, y; public void move(int dx, int dy) { x += dx; y += dy; } public int getX() { return x; } public int getY() { return y; } }
which declare two class types in the package points
:
PointVec
is not public
and not part of the public
interface of the package points
, but rather can be used only by other classes in the package.
Point
is declared public
and is available to other packages. It is part of the public
interface of the package points
.
move
, getX
, and getY
of the class Point
are declared public
and so are available to any Java code that uses an object of type Point
.
x
and y
are declared protected
and are accessible outside the package points
only in subclasses of class Point,
and only when they are fields of objects that are being implemented by the code that is accessing them.
See §6.6.7 for an example of how the protected
access modifier limits access.