Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your only fix is to change one of the method names. You can't overload return types in Java.
 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jean Fore
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ilja and also everyone.
-JEAN
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic