There are four access control operators in the Java programming language. These four access control operators can control the visibility of class members.
1. Public
(1) Definition: public is public, and the modified members of public can be accessed in any class.
(2) modified ingredients:
Public can be used to modify classes. In a Java source file, only one class can be declared as public, and once one class is public, the file name of the Java source file must be the same as the class name of the public-modified class; otherwise, the compilation fails. Here, we will introduce a little more knowledge. When a class is used as an external class, it can only be modified by public or default access modifiers. However, if a class is used as an internal class, it can be modified by four access modifiers, when a class acts as an internal class, it acts as a Member attribute of the external class. Therefore, there are four access modifiers, which are a difference between the internal class and the external class.
Public is used to modify the members (variables and methods) in the class. The members modified by public can be accessed in any class. You can access public members by operating on objects of this class.
Public represents the inheritance of classes. The members modified by public can be inherited by all subclasses.
Ii. Protected
(1) Definition: protected is protected by the package where the class is located.
(2) scope: the member modified by protected will be accessed by all classes in the same package. Members modified by protected can also be inherited by all subclasses of the class. (Note: This indicates that sub-classes in the same package or different packages can be accessed)
3. Friendly (default, default)
(1) Definition: friendly is friendly, that is, it is friendly by default when no access modifier is written before a member. The class is friendly to the same package.
(2) Scope: All classes in the same package can be accessed. The member modified by friendly can only be inherited by the subclass in the same package where the class is located. (That is, only child classes in the same package can access the friendly modified members in the parent class)
Iv. Private
(1) Definition: Private is private, that is, it can only be accessed in the current class, and its scope is minimal.
The following table describes the scopes of the four access controllers:
Scope: The current class is the same package
Other package classes
Public √
√
Protected √
√ ×
Friendly √
√ ××
Private √ ×××
These are my understanding of the access control operators in Java. I hope to help some beginners of Java, just like me. Thank you!