Two interfaces with same method signature implemented in Java class

梦想的初衷 提交于 2019-11-27 19:50:18
Joni

This scenario specifically allowed in the Java Language Specification, section 8.1.5:

It is permitted for a single method declaration in a class to implement methods of more than one superinterface. For example, in the code:

interface Fish { int getNumberOfScales(); }
interface Piano { int getNumberOfScales(); }
class Tuna implements Fish, Piano {
   // You can tune a piano, but can you tuna fish?
   int getNumberOfScales() { return 91; }
}

the method getNumberOfScales in class Tuna has a name, signature, and return type that matches the method declared in interface Fish and also matches the method declared in interface Piano; it is considered to implement both.

The text then goes on to note that if the method signatures had different return types, such as double and int, there would be no way to implement both interfaces in the same class and a compile time error would be produced.

For this issue it's necessary to understand what interfaces are for.

An interface is a kind of "contract" so that one knows which methods are compulsorily implemented in a Class with that interface.

So if you need a Class implementing "DVDPlayer" (because you need the method "play()"), you'll find CarPlayer. Same goes for the need of a Class implementing CassettePlayer. That's the technical explanation.

But of course in your semantic coding you should ensure that CarPlayer's method "play()" satisfies the semantics of both DVDPlayer and CassettePlayer. I think in a practical application it will be a bad practice.

Of course in your example it's a bad idea to have two interfaces declaring the same method. More practically, you should have made an interface "Player" with method "play()" and have two other, more specific interfaces DVDPlayer and CassettePlayer (with specific methods for DVDs and cassettes) who inherit from Player. On the onther hand, if you don't need specific methods for DVDs or cassettes, then you don't need two different interfaces only implementing one and the same method - just use one interface Player, that'll be enough.

In this situation, there is no issue because both interfaces have same method signature. But What about this ?

interface Animal {
    public void eat() throws IOException;
}

interface Plants {
    public void eat() throws NullPointerException;
}

Which one is choosen by the compiler ? Why does it get error below code ?

public class Test implements Animal, Plants {

    public void eat() throws IOException {

    }
}

Compiler says : Exception IOException is not compatible with throws clause in Plants.eat()

There is no conflict because they both specify the same contract, implementing classes only provide the one method which is called when referenced via either interface.

Why not? The class is satisfying the contracts defined by both interfaces.

The class implements both interfaces - so no issue. Of course, this sort of thing should be avoided in more complex scenarios where unintended behaviour might result.

The following page contains an example of a class that implements two interfaces that have the

1) same variable name 2) same method in each interface.

http://www.j2eeonline.com/java-tm-fundamentals-II/module2/interface-ambiguous-fields.jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!