Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple interfaces with same method names [duplicate]

Tags:

java

interface

I have a class which inherits from two different interfaces. Both interfaces declare a method with the same name. How can I provide a different implementation for each interface ?

In C#, the answer is there, but it does not work in java: Inheritance from multiple interfaces with the same method name

I thought about providing a union implementation which uses type comparison but it's kind of ugly.

Thanks

EDIT : closed, my question was a duplicate of the following, thank you for the answers ! Java - Method name collision in interface implementation

like image 439
Aurelien Ribon Avatar asked Oct 27 '10 12:10

Aurelien Ribon


People also ask

Can two interfaces have the same name and the same parameter?

In this article we will see a situation that occurs when two interfaces have methods with the same name and the same parameter (s) and one base class implements these interfaces. For example, we create interfaces that have methods with the same name. After that we create a class that inherits from multiple interfaces. What is Interface?

How to implement two interfaces in a single class?

References. If both interfaces have a method of exactly the same name and signature, the implementing class can implement both interface methods with a single concrete method. However, if the semantic contracts of the two interface method are contradicting, you've pretty much lost; you cannot implement both interfaces in a single class then.

What are the members of an interface?

Like a class, Interface can have methods, properties, events, and indexers as its members. But interface will contain only the declaration of the members. The implementation of interface’s members will be given by the class who implements the interface implicitly or explicitly.

What happens if two interfaces have the same signature in Java?

If a type implements two interfaces, and each interface define a method that has identical signature, then in effect there is only one method, and they are not distinguishable. If, say, the two methods have conflicting return types, then it will be a compilation error.


1 Answers

You can't. Interfaces describe behavior, but they don't implement it. So if you implement a method, there's no way to tell which interface you are implementing it from.

like image 78
Sean Patrick Floyd Avatar answered Oct 06 '22 05:10

Sean Patrick Floyd