TravisSwicegood.com

11 October

Why class inheritance sucks

Bernard Sumption has a post titled Inheritance is evil, and must be destroyed: part 1:

All of the pain caused by inheritance can be traced back to the fact that inheritance forces 'is-a' rather than 'has-a' relationships. If class R2Unit extends Droid, then a R2Unit is-a Droid. If class Jedi contains an instance variable of type Lightsabre, then a Jedi has-a Lightsabre.

The difference between is-a and has-a relationships is well known and a fundamental part of OOAD, but what is less well known is that almost every is-a relationship would be better off re-articulated as a has-a relationship.

I couldn't have said it better myself. Remember that next time you type extends and re-think the implementation before taking the easy route. Unless, of course, you just want to be like Java ;-)

8 comments

What I find amusing is that he doesn't mention the canonic term for this alternative to inheritance: composition
True... but I'd prefer him to be preaching the true path and not calling it by its name than the alternative :-)

Seems like he's got some cases where interfaces (or, yes, composition) would be a better solution.



But I have cases where, say, if I'm building a CMS, I have a ContentObject class that has generalized methods to determine where it's been published, etc. If I have two types of content, Document and Event, I fail to see why extending ContentObject for each one is bad.



Now if I have another type, Author, and each Document needs an Author, it's a has-a relationship. Extending Author to get the author's name as part of the Document would be wrong. Instead it should be $document->author->getName();



But in the first case, if I need to implement a publishTo($page_id) method, I'd rather do it once in ContentObject, so I can change that implementation in one place. Am I missing something that makes that a bad idea?

I don't think the point of the original article was that inheritance should be avoided at all costs. I think the point is that you use inheritance when it is appropriate an composition when it is appropriate. With the addendum that several people don't realize that many times composition is more appropriate.

Inheritance in languages like php is very limited. You can only inherit from one object. (Which I think is perfectly fine.) Whereas you can use composition, decorator and other similar patterns many, many times.
Hey Sandy, I started writing a response, but I got so long winded I ended up with another post. Hope that helps explain it a bit better. Your example is actually a perfect real world use case.
Mike: Didn't mean to imply that extends is always evil... just most of the time. ;-)
Isn't this guy just discovering the usefulness of AOP?
Lukas - basically. I think its fair to say that he's re-articulating something that's already known within the context of his understanding. I'll give anybody that wiggle-room if they can work Jedi objects in to their discussion. :-)

At any rate, it's the same as their being many paths to the top of a mountain: as long as they all lead to the top, you get to the same place.

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)