• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing multiple interfaces with identical method names !

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic