aspose file tools
The moose likes Beginning Java and the fly likes interface implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "interface implementation" Watch "interface implementation" New topic
Author

interface implementation

Mahesh Barik
Greenhorn

Joined: Aug 14, 2006
Posts: 15
in cases where one class is implementing 2 interfaces and both the interfaces have same method signature
and our class is overriding the method
which version will be called?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
There are multiple declarations of the method, but there's only one definition, so there is no ambiguity as to what gets called.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32838
    
    4
overriding the method
But you don't override the method. All methods in an interface are empty and (implicitly or explicitly) abstract.
Overriding means to change the method from the superclass.
What you are doing is implementing the empty method. That is why you write "public class Foo implements Bar{}."
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10043
    
    6

when you implement an interface, you are saying "i promise that in this class i am writing, i will put the code for the methods listed in these interfaces".

you then write the code for said methods. if two (or twelve) interfaces all declare the same method name/signiature, it doesn't matter. you implement it once and have satisfied that part of the contract for ALL the interfaces.

So, no matter how you reference the object, you will get YOUR one-and-only implementation of hte method.


Never ascribe to malice that which can be adequately explained by stupidity.
manuel aldana
Ranch Hand

Joined: Dec 29, 2005
Posts: 308
i think you mean with method signature the method name and the parameters, because implementing A and B at the same time would clash:

interface A{
void bla();
}

interface B{
String bla();
}

apart from that i think it is not quite correct to define interface methods solely by their signature. there is a lot of semantics behind it, e.g. what you input and what you get (see design by contract). that means, though your interface methods look identical they offer different semantics.

so if i was you i would go for some rename-method refactorings to avoid such a problem. or if the semantics are the same, i would try use move method to form new interface groupings.
[ August 16, 2006: Message edited by: manuel aldana ]

aldana software engineering blog & .more
Mahesh Barik
Greenhorn

Joined: Aug 14, 2006
Posts: 15
hi Thanks for your response
does the same principle apply for abstract class too?
thanks
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
No, because you can't extend two (abstract) classes.


Joanne
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
But you can extend an abstract class and implement an interface which could get you right back in the same problem spot. Anybody else remember Judy Tenuta? It could happen!

Manuel's post was pretty profound. Make sure you got the points. If the two abstractions use the same signature but are documented or intended to do different things, you're in trouble. If they use the same signature for the very same meaning, maybe the method should be its own interface.


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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface implementation
 
Similar Threads
Regarding Interface Concept
If one class implements 2 interfaces having same method...
Multiplae Interface with commaon method
Interfaces with same method
problem with deadly diamond of death