<<effective java>> the best use of combination rather than inheritance

Source: Internet
Author: User

The first thing about using the OO language in Java is that if the class is not designed to be inherited, try not to use the combination

From 2, our class B makes a copy of the add of Class A to drink the AddAll method, so that we can count the number of calls to Class A every time we call it, and we actually call Class B.

But the count variable of instance object B becomes 2, explaining the reason in Figure 1 I have done a blackboard painting, this example is effective Java inside a piece of advice, very meaningful,

Less inheritance, unless classes are specifically used to inherit, such as the activity classes in Android development are specifically designed for inheritance, these classes can be used for inheritance, and some classes that are not specifically designed for inheritance

Use the compound as much as possible, so that it does not confuse.

<<effective java>> the best use of combination rather than inheritance

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.