fourth chapter classes and Interfaces13th minimizing the accessibility of classes and members
1. A well-designed module hides all implementation details and separates its API from its implementation.
modules communicate only through their APIs, and one module does not need to know the internal work of other modules: that is, information hiding or encapsulation is one of the basic principles of software design.
2.Java provides a number of mechanisms to assist in information hiding. The access control mechanism determines the accessibility of classes, interfaces, and members (accessibility).
3. The first rule: make every class or member non-accessible to the outside world as much as possible.
Or, in the case of guaranteed functionality, use the minimum access level.
4. There are four possible levels of access for members (domains, methods, nested classes, and nested interfaces).
Private--This member can only be accessed within the top-level class that declares the member.
Package-level Private (package-private)-the member can be accessed by any class within the package that declares the member. Is the access level of the default (default).
protected (protected)-the subclass of the class that declares the member can access this member, and any class within the package that declares the member can also access the member.
Public-the member can be accessed everywhere.
5. If the method overrides one of the methods in the superclass, the access level in the subclass does not allow lower than the access level in the superclass.
This ensures that instances of subclasses can be used wherever instances of the parent class can be used. If this rule is violated, the compiler generates an error message.
The implementation of the interface also follows this rule: all class methods in an interface must be declared public in the implementation class. Because all methods in an interface imply a public access level.
"Effective Java second Edition" learning note 13th to minimize the accessibility of classes and members