Why use inheritance?

DISCLAIMER: if you are actually hoping that this post will help you answer this question, please stop. Go to some authoritative source to find the official answer to this question. I don’t quite agree with it, but who am I?

The other day I was preparing to interview an interesting candidate. This candidate had had other interview events before, so I was reading through them, when I was shocked by one note. But before I write this note, I have to observe that the two protagonists of this exchange were “experienced” Java programmers (2+ years of experience):

Interviewer: Why use inheritance?
Candidate: mumble… mumble… Because it reduces repetition between classes… mumble… mumble
Interviewer: Good! Let’s move on…

And he wasn’t kidding about the “good”. This is not an exactly transcript of the interview, or really the exact transcript of the approximate transcript of the interview (the interviewer notes). But it really worried me that at this day there is still people with more than a few years of programming experience that still think that class inheritance is just a mechanism to reduce code repetition. Especially when we are talking about a language that does not support multiple inheritance (I’m not claiming that this is right when you have multiple inheritance, but it’s even more wrong when you don’t).

It’s one of my pet peeves when people start creating very deep class hierarchies so that they can hang their “utility” methods throughout their classes without having to have explicit references to a utility class, or without having to do static imports (which I think are extremely confusing sometimes). It annoys me because I have seen people shooting themselves in the foot so many times because suddenly they need to have a real inheritance with a class they don’t have control over and now they are stuck because they can’t have multiple inheritance in Java.

I tend to be very strict about when I use inheritance. If I’m in doubt that I can say that A is-a B, even thought it will save a few characters here and there if I use inheritance and it’s very unlikely that A will diverge enough to require another parent, I just end up using containment instead of inheritance. Yes, it’s more verbose, but will keep your code sane in the future. And navigating through contained classes is generally easier than inherited methods, because if you don’t have CTRL-click on the IDE to help you figure out where the implementation of the method you are calling is, it does take much more time to find it. Not only it is time consuming, but you could be getting it wrong by missing and override somewhere in the class hierarchy.

So, why use inheritance? Polymorphism is one of the answers, although not all OO languages support it. The other answer is to improve understandability of your classes. If two classes have part of their state stored in a common parent, it should mean that that part of the state is semantically compatible (and I can write a long critique about the fact that sometimes this is not true and it drives me crazy). Containment does not generally provide this same compatibility, because things can be contained in different contexts. Thus it becomes harder for people that are analyzing the code to really tell how to interact with a set of classes.


0 Responses to “Why use inheritance?”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

RSS My FriendFeed RSS

  • An error has occurred; the feed is probably down. Try again later.

%d bloggers like this: