Multiple inheritance and multiple interface. What is the true difference?

I read over the Internet for the reason why multiple inheritance is not allowed in Java, and the following example was given to illustrate:

class A { public void doSomething() { } } class B { public void doSomething() { } } class C extends A,B { } public static void main(String args) { C c = new C(); c.doSoemthing(); // compiler doesnt know which doSeomthing to call. 

The above example illustrates what we call the diamond problem, where both parent classes have the same method name. when a child class tries to restore it, the compiler gets confused.

My question is, how can an interface solve this problem?

+4
source share
7 answers

This is not to say that multiple inheritance is achieved through interfaces in java

Java support only multiple interface inheritance , and java does not support multiple inheritance .

You should see In mixin inheritance , one class is specifically designed to be used as one of the classes in a multiple inheritance scheme.

http://csis.pace.edu/~bergin/patterns/multipleinheritance.html

+2
source

Let's look at the following code

 interface P { public void doSomething(); } interface Q { public void doSomething(); } class A { public void doSomething() { } } class B { public void doSomething() { } } class C implements P,Q { public void doSomething(){ // implementation } } class D extends A,B { // suppose it is possible } 

now, to use the C object, you have a doSomething() implementation in class C. This is only implemented in class C.

But if you could create a D object and call doSomething , which method should you call? since this method is implemented in both A and B.

The problem with diamonds

actually a real diamon task

 class A { public void doSomething() { } } class B extends A{ public void doSomething() { } } class C extends A{ public void doSomething() { } } class D extends B,C { // suppose it is possible // no implementation of doSomething. } 

enter image description here

it is called a diamond because of this diamond shape. Here if you want to do the following

 D d = new D(); d.doSomething(); // which method should be called now???? 

From wikipedia here is a good real-time example

For example, in the context of GUI software development, the Button class can inherit from both the Rectangle (for appearance) and Clickable (for handling functions / input) and the Rectangle and Clickable classes both inherit from the Object class. Now, if the equal method is called for the Button object, and there is no such method in the Button class, but there is an overridden equal method in both Rectangle and Clickable, which method should be called in the end?

+2
source

An interface does not implement the doSomething() method, so you cannot call an interface method. An interface is a simple signature of which methods to implement in the class of executive (implementing) ones. You would apply doSomething() in your C class, and that would be the method that you call when B.doSomething() or A.doSomething() called.

In the case of extending two claasses with two doSomething() methods, they can have different implementations , and you do not know which one is being called. See this example:

 class A { public void doSomething() { System.out.println("A"); } } class B { public void doSomething() { System.out.println("B"); } } class C extends A & B { //if this would be an option } public static void main(String args) { C c = new C(); c.doSoemthing(); //Print "A" or "B" ??? } 

Conclusion This is a thing of realization. Interfaces do not offer any implementation for any method, so it is safe to inherit from interfaces that have the same method signatures.

+1
source

Multiple inheritances can inherit element data from many classes, as well as all their functions. Multiple interfaces can only inherit function prototypes, and they must be implemented by a child class.

0
source

The interface does not implement the method, and therefore both of them will be combined into the same method. In fact, your promises object implements a method called doSomething, but not tied to a specific one of the interfaces (serves both)

0
source

Both classes provide code with which the JVM can jump to a call when called. This is the ambiguity. The same problem with attributes, the compiler may have two attributes with the same name, which will be considered, which will give a similar ambiguity.

The interface will not provide this code. Consequently, there will be no conflict.

Other languages ​​that support multiple inheritance force the compiler to forbid these ambiguities when they arise. And their decision must be made ad hoc. I.e.

 class C{ public void doSomething(){ // Call (this inferred) B.doSomething(); // leave A.doSomething() alone. } } 
0
source

The answer lies with your very question. In case the compiler interface is not confused, since there is no implementation in your interface. This is your specific class that will provide the real implementation. There is no confusion.

0
source

Source: https://habr.com/ru/post/1483383/


All Articles