Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing multiple interfaces with identical method names !  RSS feed

 
Wolfgang Obi
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!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
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.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!