The modifiers in Java have not been remembered, now combined with some information on the web to summarize, and focus on the protected modifier.
I. Access modifiers in Java
One of the basic concepts of Java object-oriented is to encapsulate the details and expose the interface. The Java language uses access control modifiers to control access to the methods and variables of classes and classes, exposing the interface to the consumer, but hiding the implementation details. Access control is divided into four levels:
(1)public: classes, generic variables and methods that are modified with public, and any class within and outside the package (including subclasses and ordinary classes) can be accessed;
(2)protected: class, generic variables and methods modified with protected, any class within the package, and the subclasses of the package that inherit the class to be accessible ( explained later ), protected emphasis on succession;
(3)default: If a class, generic variables, and methods do not have any modifiers (that is, none of the public, protected, and private), then their access rights are default (access rights are defaulted). Implied
Interview class, generic variables, and methods that ask permission, any class within the package (including subclasses inheriting from this class) can access it, and it cannot be accessed for any class outside of the package (including subclasses that inherit this class outside of the package). default
Focus Highlight Package ;
(4)private: class, generic variables and methods that are modified with private, only this class can access, and any class outside the package cannot access it.
some information on the web and some books on the Java access modifiers are clearly summarized in the table, as shown in the following table:
Access level |
Access Control modifiers |
Similar |
Same package |
Sub-class |
Different packages |
Public |
Public |
√ |
√ |
√ |
√ |
be protected |
Protected |
√ |
√ |
√ |
-- |
Default |
No access control modifiers |
√ |
√ |
-- |
-- |
Private |
Private |
√ |
-- |
-- |
-- |
I think that the table some problems are not clear, such as the access modifier protected, different packages can not be accessed, and the subclass can access, then ask in different packages of sub-class can be accessed or not access it? Therefore I am in the self
On the basis of understanding, for their own convenience, easy to remember, re-organized a table as follows:
Access level |
Access Control modifiers |
Similar |
Same package different classes (excluding subclasses) |
Same bun Category |
Different packages different classes (Does not contain sub-categories) |
different bun types |
Expose |
Public |
√ |
√ |
√ |
√ |
√ |
Protected |
protected |
√ |
√ |
√ |
-- |
< Span style= "color: #ff0000;" > √ (note) |
Default |
No access control modifiers |
√ |
√ |
√ |
-- |
-- |
Private |
Private |
√ |
--- |
--- |
-- |
-- |
Important summary: Through the above analysis, we can see:
1. public , private and protected have no objection to us.
2. The top-level class can only be decorated with the public access modifier and the default access modifier, where classes decorated with the default modifier (and classes without any modifiers, such as Class b{}) cannot be inherited by classes in other packages. This also indicates that the default access modifier highlights the package permissions
3. Protected: I did an experiment, found in the subclass of the different packages, new A parent class object, and use the parent class object to access the parent class with protected decorated generic variables and methods can not be accessed, and new a subclass object, Subclass objects can be accessed (the class that describes the protected adornment can be inherited by classes in other packages). You can also use the Super keyword call in a method of a subclass override parent class. Does this conflict with the summary (red tick) in the table above? I am also baffled by the solution. Finally, a relatively more acceptable explanation was found on the Internet, as follows:
The decorated member variables and methods of the protected modifier are also known as protected member variables and methods, and protected member variables and methods can be accessed through instances of the class in this class or in other classes in the same package, including subclasses, or by classes in the same package or classes in different packages. However, it cannot be accessed through an instance of the class in other classes (including subclasses) in different packages.
4. If a class uses public adornments, the class name of the class must be the same as the name of the source file where he resides. A. Java source file has only one public class, and the top-level class can only be decorated with public and default modifiers (that is, no modifiers);
5. The final decorated class cannot be inherited, and there are no subclasses.
6. Abstract modified classes cannot be instantiated, they must be inherited by the quilt class. A class must be an abstract class as long as it has an abstract method, but abstract classes do not have to be abstract.
Access modifiers in Java