# [Inheritance? Why Not?](/content/articles/inheritance-why-not/index.html)

[Mike Charlton](http://mikekchar.github.com/portfolio/)
11 October 2013

- [Share on Facebook](http://www.facebook.com/sharer/sharer.php?u=http://sharktank.co.uk/articles/inheritance-why-not/ "Share on Facebook")
- [Share on Twitter](https://twitter.com/intent/tweet?url=http://sharktank.co.uk/articles/inheritance-why-not/&text=Inheritance?%20Why%20Not? "Share on Twitter")
- [Share on Google+](https://plus.google.com/share?url=http://sharktank.co.uk/articles/inheritance-why-not/ "Share on Google+")
- [Share on LinkedIn](http://www.linkedin.com/shareArticle?mini=true&url=http://sharktank.co.uk/articles/inheritance-why-not/ "Share on LinkedIn")

As it often does, my day started with a quick peek at the open pull requests.
We had two similar classes, one which had a parent class and one which
didn’t. The discussion centered around whether or not the second class should
also inherit from the parent class since it had very similar functionality.
In the end we decided that there should be no parent class. After all, these
days the mantra is, “Avoid inheritance and embrace composition”.

“But why?”

You have to understand that this was a Monday morning and the object model
was obviously improved by our actions. One more code smell gone. On
to bigger and better problems!

“Why should we avoid inheritance?”

Sometimes I think my hearing needs checking. Often I find that when I am
particularly chuffed with my efforts and trying to remember where I hid
the good coffee beans I get this annoying voice in my head. Best just
to ignore it.

“What makes composition better?”

This time it was my IM pinging at me. Dawid was kindly reminding me that
I should really think about this problem. I guess there is no getting around it.
“Just say that [Erich Gamma](http://en.wikipedia.org/wiki/Erich_Gamma) says
[composition is better than inheritance](http://www.artima.com/lejava/articles/designprinciples4.html)
and get back to looking for the coffee”, I thought. Erich Gamma knows way more
about OOD than I do, so it must be right.

“So exactly how do you use [Strategy Pattern](http://en.wikipedia.org/wiki/Strategy_pattern)
in place of inheritance?”

This inner voice was seriously disrupting my carefully cultivated Winnie-the-Pooh,
Zen-like state of mind. Oh well, it’s not like I would ever remember where I hid
the coffee. Everyone knows that it’s twice as hard to find coffee than it is to
hide it. I was quite clever in hiding it, so there is no chance I will ever find it
again.

Of course I know the answer to this question. How could I not? I’ve spent decades
writing object models, both good and bad. I’ve made every mistake and then made
them again just to be sure they were actually mistakes. I know from all this
experience that it always works out better if you avoid inheritance unless you
need it. And this is how you have to think:

… Maybe I should just go back to looking for the coffee.

Knowing something to be true and being able to say why it is true are different things.
As humbling as it may be, sometimes knowing something to be true and it actually
being true are different things. Is it really true in this case? Erich Gamma said so.
So did a lot of other people. But to be honest, I have seen precious few good
descriptions of this issue. Pretty much all I see on the internet are a list
of good natured assertions. Isn’t that good enough? After all, Erich Gamma and
everybody on the internet can’t be wrong!

Well, I finally spotted
[this brilliant explanation](http://berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/)
by [Bernie Sumption](http://berniesumption.com/).
For those of you who tend to skim past the links, let me tantalize you with the
fact this article includes examples with both Star Wars and hippos.

## TL;DR

It is often much more difficult to understand when to use inheritance than it
might first appear. When I first replied to Dawid’s innocent question, I
spouted the normal nonsense of:

“Use inheritance when you have a clear IS-A relationship and where you do not
invalidate the [Liskov Substitution Principle](http://en.wikipedia.org/wiki/Liskov_substitution_principle).”
The problem is that even when you think you have a clear cut IS-A relationship,
it might still be better to implement it using something like the strategy
pattern.

The classic example of an IS-A relationship is the decomposition of employees.
One could argue that an employee is a person and workers and managers are
employees. But you could also look at it another way. A person has a job.
The job has a role. What makes someone a manager is the kind of
role they have. Should we use inheritance for the role, with worker and
manager roles? Or should the role have worker and management responsibilities?

At some point you start to think that you can replace all inheritance in
a system with composition. Since you have more flexibility with composition
it seems to make sense to always use composition. Are there times when you
should use inheritance?

I think the answer is “yes” and to see why it is instructive to look at the
[Composite Pattern](http://en.wikipedia.org/wiki/Composite_pattern).
In this pattern, everything is a component. There are two kinds of components:
a leaf component, and a composite. The composite is made up of components.
Component is the base class, while leaf and composite are the child classes.

The important thing to consider is that the composite pattern does not
use the base class in order to share code. Instead it is there to share
the interface. Presumably, we have a tree structure and we want to
iterate over it using the interface on component. Other design patterns
such as the [Bridge Pattern](http://en.wikipedia.org/wiki/Bridge_pattern)
use inheritance in the same way.

When you have a clear IS-A relationship _and_ you want to use all
the objects through the base class’s interface, then inheritance can
be a good way to go. It is certainly not a tool that I would throw out
of the tool chest lightly. However, if you want to deal with the derived
classes through their own interfaces, it often makes much more sense to
use composition.

[See all articles](/content/articles/index.html)

Disqus Comments

We were unable to load Disqus. If you are a moderator please see our [troubleshooting guide](https://docs.disqus.com/help/83/).

G

Start the discussion…

Comment

###### Log in with

###### or sign up with Disqus  or pick a name

### Disqus is a discussion network

- Don't be a jerk or do anything illegal. Everything is easier that way.

[Read full terms and conditions](https://docs.disqus.com/kb/terms-and-policies/)

This comment platform is hosted by Disqus, Inc. I authorize Disqus and its affiliates to:

- Use, sell, and share my information to enable me to use its comment services and for marketing purposes, including cross-context behavioral advertising, as described in our [Terms of Service](https://help.disqus.com/customer/portal/articles/466260-terms-of-service) and [Privacy Policy](https://disqus.com/privacy-policy), including supplementing that information with other data about me, such as my browsing and location data.
- Contact me or enable others to contact me by email with offers for goods or services
- Process any sensitive personal information that I submit in a comment. See our [Privacy Policy](https://disqus.com/privacy-policy) for more information

Acknowledge I am 18 or older

I'd rather post as a guest

- [Favorite this discussion](https://disqus.com/embed/comments/?base=default&f=sharktanklabs&t_u=https%3A%2F%2Fsharktank.co.uk%2Farticles%2Finheritance-why-not%2F&t_d=Sharktank%20Labs%3A%20Inheritance%3F%20Why%20Not%3F&t_t=Sharktank%20Labs%3A%20Inheritance%3F%20Why%20Not%3F&s_o=default# "Favorite this discussion")

- ## Discussion Favorited!

Favoriting means this is a discussion worth sharing. It gets shared to your followers' Disqus feeds, and gives the creator kudos!

[Find More Discussions](https://disqus.com/home/?utm_source=disqus_embed&utm_content=recommend_btn)

[Share](https://disqus.com/embed/comments/?base=default&f=sharktanklabs&t_u=https%3A%2F%2Fsharktank.co.uk%2Farticles%2Finheritance-why-not%2F&t_d=Sharktank%20Labs%3A%20Inheritance%3F%20Why%20Not%3F&t_t=Sharktank%20Labs%3A%20Inheritance%3F%20Why%20Not%3F&s_o=default#)

- Tweet this discussion
  - Share this discussion on Facebook
  - Share this discussion via email
  - Copy link to discussion

- [Best](https://disqus.com/embed/comments/?base=default&f=sharktanklabs&t_u=https%3A%2F%2Fsharktank.co.uk%2Farticles%2Finheritance-why-not%2F&t_d=Sharktank%20Labs%3A%20Inheritance%3F%20Why%20Not%3F&t_t=Sharktank%20Labs%3A%20Inheritance%3F%20Why%20Not%3F&s_o=default#)
  - [Newest](https://disqus.com/embed/comments/?base=default&f=sharktanklabs&t_u=https%3A%2F%2Fsharktank.co.uk%2Farticles%2Finheritance-why-not%2F&t_d=Sharktank%20Labs%3A%20Inheritance%3F%20Why%20Not%3F&t_t=Sharktank%20Labs%3A%20Inheritance%3F%20Why%20Not%3F&s_o=default#)
  - [Oldest](https://disqus.com/embed/comments/?base=default&f=sharktanklabs&t_u=https%3A%2F%2Fsharktank.co.uk%2Farticles%2Finheritance-why-not%2F&t_d=Sharktank%20Labs%3A%20Inheritance%3F%20Why%20Not%3F&t_t=Sharktank%20Labs%3A%20Inheritance%3F%20Why%20Not%3F&s_o=default#)

Be the first to comment.

[Load more comments](https://disqus.com/embed/comments/?base=default&f=sharktanklabs&t_u=https%3A%2F%2Fsharktank.co.uk%2Farticles%2Finheritance-why-not%2F&t_d=Sharktank%20Labs%3A%20Inheritance%3F%20Why%20Not%3F&t_t=Sharktank%20Labs%3A%20Inheritance%3F%20Why%20Not%3F&s_o=default#)

live.rezync.com

# live.rezync.com is blocked

This page has been blocked by an extension

- Try disabling your extensions.

ERR\_BLOCKED\_BY\_CLIENT

Reload

This page has been blocked by an extension

pippio.com

# pippio.com is blocked

This page has been blocked by an extension

- Try disabling your extensions.

ERR\_BLOCKED\_BY\_CLIENT

Reload

This page has been blocked by an extension
