Combination is better than inheritance

Source: Internet
Author: User

Combination is better than inheritance

 

 

 

Why is combination better than inheritance?

This is a typical Design Pattern problem. The First chapter of Head First Design Pattern seems to have been mentioned, and I have forgotten it before. Next, I will use the answer with a higher score on stackoverflow. I think this is easier to understand.

Differences

Think of composition as a has a relationship. A car has an engine, a person has a name, etc.

Think of inheritance as an is a relationship. A car is a vehicle, a person is a mammal, etc.

Although the above two statements did not explain why the combination is better, the difference between the two statements is justified.

In simple translation, the combination is applicable to the relational Class, that is, Class B only needs A function of Class.

If B extends A is A relational database, B is A relational database.

Reason why a combination is better than inheritance
  1. Java does not support multiple inheritanceIf you need multiple functions, you cannot use inheritance because only one class can be inherited. Composition offers better testability of a class than Inheritance. if one class is composed of another class, you can easily create Mock Object representing composed class for sake of testing. inheritance doesn' t provide this luxury. in order to test derived class, you must need its super class. since unit testing is one of the most important thing to consider during software development, especially in test driven development, composition wins over inheritance. jsonobject oriented design patterns mentioned by Gang of Four in there timeless classic Design Patterns: Elements of Reusable object-Oriented Software, favors Composition over Inheritance. classical examples of this is Strategy design pattern, where composition and delegation is used to change Context's behavior, without touching context code. since Context uses composition to hold strategy, instead of getting it via inheritance, it's easy to provide a new Strategy implementation at runtime. another good example of using composition over inheritance is Decorator design pattern. in Decorator pattern, we don't extend any class to add additional functionality, instead we keep an instance of the class we are decorating and delegates original task to that class after doing decoration. this is one of the biggest proof choosing composition over inheritance, since these design patterns are well tried and tested in different scenarios and withstand test of time, keeping there head high.More flexible combinationYou can refer to the Comparator interface I mentioned earlier. For example, we need a tool class that inherits the Comparator interface. In this case, we should select a combination method instead of inheriting this tool class, if you choose to inherit, you have only one comparative choice during the runtime, And we can define multiple tool classes. If you use a combination, we can select a tool class during the runtime.

    Reference

    1. Prefer composition over inheritance? Why Favor Composition over Inheritance in Java and Object Oriented Programming

      23:44:27

      Brave, Happy, Thanksgiving!


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.