File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Interfaces with same method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interfaces with same method" Watch "Interfaces with same method" New topic
Author

Interfaces with same method

sridhar lakka
Ranch Hand

Joined: Jan 02, 2007
Posts: 109
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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces with same method
 
Similar Threads
How a method on stack knows that to which object on heap it is connected?
Basic Question about extending/implementing
regarding interface
How a method on stack knows that to which object on heap it is connected?
Interface Doubt