Calling the same method name from two different interfaces - Java

Java does not allow multiple inheritance to protect the problem with diamonds. It uses an interface to solve this problem.

Then the case of using an 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 run() method in class C , how can we determine which interface we are using?

+7
java class interface diamond-problem abstract
source share
7 answers

You do not agree. And it doesn’t matter, since the implementation is not on the interface, but on the class. Thus, the implementation is unique. There is no ambiguity.

What does it mean if each ad 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 may encounter multiple interfaces in Java.

+10
source share

It does not matter. The purpose of the interface is to indicate that class C has a run () method. You may depend on this method.

Despite the fact that both A and B specify the start method, it is implemented only once, so there is no conflict.

When you say you use an interface, it really means how your objects are declared:

  A myobject = new C(); myobject.run(); 

against

  B myObject = new C(); myobject.run(); 

In the first case, you use the "interface", i.e. your code assumes that myobject is of type A and that it has a method called run (). In the second case, you "use" interface B.

+7
source share

If a type implements two interfaces, and each interface defines a method that has an identical signature, then in fact there is only one method, and they are not distinguishable. If, say, two methods have conflicting return types, then this will be a compilation error. This is a general rule of inheritance, method overriding, hiding and declarations, and also refers to possible conflicts not only between two inherited methods of an interface, but also an interface and a superclass method, or even just conflicts due to erasing generic types.

Compatibility example

Here is an example in which you have a β€œGift” interface that has a present () method (just like when giving gifts), as well as a β€œGuest” interface that also has a present () method (for example, a guest is present and not absent) .

Presentable johnny is a gift and a guest.

 public class InterfaceTest { interface Gift { void present(); } interface Guest { void present(); } interface Presentable extends Gift, Guest { } public static void main(String[] args) { Presentable johnny = new Presentable() { @Override public void present() { System.out.println("Heeeereee Johnny!!!"); } }; johnny.present(); // "Heeeereee Johnny!!!" ((Gift) johnny).present(); // "Heeeereee Johnny!!!" ((Guest) johnny).present(); // "Heeeereee Johnny!!!" Gift johnnyAsGift = (Gift) johnny; johnnyAsGift.present(); // "Heeeereee Johnny!!!" Guest johnnyAsGuest = (Guest) johnny; johnnyAsGuest.present(); // "Heeeereee Johnny!!!" } } 

The above snippet compiles and runs.

Please note that only one @Override is needed !!! This is because Gift.present () and Guest.present () are "@ Override-equivalent" (JLS 8.4.2).

So johnny has only one implementation of present (), and it doesn't matter how you feel about johnny, whether it is a gift or as a Guest, there is only one method to call.

Incompatibility example

Here is an example where there are two inherited methods: NOT @ Override equivalent:

 public class InterfaceTest { interface Gift { void present(); } interface Guest { boolean present(); } interface Presentable extends Gift, Guest { } // DOES NOT COMPILE!!! // "types InterfaceTest.Guest and InterfaceTest.Gift are incompatible; // both define present(), but with unrelated return types" } 

This once again confirms that the inheritance of members from the interface must obey the general rule for declaring members. Here we have Gift and Guest define present () with incompatible return types: one void another logical. For the same reason that you cannot use void present () and boolean present () in the same type, this example leads to a compilation error.

Summary

You can inherit methods equivalent to @Override, given the usual requirements of overriding and hiding a method. Since they are ARE @ Override-equivalent, only one method is effectively implemented and, therefore, there is nothing to distinguish / choose from.

The compiler does not need to determine which method is for which interface, because once they are defined as equivalent by default, they are the same method.

Resolving potential incompatibilities can be challenging, but this is another problem.

References

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.4

+2
source share

Even if you implement multiple interfaces with the same method declarations, you will only have one definition of this method in your class. So there is no ambiguity here, as there is only one method to call.

+1
source share

You cannot say; however, this does not matter since the interfaces do not have functionality.

This will change a bit when Java 8 arrives, as interfaces can have standard default function implementations; if there is more than one default implementation, then there may be ambiguity. How this is resolved is explained in the question. Are JDK 8 default values ​​a form of multiple inheritance in Java? .

+1
source share

Your question does not make much sense. Take a realistic example. A Human may cry. AnimalBaby may cry. Now you have a copy of John , which is both Human and AnimalBaby, and you are crying. What's happening? John screams. The fact that he implements the cry() methods of both interfaces does not change what crying means and does for John.

0
source share

I. C inherited two methods from two interfaces (which turned out to be overriding equivalents (8.4.8.4) ). C, that is, one method that overrides both because the signature matches both. (8.4.2)

However, one may wonder β€” simply because the two methods have the same parameter names and types, does not necessarily mean that they have the same semantics. This name can be one English word, which means two different things. What if we inherited two methods from two superinterfaces that look the same but are actually different in semantics, so there must be two implementations in the subclass? for example

 interface java.lang.Runnable void run() interface com.animal.Runnable void run() class Dog implements com.animal.Runnable, java.lang.Runnable // a dog as a task ??? 

We can imagine a language that can handle such cases. But Java does not. If the two methods are quite similar, it is assumed that they have the same semantics. Fortunately, in practice this still seems like a problem.

0
source share

All Articles