Differences between Java inheritance and combination

Source: Internet
Author: User

1) The combination (has-a) relationship can explicitly obtain the object of the contained class (inherited called the parent class), while the Inheritance (is-) the object of the parent class is obtained implicitly, and the contained class corresponds to the parent class, while the external class and the subclass are combined.

2) The composite relationship is determined at runtime, while the inheritance relationship is determined at compilation.

3) combination is a loose coupling relationship between the combination class and the included class, while inheritance is a tight coupling relationship between the parent class and the Child class.

4) when you choose to use a composite relationship, the composite class contains the object of the external class. The composite class can call the methods required by the external class, but use the inheritance relationship, all methods and variables of the parent class are unconditionally inherited by the quilt class, And the subclass cannot be selected.

5) The most important thing is that you can trace back the type when using the inheritance relationship. That is, you can reference the subclass object with the parent variable to realize polymorphism without combining this feature.

6) Note that if you are sure that the method for reusing another class never needs to be changed, you should use a combination, because the combination is just a simple reuse of interfaces containing classes, in addition to reusing the parent class interfaces, inheritance can even overwrite these interfaces and modify the default Implementation of the parent class interfaces. This feature is not available in combination.

7) Logically, a combination mainly reflects a kind of overall and partial thought. For example, a computer class is composed of memory class, CPU class, hard disk class, etc, inheritance represents a type of parent-child relationship that can be traced back. Child classes are also an object of the parent class.

8) the differences between the two are mainly reflected in the abstract stage of the class. when analyzing the relationship between classes, we should determine whether to adopt combination or inheritance.

9) a classic quote from a netizen should tell everyone the difference between inheritance and combination: A combination can be said to be "I invited an old man to work in my house ", the inheritance is "My father helped me work at home ".

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.