When to use delegation instead of inheritance?

OopInheritanceDelegation

Oop Problem Overview


Could someone please explain when would I want to use delegation instead of inheritance?

Oop Solutions


Solution 1 - Oop

When you want to "copy"/Expose the base class' API, you use inheritance. When you only want to "copy" functionality, use delegation.

One example of this: You want to create a Stack out of a List. Stack only has pop, push and peek. You shouldn't use inheritance given that you don't want push_back, push_front, removeAt, et al.-kind of functionality in a Stack.

Solution 2 - Oop

They have nothing to do with each other. Delegation is a behavior. Inheritance is a model technique.

Inheritance is for modeling "is-a". A computer "is-a" electronic system.

Delegation is how methods provide results. Sometimes one object will delegate work to another object. Delegation can be via any relationship -- you can delegate to a superclass, to a member of a composite or aggregate, or any relationship.

Solution 3 - Oop

You may use delegation to multiple internal class instances to simplify their functionality into a common grouping. If your language doesn't implement multiple inheritance for instance you may inherit from one of the bases and wrap the other, delegating the functionality you want to expose to the underlying implementation. Inheritance also ties your class into the hierarchy of classes you are inheriting from where as with delegation you may keep your place in your own hierarchy and delegate calls to another.

Solution 4 - Oop

Assume your class is called B and the derived/delegated to class is called A then

Here are some examples when inheritance or delegation are being used:
If

  • you want to express relationship (is-a) then you want to use inheritance.
  • you want to be able to pass your class to an existing API expecting A's then you need to use inheritance.
  • you want to enhance A, but A is final and can no further be subclassed then you need to use composition and delegation.

Solution 5 - Oop

In my opinion, delegation can be called when it's time to use and inheritance is embedded ever.

Take the in_threads gem for example, it use InThreads delegator to implement the threads for any Ruby Enumerable module. With this, only call in_threads methods on a array, it will run in threads.

Solution 6 - Oop

I agree with @Anzurio. Just to explain it in simple words:

> Use inheritance only when you feel like the new class is a natural extension of the existing class. Prefer to use composition/delegation for all other purposes like using a specific method, etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCugaView Question on Stackoverflow
Solution 1 - OopAnzurioView Answer on Stackoverflow
Solution 2 - OopS.LottView Answer on Stackoverflow
Solution 3 - OopAdam MarkowitzView Answer on Stackoverflow
Solution 4 - OoplotharView Answer on Stackoverflow
Solution 5 - OopAstonView Answer on Stackoverflow
Solution 6 - Oopabhay anandView Answer on Stackoverflow