[Effective Java Reading notes] Chapter III Class and interface 16th

Source: Internet
Author: User

The 16th compound takes precedence over inheritance

If you are unsure of the relationship between B and a, and B is indeed a, then you should not use B to inherit a, or you will expose the implementation details, and your implementation will be limited to the original implementation, Is-a.

The first example of the book cited implements a class extends HashSet class, because the lack of understanding of the AddAll method of the HashSet class (AddAll calls the Add method repeatedly), resulting in a multiple count of calls, which is the implementation of their own constraints on the implementation of the parent class.

There is also a note in the book that if the superclass adds a new method to the subsequent version, exactly the same as the new method in your class (the argument list and the function name match), but the return value is inconsistent, then a compilation error occurs (the function signature consistent return value is inconsistent and cannot be overloaded).

The use of composite methods can solve these problems, the book cited example, is to use the Instrumentedset class to wrap set instances, the statistical steps are implemented in Instrumentedset, and the operation of the set independent, with an object to operate

[Effective Java Reading notes] Chapter III Class and interface 16th

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.