Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling same method name from two different interface - Java

Java doesn't allow the multiple inheritance to protect diamond problem. It uses interface to take care of this problem.

Then the case of using interface, let's say

interface A{
run();
}

interface B{
run();
}

class C implements A, B{
run() {}   //Which interface we are using?
}

When we call the method run() in the class C, how can we determine which interface we are using?

like image 449
Taewan Avatar asked Nov 13 '13 16:11

Taewan


1 Answers

You don´t. And it does not matter, since the implementation is not on the interface but on the class. So the implementation is unique. No ambiguity.

What does matter is if each declaration wants to have a different return type:

interface A{
    void run();
}

interface B{
    String run();
}

class C implements A, B{
    ???? run() {}
}

This is the only way you can have problems with multiple interfaces in Java.

like image 60
Akira Avatar answered Oct 29 '22 14:10

Akira