Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing multiple interfaces with identical method names !  RSS feed

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm trying to realise the following,...



I'm getting error messages concerning:

1. the method names.
2. the return values.

why wont it work?
and : what would be the right way to do this ?

thanks!
 
author and iconoclast
Sheriff
Posts: 24219
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If two interfaces each declare a method with the same name and argument list, but different return types and/or incompatible exception declarations, then a single class may not implement both those interfaces. This is explicitly pointed out in the Java Language Specification.

One thing you can do is implement just one interface, and use an "adapter" method to provide a "view" of an object that implements the second interface. An inner class is ideal for this, since it has access to the members of the outer class.

 
lowercase baba
Bartender
Posts: 12624
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You somehow posted your question 3 times. Please try to be more careful. I have removed two of them.

It would help if you posted the EXACT error message you are getting, not a paraphrase of what it sort of said.

It would ALSO help if you posted the relevant section of your actual code, not a pseudo-version of what is kind of looks like.

Now, having said all that...

IF what you mean is that interface 1 has a method like

public long a();

and interface 2 has a method like

public int a();

you're in trouble. java uses the name and parameters to determine what method to call (this is called the 'signature' of the method), not a return type. If you have two methods that have the same signature, but return different things, java can't tell which one you are trying to call.
 
She'll be back. I'm just gonna wait here. With this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!