"Effective Java second Edition" learning note 13th to minimize the accessibility of classes and members

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.