File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interfaces with same method" Watch "Interfaces with same method" New topic

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,
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.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

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 8 API documentation
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:
subject: Interfaces with same method
It's not a secret anymore!