Java Reference
In-Depth Information
• A member (class, interface, field, or method) of a reference (class, interface, or ar-
ray) type or a constructor of a class type is accessible only if the type is accessible
and the member or constructor is declared to permit access:
♦ If the member or constructor is declared
public
, then access is permitted.
All members of interfaces are implicitly
public
.
♦ Otherwise, if the member or constructor is declared
protected
, then access is
permitted only when one of the following is true:
Access to the member or constructor occurs from within the package con-
taining the class in which the protected member or constructor is declared.
Access is correct as described in §
6.6.2
.
♦ Otherwise, if the member or constructor is declared
private
, then access is per-
encloses the declaration of the member or constructor.
♦ Otherwise, we say there is
default access
, which is permitted only when the ac-
cess occurs from within the package in which the type is declared.
Example 6.6-1. Access Control
Consider the two compilation units:
package points;
class PointVec { Point[] vec; }
and:
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
:
• The class type
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.
• The class type
Point
is declared
public
and is available to other packages. It is
part of the
public
interface of the package
points
.