Saturday, March 28, 2009

When Multiple Inheritance is bad

We all see and know that Multiple inheritance is one of the most classy features of C++. Something that was sorely missed in Java which did not allow. Well recently I realized that its actually a good thing if we just stick with single inheritance. Why, well the reason is simple.

 Diagram1

So now say you are the creator of all the classes, A,B,C, X and Y.  In that case your best approach would be if it was such that there was single inheritance. In that case you would have several benefits. For example if there was such a case that at some point of time, you need to use some function in B from class Y, you would not be able to do it. A single inheritance would provide you the extra flexibility to do operations on the object C in the base classes.

Well this is debatable that you should always stick to single inheritance or not. My take is that one hat does not fit all. So based on the situation, we need to take a call and do what best solves the problem at hand.