Java has four access rights, three of which have access modifiers, private,public and protected, and one without any modifiers. Where the package represents the default access rights, the code is the default case where no modifiers are written. For the sake of good, name the package for the moment.
Public
protected
Private
Package Access control for top-level code resourcesfor Class,interface,enum of file level (top-level), there are 2 access modifiers: Public and the default package. Public
: Can be used in any other place.
Package: A class,interface or enum that can be used only in the same packet. Other packages cannot be used. Java source code file stipulates that: A. java file can have only one class, interface, or enum that is decorated as public, and the name of the file must be the same as the public modifier. The general habit is a public-modified class, interface, and the enum corresponds to a separate 1. java file.
Access modifiers for class membersPublic
: Members can be used by any other code access.
protected: Direct access in this class, other code of the same package can be accessed directly, and any subclass can also be accessed directly.
Package: For other code in the same bundle, you can directly visit , equivalent to public, not the same package of other code, then the equivalent of private, hidden.
Private: Use can only be accessed in this class.
permission modifier |
private |
package< /th> |
protected |
public |
same class |
√ |
√&NBSP; |
√ |
√&NBSP; |
|
&NBSP; |
√ |
√ |
√ |
|
&nb SP; |
&NBSP; |
√ |
√&NBSP; |
other |
&NBSP; |
&NBSP; |
&NBSP; |
√ |
Access modifiers in Java