wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes methods with same signature, different return types in interfaces conflict in class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "methods with same signature, different return types in interfaces conflict in class " Watch "methods with same signature, different return types in interfaces conflict in class " New topic
Author

methods with same signature, different return types in interfaces conflict in class

Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
Hi,
I have a class which implements 2 simple interfaces. They have a common method with the same signature, but different return types. How will I take care of this in the client class ?
Following is the code for the class and the intefaces. This is giving compilation error. How do I fix this regarding the implementation of method b()?

public interface interface1
{
void a();
int b();
}

public interface interface2
{
void b();
void c();
}


public class InterfaceTest implements interface1, interface2
{
public void a(){}
public int b()
{
int p=0;
return p;
}
public void b(){}
public void c(){}
}

Thanks
-JEAN
[ March 24, 2006: Message edited by: Jean Fore ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Your only fix is to change one of the method names. You can't overload return types in Java.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
When you say "you can't overload return types in Java", does it mean that while overloading methods in Java, I must have different signatures ONLY? I can't have different return types? Or do you mean that you MUST always have different signatures and also you may or may not have different return types along with different signatures?
-Thanks
JEAN
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Paul Sturrock:
Your only fix is to change one of the method names. You can't overload return types in Java.

...or not have the class implement both interfaces. Of course, thiss is a made up example, so consider java.util.Map as a better motivating example. With a Map there is a Set of keys, as well as Collection of values, but Map doesn't implement Set or Collection. Instead it has methods:

Set keySet();
Collection values();

Again, one needs a real context to see if the class has been painted into a corner or if a better solution is to use composition (or "views") like Map.


There is no emoticon for what I am feeling!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jean Fore:
When you say "you can't overload return types in Java", does it mean that while overloading methods in Java, I must have different signatures ONLY? I can't have different return types? Or do you mean that you MUST always have different signatures and also you may or may not have different return types along with different signatures?
-Thanks
JEAN


The latter.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jean Fore
Ranch Hand

Joined: Feb 23, 2006
Posts: 33
Thank you Ilja and also everyone.
-JEAN
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Under JDK 5 it does become possible to have different return types in some situations. If it's possible to find a class or interface which is a subclass or subinterface of the return types of both the method declarations you're trying to implement. E.g.

However in many cases it's not possible to find a single type which is a subtype of the required return types. And I don't think this sort of thing is possible at all for primitive return types, or void, such is in Jean's original problem. In which case we're back to the alternative already suggested: change one of the method names, or don't implement one of the interfaces. There's really no other way to make this work, as far as I can see.
[ March 24, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:
Under JDK 5 it does become possible to have different return types in some situations.


And that's called "covariant return types". But that's not about overloading anymore - that is, you can't put two methods with the same signature and different return types in the same class. You can only override a method with a more specific return type than the one in the superclass/interface.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Um, this topic has never really been about overloading. Paul Sturrock's comment notwithstanding. It's always been about methods with the same signature, which are therefore overriding, not overloading. Signature does not include the return type. And you can never have two methods with the same signature in the same class, period - regardless of their return types. If we were overloading, there would be no problem at all having different return types.

As for whether or not my post is relevant to the original question, that depends whether you consider a subtype to be "different" from its supertype. Either way, my point was that this is as close as you can get to a valid answer to the original question. Otherwise it boils down to no, you can't do that.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:
Um, this topic has never really been about overloading.


The InterfaceTest class in the original question had two b methods with different return types, so I understood Jean to want to overload it.

It's always been about methods with the same signature, which are therefore overriding, not overloading.


Exactly! I just wanted to make sure that Jean understands that - no critique on your post implied; I think it was highly relevant! Sorry for my ambiguous phrasing. I guess I need to work on the elaborateness of my posts...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I suppose that the prolem is that what she was doing had elements of both overriding and overloading, and that's why it didn't work. (And why we were calling it different things.) You can override or overload, but not both. Anyway I figured the original goal was to implement two interfaces with the same bethd but different return types, so I just focued on that. I wasn't understanding why some people were talking about overloading, and I misinterpreted your post as disagreement. Sorry about that. Cheers...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: methods with same signature, different return types in interfaces conflict in class
 
Similar Threads
Interface
Questions for you
Which concept of OOPs is this?
how to implement two different definition for class which implements two interface with same method
Is this a Bug in Java