9

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

Community
  • 1
  • 1
Aurelien Ribon
  • 7,548
  • 3
  • 43
  • 54

2 Answers2

5

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.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

No, there's no equivalent feature in Java.

And you can't do it yourself, because inside the method, you have no way of telling if the calling code is referencing the object as InterfaceA or InterfaceB. Even if you could, I think it would be a bad idea.

Mike Baranczak
  • 8,291
  • 8
  • 47
  • 71