Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces with same method

 
sridhar lakka
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Thanks in advance.
I have two interfaces with same signature.like,
interface intf1{
public void method1(int a);
}
interface intf2{
public void method1(int a);
}
and one class which is implementing these two interfaces and I have created object to this class called the method method1() from that object which is working 100% fine.
However,I am not getting how, why there is no ambiguity and which interfface method is called?

Please help me.

Regards,
Sree
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15492
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method declared in both interfaces is exactly the same. So the method implemented in the class works for both interfaces. There's nothing ambiguous about it.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If, in both interfaces, the required semantics of method1() are the same, then there is no problem.

On the other hand, if the two interfaces' versions of method1() require different semantics, then you have a problem. It is not ambiguity - there's only one implementation of method1(), so we know what will be called. The problem is that the implementation cannot provide two different types of semantics.

If at least one of the interfaces is your own, it is easy to fix such a problem. Just rename the method in one of the interfaces.

If you can't change either interface, you have to redesign your implementation. Your single class cannot implement both, so you need more than one class. Perhaps your existing class can have getter methods, each returning an object implementing one of the interfaces.

When this situation was first described to me, I thought: what a terrible limitation of Java! However, since then, there has not been a single occasion where this issue has come up and has not been trivially fixable.
[ October 08, 2007: Message edited by: Peter Chase ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic