Calling same method name from two different interface - Java

Taewan picture Taewan · Nov 13, 2013 · Viewed 7.3k times · Source

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?

Answer

Akira picture Akira · Nov 13, 2013

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.