Question about multiple inheritance
Why does javanot support multiple inheritance, but also give you the ability to use interfaces?
I've done a quick search on here which turned up the same thing as the books on java I've read - they tell methat java doesn't support multiple inheritance, and that it supports interfaces, but not why.
And from what I can see, the between multiple inheritance and single inheritance + interfaces make them seem almost equivalent, especially when you consider abstract classes. So why did the java designers make this decision?
Edit: Just to say I've never programmed in an OO language that supports multiple inheritance, so I've never had to deal with it. Also, single inheritance has never crippled any of my designs (not that there have beenthat many), I'm not whingeing, just asking.
Message was edited by:
Dross
[880 byte] By [
Drossa] at [2007-11-26 18:54:50]

The language designers decided it added too much complexity and confusion without really being very useful.At least that's what I seem to recall Gosling saying in a white paper a decade ago. Geez has it really been that long?
> Why does java not support multiple
> inheritance, but also give you the ability to use
> interfaces?
It does support MI, just not MI of Implementation.
> why.
class Beasty { }
class Horse extend Beasty {
public void gallop() { System.out.println( "horse" ); }
}
class Donkey extend Beasty {
public void gallop() { System.out.println( "donkey" ); }
}
class Mule extend House, Donkey {
}
Mule mule = new Mule();
mule.gallop();
what would this print out.
MI of implementation makes life harder, but adds very little to the party. So why add it?
mlka at 2007-7-9 20:32:24 >

Mutliple inheritance tends to cause more problems than it solves in nearly every case.
Conflicting implementations are namely just one of the major concerns...
What if class Square inherits from both Rectangle and Shape, and Rectangle and Shape both have an equals() method? Which one does it inherit?
Interfaces don't provide that implementation so they allow any one class to implement any number of interfaces.
It allows developers to create a contract that all implementers of that interface must follow.
Abstract classes may implement interfaces as well but all in all they are still a class and you can only extend one.
One simple example where complexities arise is when your class inherits from different classes that provide different implementations for a method with the same signature.
If you're using interfaces and single inheritance, there is always at most one single implementation for every method (signature).
Ah, of course! I was initially thinking that the issue pointed out above might still arise if a class' superclass and an interface it implements have methods with the same signature, but of course your class would have to explicitly specify what to do when calling that method.
Makes sense now, thank you all very much.
> One simple example where complexities arise is when
> your class inherits from different classes that
> provide different implementations for a method with
> the same signature.
>
> If you're using interfaces and single inheritance,
> there is always at most one single implementation for
> every method (signature).
Name clashes of this kind is rather a design problem that won't go away with the Java inheritance model. You still have the design problem of dealing with the two clashing methods regardless of whether they're inherited with or without implementation.
> Why does java not support multiple
> inheritance, but also give you the ability to use
> interfaces?
The Java inheritance model is easier on the compiler than full MI of implementation. The Java inheritance model makes sure there's just one implementation to consider regardless of how many method name clashes there are. Case closed for the compiler but the problem is still there for you as designer because you probably don't want the same implementation for all inherited methods which happen to have been given the same name.
> class Mule extend House, Donkey {I've always wondered what would happen if you bred a House to a Donkey. Now I know!
> Mule mule = new Mule();
> mule.gallop();
>
> what would this print out.
Nothing. The compiler would ask you to resolve the conflict (using some syntax Java would have if it had supported MI of implementation).
Note that the problem you address doesn't go away with the Java inheritance model,
class Mule implements House, Donkey {}
The compiler would gladly accept one common implementation for the two gallop() methods, but that's probably not what you want.
> Why does java not support multiple inheritance, but also give you > the ability to use interfaces?One answer to that question is to separate (in your mind) interfaces from inheritance. http://www.objectmentor.com/resources/articles/isp.pdf