Java Access Qualifiers
| Java Access Qualifiers, in Least-Restrictive to Most-Restrictive Order | ||||
|---|---|---|---|---|
| Access Type |
Accessible from
same class? |
Accessible from
any class in same package? |
Accessible from
any subclass anywhere? |
Accessible from
any class anywhere? |
| public | yes | yes | yes | yes |
| protected | yes | yes | yes | no |
| package (default) | yes | yes | no | no |
| private | yes | no | no | no |
| Access Rules Just Related to Inheritance | ||
|---|---|---|
| Access Type |
Accessible from
any subclass in same package? |
Accessible from
any subclass anywhere? |
| public | yes | yes |
| protected | yes | yes |
| package (default) | yes | no |
| private | no | no |
The following character-based diagrams,showing the access qualifier information in pictorial form, are based on those in "On to Java" by Patrick Winston:
public
*------------------------------------------------------------*
| Everywhere else |
*------------------------ | ---------------------------------*
|
|
|
*------------------------ | ---------------------------------*
| *---------------------- | -------* *-------------------* |
| | *----- v -----* | | | |
| | | Same | | | | |
| | | class | | | | |
| | | | | | | | |
| | Rest of the | v | | | Other compilation | |
| | compilation -----> public <-------- unit, same package| |
| | unit | ^ | | | | |
| | | | | | | | |
| | *----- | -----* | | | |
| *---------------------- | -------* *-------------------* |
*------------------------ | ---------------------------------*
|
|
|
*------------------------ | ---------------------------------*
| *---------------------- | -------------------------------* |
| | *----------- | -------* | |
| | | Subclass in another | | |
| | | compilation unit in | | |
| | | another package | | |
| | *---------------------* | |
| *--------------------------------------------------------* |
*------------------------------------------------------------*
protected
*------------------------------------------------------------*
| Everywhere else |
*------------------------ | ---------------------------------*
|
|
| ^
*-*
*------------------------------------------------------------*
| *--------------------------------* *-------------------* |
| | *-------------* | | | |
| | | Same | | | | |
| | | class | | | | |
| | | | | | | | |
| | Rest of the | v | | | Other compilation | |
| | compilation ----> protected <------- unit, same package| |
| | unit | ^ | | | | |
| | | | | | | | |
| | *----- | -----* | | | |
| *---------------------- | -------* *-------------------* |
*------------------------ | ---------------------------------*
|
|
|
*------------------------ | ---------------------------------*
| *---------------------- | -------------------------------* |
| | *----------- | -------* | |
| | | Subclass in another | | |
| | | compilation unit in | | |
| | | another package | | |
| | *---------------------* | |
| *--------------------------------------------------------* |
*------------------------------------------------------------*
package (default)
*------------------------------------------------------------*
| Everywhere else |
*------------------------ | ---------------------------------*
|
|
| ^
*-*
*------------------------------------------------------------*
| *--------------------------------* *-------------------* |
| | *-------------* | | | |
| | | Same | | | | |
| | | class | | | | |
| | | | | | | | |
| | Rest of the | v | | | Other compilation | |
| | compilation -----> default <-------- unit, same package| |
| | unit | | | | | |
| | *-------------* | | | |
| *--------------------------------* *-------------------* |
*------------------------------------------------------------*
*-*
| v
|
|
*------------------------ | ---------------------------------*
| *---------------------- | -------------------------------* |
| | *----------- | -------* | |
| | | Subclass in another | | |
| | | compilation unit in | | |
| | | another package | | |
| | *---------------------* | |
| *--------------------------------------------------------* |
*------------------------------------------------------------*
private
*------------------------------------------------------------*
| Everywhere else |
*------------------------ | ---------------------------------*
| ^
*-*
*------------------------------------------------------------*
| *--------------------------------* *-------------------* |
| | *-------------* | | | |
| | | Same | | | | |
| | | class | | | | |
| | | | | | | | |
| | Rest of the | v | | | Other compilation | |
| | compilation --*| private | |*--- unit, same package| |
| | unit || | || | | |
| | <-*| | |*->| | |
| | *-------------* | | | |
| *--------------------------------* *-------------------* |
*------------------------------------------------------------*
*-*
| v
*------------------------ | ---------------------------------*
| *---------------------- | -------------------------------* |
| | *----------- | -------* | |
| | | Subclass in another | | |
| | | compilation unit in | | |
| | | another package | | |
| | *---------------------* | |
| *--------------------------------------------------------* |
*------------------------------------------------------------*