Java Access Controller
Java provides three access control operators: private, protected, and public, which represent three access control levels. In addition, there is an access control level without any access control operators, provides four access control levels. The Java access control level ranges from small to large:
The default of the four access control levels in the figure does not have the corresponding access control operator. When no access control operator is used to modify class or class members, the system uses this access control level by default. The four access control levels are described as follows.
- Private (access permission of the current class): If a member (including member variables, methods, constructors, and so on) in the class is modified using the private access controller, the member can only be accessed within the current class. Obviously, this access control operator is most suitable for modifying member variables. using it to modify member variables can hide member variables inside the class.
- Default (package access permission): If a member (including member variables, methods, constructors, and so on) in the class or an external class does not use any access control modifier, the default access control member or external class can be accessed by other classes under the same package.
- Protected (subclass access permission): If a member (including member variables, methods, constructors, and so on) is modified using the protected access controller, this member can be accessed by other classes in the same package or by sub-classes in different packages. In general, if you use protected to modify a method, you usually want its subclass to override this method.
- Public (public access permission): This is the loosest access control level. If a member (including member variables, methods, constructors, and so on) or an external class is modified with a public access control character, this member or external class can be accessed by all classes, regardless of whether the parent class or external class is in the same package or whether it has a parent-child inheritance relationship.