(1) For public modifier, it has the largest access permissions and can access any class, interface, exception, etc. under CLASSPATH. It is often used in external cases, that is, an interface form of an object or class external to the external.
(2) For the protected modifier, its main function is to protect subclasses. Its meaning is that the subclass can be modified by it, and the others cannot be. It is equivalent to an inherited thing passed to the subclass.
(3) For default, it sometimes becomes a friendly member. It is designed for access to this package. Any classes, interfaces, exceptions, etc. that are under this package can be accessed by each other, even members of the parent class that are not modified with protected.
(4) For private, its access permissions are limited to the inside of the class, which is a manifestation of encapsulation. For example, most member variables are modifiers private, and they do not want to be accessed by any other external class.
The following table shows the meaning and usage of Java access control characters
| Inside the class | This package | Subclass | External package | |
| public | √ | √ | √ | √ |
| protected | √ | √ | √ | × |
| default | √ | √ | × | × |
| Private | √ | × | × | × |
Note: Java's access control stays at the compilation layer, that is, it will not leave any traces in the .class file, and only checks access control during compilation. In fact, through reflection, you can access members of any class under any package. For example, it is also possible to access private members of the class.
the difference:
(1) public: It can be accessed by all other classes.
(2) private: can only be accessed and modified by yourself.
(3) protected: It can be accessed by itself, subclasses and classes in the same package.
(4) default: Classes in the same package can be accessed, and no modifier is added when declaring, so it is considered friendly.
The above is all the content of the difference (detailed explanation) in Java brought to you by the editor. I hope everyone will support Wulin.com~