Modifier
public
protected
no modifier (package scope or package-private)
private
- At the top level—
public
, or package-private (no explicit modifier). The top level here means Class level - At the member level—
public
,private
,protected
, or package-private (no explicit modifier).
public
, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package>> At the member level, you can also use the
public
modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private
and protected
. The private
modifier specifies that the member can only be accessed in its own class. The protected
modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.Example:
Classes and Packages of the Example Used to Illustrate Access Levels
The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.
Visibility Modifier Alpha Beta Alphasub Gamma public
Y Y Y Y protected
Y Y Y N no modifier Y Y N N private
Y N N N