Java doesn't allow multiple inheritance, but it allows implementing multiple interfaces. Why?
相关问题
- Delete Messages from a Topic in Apache Kafka
- how to define constructor for Python's new Nam
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
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.
* This is a simple answer since I'm a beginner in Java *
Consider there are three classes
X
,Y
andZ
.So we are inheriting like
X extends Y, Z
And bothY
andZ
is having a methodalphabet()
with same return type and arguments. This methodalphabet()
inY
says to display first alphabet and method alphabet inZ
says display last alphabet. So here comes ambiguity whenalphabet()
is called byX
. Whether it says to display first or last alphabet??? So java is not supporting multiple inheritance. In case of Interfaces, considerY
andZ
as interfaces. So both will contain the declaration of methodalphabet()
but not the definition. It won't tell whether to display first alphabet or last alphabet or anything but just will declare a methodalphabet()
. So there is no reason to raise the ambiguity. We can define the method with anything we want inside classX
.So in a word, in Interfaces definition is done after implementation so no confusion.
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.
Since this topic is not close I'll post this answer, I hope this helps someone to understand why java does not allow multiple inheritance.
Consider the following class:
In this case the class Abc does not extends nothing right? Not so fast, this class implicit extends the class Object, base class that allow everything work in java. Everything is an object.
If you try to use the class above you'll see that your IDE allow you to use methods like:
equals(Object o)
,toString()
, etc, but you didn't declare those methods, they came from the base classObject
You could try:
This is fine, because your class will not implicit extends
Object
but will extendsString
because you said it. Consider the following change:Now your class will always return "hello" if you call toString().
Now imagine the following class:
Again class
Flyer
implicit extends Object which has the methodtoString()
, any class will have this method since they all extendsObject
indirectly, so, if you calltoString()
fromBird
, whichtoString()
java would have to use? FromAbc
orFlyer
? This will happen with any class that try to extends two or more classes, to avoid this kind of "method collision" they built the idea of interface, basically you could think them as an abstract class that does not extends Object indirectly. Since they are abstract they will have to be implemented by a class, which is an object (you cannot instanciate an interface alone, they must be implemented by a class), so everything will continue to work fine.To differ classes from interfaces, the keyword implements was reserved just for interfaces.
You could implement any interface you like in the same class since they does not extends anything by default (but you could create a interface that extends another interface, but again, the "father" interface would not extends Object"), so an interface is just an interface and they will not suffer from "methods signature colissions", if they do the compiler will throw a warning to you and you will just have to change the method signature to fix it (signature = method name + params + return type).
Java does not support multiple inheritance , multipath and hybrid inheritance because of ambiguity problem:
multiple inheritance
Because an interface is just a contract. And a class is actually a container for data.