• Post Reply Bookmark Topic Watch Topic
  • New Topic

Same method signature in extended class AND implemented interface?  RSS feed

 
Chris Akes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello,

After reading a glowing account of how interfaces solve issues with multiple inheritance, I was wondering how the JVM resolves the problem of methods with identical signatures getting inherited from an extended class AND an implemented interface? Do the interface methods simply override any conflicting methods inherited from the extended class?

Thanks!

Chris Akes
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As interfaces have no implementation, there's nothing to be over-riden. A class simply implements a method as defined by the interface. It doesn't matter where the method comes from, inherited or otherwise.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And even if you implement two interfaces with the same method signature, there's nothing special the the compiler or JVM need to do. All they care about is that you're honoring both contracts--at least the parts of the contracts that are in their jurisdiction.

The problem however--both with implementing two interfaces with the same method signature and with implementing one interface and extending a class, if that class didn't get the method from the interface as well--is that the semantics of the method may be different, incompatible, or contradictory. The compiler and JVM don't know or care about that, but the user of your class certainly will.




 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this case i'd say that the implementations that should have been different originally completely overlap now. the two share one method implementation. this might cause problems. but it's the design stage issue i think.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
drac yang wrote:in this case i'd say that the implementations that should have been different originally completely overlap now. the two share one method implementation. this might cause problems. but it's the design stage issue i think.
What does that mean?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Akes wrote:I was wondering how the JVM resolves the problem of methods with identical signatures getting inherited from an extended class AND an implemented interface?

well...why do you think there IS a problem?

Let's think through this...

An interface - call it MyInterface - simply lists a bunch of methods + signatures. It basically says "Anybody who wants to consider themselves one of ME has to have these methods".

Then you have ClassA. Let us assume that it implements one (or more) of the methods defined by the interface...so far, no problem. Any class can have any method it wants.

Now, let us have ClassB extend ClassA. It gets the methods for free - hurray!!! There is still no problem, because again, any class can implement any method it wants.

Now, suppose ClassB also implements MyInterface. The compiler says "ClassB wants to be considered a MyInterface. Does it have all the necessary methods required, either by implementing them directly or does it get them via inheritance? Yes, it does. OK, all is right and good in the world. Carry on".

So...where exactly do you see a problem?
 
drac yang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
drac yang wrote:in this case i'd say that the implementations that should have been different originally completely overlap now. the two share one method implementation. this might cause problems. but it's the design stage issue i think.
What does that mean?

what i wanted to mean is just the same with jeff's.

originally there should have been two different method implementations for the two different interfaces, say, draw1 and draw2, now they have become one method implementation, draw.
it might cause problems(in business logic area), but it's the design that has caused this(class chooses to implement two different interfaces with same method name).
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean that implementing two interfaces with methods which are override‑equivalent to each other, but have different intent is bad design? Agreed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!