开发者

Java - double definition in two interfaces

Class A can implement multiple interfaces.

What I do not understand is what happens in this case:

public interface example1 {
    void checkInt(int a);
}

public interface example2 {
    void checkInt(int b);
}

public class class1 implements example1,example2 {
    void checkInt(int c){
        System.out.print(c);
    }
    this.checkint(5);
}

I tried to run it, and it gives me compilation errors. But my question is in general, can I implement two interfaces, that have a 开发者_开发知识库functions with the same signature?


What compilations errors are you getting? Because this.checkint is in the wrong case, it should be checkInt(5) ? Also you're reducing the scope of the method from implied public (as all interface methods are) to default (packaged protected). The checkInt method needs to be public in the implementing class.

Both interfaces define the method, but this is fine as there is one implementation on class1. So class1 fulfils the implemented methods of each interface with its implementation of checkInt(int i). With interfaces you're only fulfilling a contract, so there is no ambiguity about which method to call - if you work with references of type example1 or example2 it will still be the same checkInt method on class1 which gets invoked.


The compilation error is because you are missing visibility attribute (public) in the implementing class for the method checkInt and you have a statement that's not enclosed by any method.

But in general if you have a class implementing multiple interfaces that have common method signatures,then the class will have just one implementation to satisfy both interfaces.


I'm pretty sure your compilation error is from the this.checkint(5) line, and not the interface implementations. Implementing two interfaces that share a method with an identical signature is perfectly legal, and you have done so above.

You can think of it this way: when a class implements an interface, it agrees to implement all the methods defined by the interface. As long as you have fulfilled this requirement for each interface, it doesn't matter how many methods you've actually written to do so.

Also, consider how interfaces are actually used:

example1 e1 = new class1();
e1.checkInt(3); // Calls class1.checkInt()

example2 e2 = new class1();
e2.checkInt(3); // Also calls class.checkInt()

The interface variables are actually referring to an object of type class1, so it is class1's methods that matter, not what other interfaces class1 implements.


You do not Inherit an interface, you implement it. What implementing an interface does for you is to notify consumers of your class that you guarantee that you will have a certain set of methods available for use. Many interfaces can have a single method signature in their contract but since only the implementing class has actual code, there is not and issue.


Classes dont inherit from interfaces.

Classes inherit Classes
Interfaces inherit interfaces
Classes `implements` Interfaces

Your example is perfectly fine as Interface just declares signatures (or contract), does not define behaviour. Classes 'implement' Interfaces to provide behaviour. So a class implementing two interfaces can provide 'same' implementation to both methods - Contract is not voilated as the class is inplemeenting the behaviour.


Yes you can implement both interfaces. However, this is usually not recommended, as it could lead to confusion as you say.

You can only implement both interfaces in the very specific case that the arguments, name and return types exactly match, otherwise there will be a conflict. If the names or argument types are different you will need to implement both separately. If all that differs is the return type, then it is impossible to implement both interfaces as they clash.

If the two interfaces happen to have a different meaning for the method, then you are better providing an adapter instead. See the Adapter pattern.

A whole article on solving this problem has been written here.

Your compilation errors are due to something else (in this case you haven't declared your implementing method public), and looking at the code sample you provided, probably lots of other things!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜