Inheritance Sucks (most of the time)!!!

Gattaca is one of my favourite movies. I see it as a powerful social commentary on discrimination and the will to overcome all odds. The message of the movie is completely unrelated to  the agenda of this blog post. Let me come to the point.  The film talks about a dystopian society in the future where kids are manufactured with all the ideal features from the parents’ genes. All these kids are ‘conceived’  in the lab and do not INHERIT any undesirable features from their parents. Yes Inheritance means that you inherit some or all features from your parents.

In the object oriented world, it is slightly different; the difference being that a child class inherits all the features from a parent class (unless it chooses to override it). Holding that thought, it is always said that “Prefer composition over inheritance”. Most people take the rule as an axiom without seeing the reason. Some give the answer that it makes your code easily extensible  and testable. Yes, but why does it make your code extensible and testable? Most people really don’t ask this question. They are happy with their code being extensible and testable. Even I didn’t go deeper into it (Until today that is).

Today I was asked to help a few folks at work with writing a custom control in Microsoft Win Forms. They wanted a table with multiple radio buttons embedded in the cells. Me being a web developer initially thought that it was going to be a cake walk. Its been one whole day and I am still on square one. Google helped me initially; it gave me an MSDN link It was just an example (a really bad one at that). I downloaded the code and presto I had a working gird with radio buttons embedded in the cells. There was a slight problem though, the radio buttons were one below the other (the client wanted them horizontally aligned). I tweaked the code a little (made y into x …. hack … hack… hack).The layout was there but only one radio button was selectable. I looked at the code more; there was a custom control which extended the standard combo box control and guess what a combo box control’s width stays the same. The first radio button was covering the others. There are 2 things I learnt today,

1. HTML rocks

2. Inheritance sucks

Inheritance sucks, not every time, it sucks when you inherit the stuff which you don’t want. It just forces unwanted stuff on you. It’s like inheriting debts from a dead person. That is the precise reason why they say prefer composition over inheritance. If you use inheritance, you get behaviour that you want, but most importantly you also get the behaviour which you don’t want. Composition gives you a choice. Having said that, if you know what you are doing and want all the behaviour of a parent in a child class and its future progeny inheritance is a powerful tool and  help you structure your design better. But as with all powerful things in the world, it misused more than it is used rightly. I expected better from Microsoft.



Leave a Reply

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

You are commenting using your 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