Why multiple inheritance is not allowed in Java

For long time I had a question “why Sun introduced Interface concept instead of C++ style of multiple inheritance?”. I did googling but many articles and forums talks about difference between abstract class and Interface not why Interface concept required in Java. After extensive search and analysis I came to know the reason behind the Interface concept in Java.

In C++ multiple inheritance is possible, virtual keyword is been used to define virtual function. Whereas in Java multiple inheritance is not possible however using Interface concept the same class can represent different structure.

Lets take an example in C++ where two parent class contains same virtual function.

Example:

class BaseCol
{
public:
virtual void a() { cout << “BaseCol::a” << endl; }
virtual void b() { cout << “BaseCol::b” << endl; }
};

class ChildCol1 : public BaseCol
{
public:
virtual void a() { cout << “ChildCol1::a” << endl; }
virtual void b() { cout << “ChildCol1::b” << endl; }
};

class ChildCol2 : public BaseCol
{
public:
virtual void a() { cout << “ChildCol2::a” << endl; }
virtual void b() { cout << “ChildCol2::b” << endl; }
};

class Row : public ChildCol1, public ChildCol2
{
public:
// i want to overide the b function of ChildCol1 only !!
void b() { cout << “Row1::b” << endl; }
};

While executing the above code at runtime ambiguity error will occur since the implementation has conflicting methods. This problem is called “Diamond inheritance problem“.

To avoid this complexity Java provides single inheritance with interface concept. Since interface cannot have method implementation, the problem is therefore avoided since there is always only one implementation to a specific method and hence no ambiguity will arise.

Every concept/ design is to solve specific problems not to create magics.

Delicious Digg reddit Facebook StumbleUpon

Related Articles

Advertisements

9 Responses to Why multiple inheritance is not allowed in Java

  1. William (AKA Rosencrantz) says:

    Hi Venkat Sadasivam, (Just in case you don’t get my message on Wikianswers)

    I read over your answer on your site, and its great! You know you can add your answer to the existing one, since not many read the discussion page, I would hate for people to skim over your amazing answer. Also, I would like to invite you to help contribute more to this section and Wikianswers as a whole since your answers (on your site) are amazing.

    Sincerely,
    Rosencrantz
    Java Section Supervisor

  2. Hari says:

    Though many people would have come across this Diamond inheritance problem, few would have related that to the interface concept in Java. Good that you put the raison d’etre, Venkat. You don’t know how much your articles can make people think!

    I would just like to add a few points here:

    1) Consider there is no super class ‘A’ [which forms of the top of ‘Diamond’], and we have just classes B and C. Say, we have 2 identical methods in both ‘B’ and ‘C’, even in that case, a class ‘D’ which inherits both ‘B’ and ‘C’ will have problem choosing the appropriate method implementation to run [assuming that D doesn’t override the method at all].

    The same scenario could be considered in Java, where 2 different interfaces can have one method each, with the same signature. A class inheriting both the interfaces, however, needn’t worry and provide implementation to the method only once.

    But,think of a slight difference to the scenario above, where one interface ‘A’ has a method foo() which returns ‘int’ and another interface B which also has a method definition foo() which returns nothing[void]. Now a class ‘C’ cannot inherit both the interfaces together if it does, then it has to provide implemenation for both versions of ‘foo()’. If the class goes on to do that, the Java compiler doesn’t allow because, Java doesn’t support method overloading based on return type. Related to the same, I found this article interesting: http://today.java.net/pub/a/today/2008/07/31/return-type-based-method-overloading.html

  3. Asit says:

    Before searching the answer about the avoidance of multiple inheritance in java I was thinking about the diamond problem occurs in c++ .Thanks you gave the same answer.

  4. gopal says:

    Thankyou for giving good reason behind that”why interface concept introduced in java?”…………………..

  5. Venkat says:

    Hey Venkat…
    Got it .. Thanks ..
    – Venkat.

  6. Anonymous says:

    I don’t see why there has to be a conflict problem. Why couldn’t they just say that the base class listed first has the priority?

  7. Suresh Babu says:

    Hi venkat,

    You have given a great thought to those who are still thinking How multiple inheritance is satisfield in java.Its amazing and you made a great help to java developers.

  8. Anonymous says:

    thanx venkat

  9. M says:

    “I don’t see why there has to be a conflict problem. Why couldn’t they just say that the base class listed first has the priority?”

    Probably because you might have a case where you want one to B to override C for one method, but you want C to override B for another method.

    However, I think you could force the programmer to override and then to call one explicitly: super.parentA.method().

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: