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
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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With