What is the difference between combination and inheritance?

Source: Internet
Author: User

Combination and inheritance are two methods of code reuse in object-oriented systems. A combination is used to create objects of the original class in the new class and reuse the functions of the existing class. Inheritance is one of the main characteristics of object-oriented, which allows designers to define the implementation of a Class Based on the implementation of other classes. Both combination and inheritance allow subobject to be set in the new class. Only combination is explicit, while inheritance is implicit. There is a correspondence between combination and inheritance: The overall class in the combination corresponds to the subclass in the inheritance, and the local class in the combination corresponds to the parent class in the inheritance.

What is the difference between the two? First, analyze an instance. Car indicates the vehicle object, vehicle indicates the vehicle object, and tire indicates the tire object. The relationships between the three classes are shown.


We can see that car is a type of vehicle, so it is an inherited relationship (also known as the "is-a" relationship), and car contains multiple tire, therefore, it is a composite relationship (also known as the "has-a" link ). The implementation method is as follows:

Inheritance

Combination

Class verhicle {

}

Class car extends verhicle {

 

}

Class tire {

}

Class car extends verhicle {

Private tire T = new tire ();

}

Since both inheritance and combination can realize code reuse, how should we choose it in actual use? Generally, the following two principles are observed.

(1) Unless the relationship between the two classes is "is-a", do not use inheritance easily. Do not simply use inheritance for code reuse, too much inheritance will damage the maintainability of the Code. When the parent class is modified, it will affect all subclasses inherited from it, thus increasing the maintenance difficulty and cost of the program.

(2) do not use inheritance only to realize polymorphism, if there is no "is-a" relationship between classes. You can achieve the same purpose by implementing interfaces and combinations. The policy pattern in the design pattern can be a good illustration of this point. The method of using interfaces and combinations has better scalability than the method of using inheritance.

Because Java only supports single inheritance, If You Want To inherit two or more classes at the same time, it cannot be directly implemented in Java. At the same time, in the Java language, if the inheritance is used too much, it will make the content in a class bloated. Therefore, do not use inheritance when using combinations in Java.


From the official website of the new book "Java programmer interview test book"

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.