When to use delegation instead of inheritance? [closed]
Asked Answered
I

6

60

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

Inca answered 7/5, 2009 at 1:37 Comment(2)
Good discussion here: #49502Mra
I won't expand on the answers given, however a book I've found really useful with these sorts of OO design issues is Arthur Riel's excellent book Object-Oriented Design Heuristics. davegardner.me.uk/reading/oo-design-heuristics amazon.co.uk/Object-oriented-Design-Heuristics-Arthur-Riel/dp/…Kielty
D
74

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.

Dickie answered 7/5, 2009 at 1:40 Comment(0)
A
32

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.

Additive answered 7/5, 2009 at 1:41 Comment(5)
Inheritance is for modeling "is-a" relationships but also where the sub class extends the functionality of the base class and does nothing to restrict it.Elliellicott
@Chadworthington: Not true; a subclass can override methods and replace them -- essentially a restriction -- without delegating back to the superclass.Additive
@S.Lott: While you can override methods, you have to be careful to override all the methods that expose the behaviour you want to restrict - including some that may not have been written yet. @Chadworthington's advice is safer, particularly when you don't control the superclass.Pantelleria
So basically, it should be easy to determine when you want to delegate - the hard bit is to decide whether to do it via inheritance or not!Trochal
IMO it's an overstatement to claim "they have nothing to do with each other". It's often a difficult question whether to use "is a" rather than "has a" relationships when creating a model. Especially since all "is a" relationships can be modeled as "has a" relationships instead. The Go language doesn't even have classical inheritance relying entirely on delegation for code reuse.Haste
R
4

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.

Rascally answered 7/5, 2009 at 1:47 Comment(0)
O
4

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.
Orgiastic answered 7/5, 2009 at 2:46 Comment(0)
V
1

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.

Vadim answered 25/10, 2015 at 5:33 Comment(0)
H
1

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.

Hellgrammite answered 6/9, 2018 at 9:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.