I would like to know what is the problem with handling multiple inheritance in java? that is, I was working on something when I got to the point where I needed to inherit several abstract classes with extends
in Java but I realize that it is not allowed or at least I tried something like extends CLaseA,ClaseB
I have searched for a while and I have not found anything that explains why that.
so can someone help me to know why? Also, since it is not possible to do this , what other alternatives do I have?
Short answer
We can say that there are basically two types of multiple inheritance: Class implementation multiple inheritance and Type implementation multiple inheritance .
The creators of Java explicitly prohibited the former. So any attempt to extend a class from more than one class will result in an error. The main reasons for doing so were:
All this will be explained in more detail in the long answer.
long answer
We all know what it means to inherit . In short, it is receiving something from someone like us and being able to enjoy it.
In OOP inherit would mean that an object receives (inherits) something from another object in order to use it.
The word to express this in Java is
extends
. When a class extends more than one class, this is called multiple inheritance.For example, the class
VehiculoMotor
extends the classesVehiculoDiesel
andVehiculoGasolina
.Now, we can talk about two types of multiple inheritance, according to the Java documentation : implementation multiple inheritance (of classes) and implementation multiple inheritance of types (with interfaces).
1. Multiple Implementation Inheritance (of Classes)
Implementation multiple inheritance is the ability to inherit method definitions from multiple classes.
This type of inheritance is not possible in Java .
Why doesn't Java support multiple inheritance?
The same documentation (link above), explains some reasons:
Naming conflicts and ambiguity. When the compilers of programming languages that support this type of multiple-succession superclass contain methods with the same name, they sometimes cannot determine which member or method to access or call.
Also, a programmer may unintentionally introduce a naming conflict by adding a new method to a superclass.
A typical problem that would occur in implementation multiple inheritance would be the so-called diamond problem .
Let's see an image:
This problem would occur if the classes
VehiculoDiesel
andVehiculoGasolina
had methods with the same name. Let's imagine that both have a methodllenar()
. So far everything perfect. But, since the classVehiculoMotor
extends from these two classes. If we do this:What are we going to fill our
camion
, Diesel or Gasoline? It's a tricky question, isn't it? We could wreck the truck.That is the main reason why implementation multiple inheritance is not possible in Java.
2. Implementation Multiple Inheritance with Types
As of Java 8, thanks to the introduction of default methods, a multiple-inheritance form of implementation can be used, using interfaces. That is, a class can implement (
implements
) more than one interface, which can contain default methods that have the same name. The Java compiler provides some rules for determining which default method a particular class uses.The Java programming language supports multiple type inheritance, which is the ability of a class to implement more than one interface. An object can have multiple types: the type of its own class and the types of all the interfaces that the class implements. This means that if a variable is declared to be the type of an interface, then its value can reference any object that is instantiated from any class that implements the interface. This is discussed in the Using an Interface as a Type section of the Java documentation.
As with implementation multiple inheritance, a class can inherit different implementations of a method defined (as default or static) in the interfaces it extends. In this case, the compiler or the user must decide which one to use.
This solves the diamond problem mentioned above, which was the main reason why multiple inheritance between classes was prevented in the Java language.
Taking the same example from the image, we can make the class
VehiculoMotor
implement two interfacesLlenableGasolina
andLlenableDiesel
, each of them with a default methodllenar()
.Interface
LlenableDiesel
Interface
LlenableGasolina
Having the two interfaces like this, we can create a class
VehiculoMotor
in which the programmer can override the methodllenar()
indicating within it which of the two methods will be called, using the default method of any of the interfaces that the class implements. Thus, if the method corresponding to diesel vehicles is to be called:Instead, to call the method
llenar()
on gasoline vehicles:The output of this code:
Class
VehiculoMotor
It would then be this:
More details
The subject was discussed years ago (in 2002) in JavaWorld .
Among the reasons why Java does not allow multiple inheritance are:
And they say that using multiple interfaces was chosen as a solution:
In that article there are links to other interesting articles and discussions on the subject, if you want to go deeper.
To add to A.Cedano's answer , there are two additional considerations:
Java supports multiple inheritance at the interface level. Example:
Since Java 8, the power of interfaces has been extended by having default methods, that is, an interface can provide the implementation of its methods. This means that an interface/class, by extending/implementing more than one interface, could now fall into the diamond problem. The compiler solves these cases by requiring the concrete class to provide an implementation for the conflicting method. Example:
Departure:
James Gosling ruled it out when creating java. He noted that multiple inheritance did not add great advantages to C++, and that it could be better implemented with the correct use of interfaces.
I really don't see what is the problem? It seems to me that the solution is as simple as having to specify which parent class the method we want to call comes from, only if it is repeated in other parent classes. the development environment of Java, .Net, ruby... and any other object-oriented language should warn the programmer every time this "conflict happens" with a spotlight, a red light or whatever they want and that's it! they've been drowning for decades on something so silly, and it doesn't look like they're going to want to breathe again. multiple inheritance is super necessary and useful.