views:

576

answers:

8

Java doesn't allow multiple inheritance but it allows implementing multiple interfaces. Why?

+1  A: 

Because an interface is just a contract. And a class is actually a container for data.

Snake
+3  A: 

For the same reason C# doesn't allow multiple inheritence but allows you to implement multiple interfaces.

The lesson learned from C++ w/ multiple inheritence was that it lead to more issues than it was worth.

An interface is a contract of things your class has to implement. You don't gain any functionality from the interface. Inheritence allows you to inherit the functionality of a parent class (and in multiple-inheritence, that can get extremely confusing).

Allowing multiple interfaces allows you to use Design Patterns (like Adapter) to solve the same types of issues you can solve using multiple inheritence, but in a much more reliable and predictable manner.

Justin Niessner
C# does not have multiple inheritance precisely because Java does not allow it. It was designed much later than Java. The main problem with multiple inheritance I think was the way people were taught to use it left and right. The concept that delegation in most cases is a much better alternative just was not there in the early and mid-nineties. Hence I remember examples, in textbooks, when Car is a Wheel and a Door and a Windshield, vs. Car contains Wheels, Doors and Windshield. So the single inheritance in Java was a knee jerk reaction to that reality.
Alexander Pogrebnyak
+22  A: 

Because interfaces specify only what the class is doing, not how it is doing it.

The problem with multiple inheritance is that two classes may define different ways of doing the same thing, and the subclass can't choose which one to pick.

Bozho
I used to do C++ and ran into that exact same issue quite a few times. I recently read about Scala having "traits" that to me seem like something in between the "C++" way and the "Java" way of doing things.
Niels Basjes
+10  A: 

Because inheritance is overused even when you can't say "hey, that method looks useful, I'll extend that class as well".

public class MyGodClass extends AppDomainObject, HttpServlet, MouseAdapter, 
             AbstractTableModel, AbstractListModel, AbstractList, AbstractMap, ...
Michael Borgwardt
Priceless! (15ch)
BalusC
+4  A: 

Implementing multiple interfaces is very useful and doesn't cause much problems to language implementers nor programmers. So it is allowed. Multiple inheritance while also useful, can cause serious problems to users (dreaded diamond of death). And most things you do with multiple inheritance can be also done by composition or using inner classes. So multiple inheritance is forbidden as bringing more problems than gains.

Tadeusz Kopec
+12  A: 

One of my college instructors explained it to me this way:

Suppose I have one class, which is a Toaster, and another class, which is NuclearBomb. They both might have a "darkness" setting. They both have an on() method. (One has an off(), the other doesn't.) If I want to create a class that's a subclass of both of these...as you can see, this is a problem that could really blow up in my face here.

So one of the main issues is that if you have two parent classes, they might have different implementations of the same feature — or possibly two different features with the same name, as in my instructor's example. Then you have to deal with deciding which one your subclass is going to use. There are ways of handling this, certainly — C++ does so — but the designers of Java felt that this would make things too complicated.

With an interface, though, you're describing something the class is capable of doing, rather than borrowing another class's method of doing something. Multiple interfaces are much less likely to cause tricky conflicts that need to be resolved than are multiple parent classes.

Syntactic
+1 for that epic quote
NomeN
Thanks, NomeN. The source of the quote was a PhD student named Brendan Burns, who at the time was also the maintainer of the open-source Quake 2 source repository. Go figure.
Syntactic
+1  A: 

Take for example the case where Class A has a getSomething method and class B has a getSomething method and class C extends A and B. What would happen if someone called C.getSomething.

Interfaces basically just specify what methods a implementing class needs to contain. A class that implements multiple interfaces just means that class has to implement the methods from all those interfaces. Whci would not lead to any issues as described above.

John
A: 

There's one more underlying reason for not allowing multiple inheritance. A class defines how an object's memory is supposed to be laid out. All of the properties of a class are arranged sequentially in memory whenever the class is instantiated. If there were multiple inheritance, how would you know which properties to combine, which to keep separate, and in what order to store them?

Easy. You define a separate class and use interfaces.

Erick Robertson