• Post Reply Bookmark Topic Watch Topic
  • New Topic

inheritance ambiguity problem  RSS feed

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,
if I am trying to implement in one class two interfaces that has methods with the same signature, how do i escape from the ambiguity problem. is there a way to notify in the syntax who is the father of the interface i am trying to implement (like in c++).
could you please give me some syntax/code example.

thanks, joseph
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there's not. You will probably have to abandon the idea of implementing both interfaces in a single class. One possible alternative is to implement at least one of the interfaces using a nested class of some sort, and give the outer class a method to return an instance of the nested class. So, given an instance of the outer class, you'll be just a method call away from an instance of the interface you want, even though the outer class doesn't implement that interface. It's similar to how all Collections (or nowadays, all Iterables) have an iterator() method to return an Iterator which is really another way of accessing the same data that the Collection had. It's like having a different view of the data held by the object.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you implement an interface you just promise to have the methods the interface requires. If two interfaces require myMethod() and you implement it once you've satisfied both promises. Your method might not satisfy the caller's expectations, however.

The two myMethod calls both call the single myMethod implementation. Will that do what callers expect? Depends on what the two interfaces led people to expect.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is why I said "probably". It's possible that if both interfaces use the same signature and return value, then it's possible satisfy both interfaces simultaneously. At least as far as the compiler and JVM are concerned. However I would say it's fairly unlikely you will be able to satify the intent of each API simultaneously, unless one interface is an extension of the other - in which case the method declaration in the subinterface is a refinement of the original declaration, and is designed to be compatible. E.g. compare add(Object) in Collection and add(Object) in List. The latter merely gives more specific requirements about the behavior of the method. But if you're dealing with two interfaces which are not related except they share a method signature, it's almost certainly a bad idea to try to implement both interfaces simultaneously. Even if the compiler does let you do it.

Now if the two interfaces declare methods with the same signature but different return types, there is no way you will possibly implement both interfaces with a single class. Ever.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!