Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One class implement two interfaces with common methods??

 
Hemant Agarwal
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two interfaces have same methods.
one class implements both interfaces.
Is any problem in this? How will we distinguish the methods?
 
Albin Joseph
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem.

Why you need to distingush methods from these two interfaces ? Because there is no implementation in the interface so would it make any sense distingushing these two itnerfaces.
 
Hemant Agarwal
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But .NET is distinguishing why?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No idea about .NET ... does the method implementation specify which interface it is for? But in Java the logic is fairly simple.

Interface A says the implementer must have method x(). Interface B says the implementer must have method x(). If you have method x() the syntax is good and you have satisfied the compiler's checking for both interfaces.

If the documentation for interface A says x() increments the x coordinate and the doc for B says it decrements the x coordinate, the semantics are inconsistent and some client of your class will be unhappy.

So there can be cases where implementing two interfaces with matching method signatures is possible, but not correct. If .NET matches the method implementation to the interface they might get around this issue.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic