• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Implementing multiple interfaces with identical method names !  RSS feed

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

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 ?

author and iconoclast
Posts: 24203
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
Posts: 12705
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.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!