(1) for the public modifier, it has the largest access and can access any one of the classes, interfaces, exceptions, and so on under Classpath. It is often used for external situations, i.e. the form of an interface to an object or class.
(2) for the protected modifier, its main function is to protect the subclass. It means that subclasses can use it to modify the members, others can not, it is equivalent to pass to the subclass of an inherited thing.
(3) for default, a bit of time also become friendly (friend), it is designed for this package access, any in this package of classes, interfaces, exceptions, etc., can access each other, even if the parent class is not decorated with protected members can also be.
(4) for private, its access is limited to the interior of the class and is an embodiment of encapsulation, for example, most of the member variables are modifiers private and do not want to be accessed by any other external class.
The following table is the meaning and usage of Java access control characters
|
Class Internal |
This package |
Sub Class |
External Package |
Public |
√ |
√ |
√ |
√ |
Protected |
√ |
√ |
√ |
X |
Default |
√ |
√ |
X |
X |
Private |
√ |
X |
X |
X |
Note: Java access control is stuck at the compile level, which means that it does not leave any traces in the. class file and only checks for access control at compile time. In fact, by means of reflection, it is possible to access members of any class under any package, for example, to access the private members of a class.
Difference:
(1) Public: can be accessed by all other classes.
(2) Private: can only be accessed and modified by themselves.
(3) Protected: itself, subclasses and classes in the same package can be accessed.
(4) Default: Classes in the same package can be accessed, and declarations without modifiers are considered friendly.
The above is a small series for everyone to bring the private in Java, Protected, public and default of the difference (detailed) all content, I hope that we support cloud Habitat Community ~