Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Azure, PHP, OpenXML, VSTS, ...

About the author

Maarten Balliauw is an MVP ASP.NET and is currently employed as .NET Software Engineer at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP.
More about me More about me
Send mail E-mail me


Microsoft Most Valuable Professional - MVP - ASP.NET

Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn RealDolmen - Rock-solid passion for ICT
I'm a speaker at TechDays Belgium and TechDays Finland

Search

Latest Twitter

    Follow me on Twitter...

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2010

    Inheritance is evil!

    Read this on Bernie's blog:

    "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 suggest you read the full story, as it's very interesting! Bottom line is that you should be careful using OO inheritance, and use the Strategy pattern instead.


    Comments

    Sam Goutsmit Belgium |

    Friday, October 12, 2007 1:02 PM

    Sam Goutsmit

    Which just goes to prove that inheritance is a tool like any others, ie it can be used well or poorly.  Strategy Pattern is indeed a good alternative in his examples.

    However, to draw a general "inheritance is evil" conclusion from that, is just a bit over-the-top.  But I understand that a blog entry reads better when it has a catchy title Smile

    maartenba Belgium |

    Friday, October 12, 2007 2:07 PM

    maartenba

    Catchy blog titles are important tools too Wink

    But you are right in your opinion. You should not stop using inheritance, as it is a powerful tool. Just remember to refactor some things into a Strategy pattern if you come across an issue with Jedi's...

    Comments are closed