| Author |
inheritance ambiguity problem
|
Joseph Sweet
Ranch Hand
Joined: Jan 29, 2005
Posts: 327
|
|
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
|
We must know, we will know. -- David Hilbert
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
|
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.
|
"I'm not back." - Bill Harding, Twister
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
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.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
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.
|
 |
 |
|
|
subject: inheritance ambiguity problem
|
|
|