Most people prefer compose to inherit,
but what's the significant benefit for doing so?
Most people prefer compose to inherit,
but what's the significant benefit for doing so?
In languages that do not support multiple inheritance, you don't 'burn' your one chance to derive from another class(type).
Also, it can be a more appropriate modelling of the problem domain.
because the new class does not fully qualify as something the can be put under the 'inheritance' hierarchy for that particular 'base class'...
significant benefits:
the option of extending another class is 'always' available
you don't get tied down to the implementation and definition of the base class
Inheritance is a good tool for the right situation, but is often overused.
By preferring composition to inheritance, you get a way to swap out implementations on the fly. For example, you can compose a class by giving it an interface that provides a behavior. If that class needs 2 different implementations for different situations then you have that freedom. If you had used inheritance, however, then you would still have the behavior of your super class and would have to override it in a ton of spots. Basically, composition is a far more flexible model for creating objects.
This article should give you a complete answer: Design Principles from Design Patterns: Composition versus inheritance
I quote a straight answer from Erich Gamma here:
I still think it's true even after ten years. Inheritance is a cool way to change behavior. But we know that it's brittle, because the subclass can easily make assumptions about the context in which a method it overrides is getting called. There's a tight coupling between the base class and the subclass, because of the implicit context in which the subclass code I plug in will be called. Composition has a nicer property. The coupling is reduced by just having some smaller things you plug into something bigger, and the bigger object just calls the smaller object back. From an API point of view defining that a method can be overridden is a stronger commitment than defining that a method can be called.
In a subclass you can make assumptions about the internal state of the superclass when the method you override is getting called. When you just plug in some behavior, then it's simpler. That's why you should favor composition. A common misunderstanding is that composition doesn't use inheritance at all. Composition is using inheritance, but typically you just implement a small interface and you do not inherit from a big class. The Java listener idiom is a good example for composition. With listeners you implement a listener interface or inherit from what is called an adapter. You create a listener object and register it with a Button widget, for example. There is no need to subclass Button to react to events.
Several design patterns use compsition of an interface for which an inherited implementation is used. So composition doesn't rule out inheritance.
But using inheritance for everything tends to lead to multi-inheritance, big class hierarchies and lots of exceptions.