• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface Doubt  RSS feed

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



Here I am confused which runStyle() is implemented? Sprinter one ? or Panther One ?

And why ?

Thanks in Advance !
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the compiler's point of view, both. The compiler only checks if all methods of all interfaces are implemented, and the runStyle method signature matches that of both interfaces.

It could be the case that the meaning of the method is different in both interfaces, and then it is very well possible that the method doesn't behave like one of the interfaces says it should. For instance, if Panther.runStyle() specifies that 4 legs should be used and Sprinter.runStyle() specifies that 2 legs should be used, you can't have both.
 
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One, the other or both. As long as the runStyle() methods have the same return type and the same intention, it doesn't make any difference.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me begin with an analogy.

Take the example of a person named Larry giving a contractor named Curly the job of building a house. Larry tells Curly that the house must be Durable, Weatherproof and Comfortable to live in.

Now Larry knows nothing about building a house or making sure that it is Durable, Weatherproof or Comfortable. Curly fortunately does. And Larry doesn't want to know or doesn't care. He just wants the house to be all these.

So, Larry needs to do some work to make sure all these conditions are fulfilled.

So, we have:



So, whatever methods are specified by the interface have to be implemented by the implementing class, returning a result of a specific form, it doesn't matter if interfaces have the same methods signatures (the return type and the arguments), as long as they are implemented without fail.

Now we move on to your doubts.

Ashish Tiwari wrote:
Here I am confused which runStyle() is implemented? Sprinter one ? or Panther One ?
And why ?


Interfaces specify what needs to be done, not how they need to be done. So, keeping this in mind, it is not Panther's runStyle or Sprinter's runStyle() that is implemented, but it is the object's, which in this case is RunStyle.

And RunStyle clearly fulfills the conditions set by both Panther and Sprinter since it implements the method void runstyle().

Ashish Tiwari wrote:




So, that's the reason this program runs without any issue. Because RunStyle simply does what Panther and Sprinter want to be done.

Hope that clears it up.
 
Campbell Ritchie
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice answer, and welcome to the Ranch, Manu Ullas
 
Manu Ullas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell. I waited till I got an SJCP 6 before joining up.
 
Fidel Edwards
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manu Ullas wrote:

So, that's the reason this program runs without any issue. Because RunStyle simply does what Panther and Sprinter want to be done.

Hope that clears it up.


Thanks Manu for this nice explanation.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Tiwari wrote:
Here I am confused which runStyle() is implemented? Sprinter one ? or Panther One ?


Clashing method names is no problem for the compiler. It just makes sure an implementation is available and this implementation is used for all clashing names.

But clashing names indicate a design problem. Do you really want the clashing methods to share the same implementation? More often than not the answer is no. In that case you need to rename the methods to resolve the conflict.

Note how Java's inheritance model makes sure clashing names can never be a problem for the compiler. It would be far worse if the clashing methods each came with a different implementation (which would be possible if Java allowed multiple inheritance of implementation).
 
Fidel Edwards
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tons of Thanks Ulrika !

 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Tiwari wrote:Tons of Thanks Ulrika !


You're wellcome.

Thanks to Java's inheritance model (multiple inheritance of interface, single inheritance of class), clashing methods are the "only" remaining problem with multiple inheritance. But it can be quite annoying. For some reason people tend to pick very similar method names for their types so clashes tend to be quite common. And the compiler stays silent when it happens so one has to be careful.
 
Fidel Edwards
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulrika Tingle wrote:
For some reason people tend to pick very similar method names for their types so clashes tend to be quite common. And the compiler stays silent when it happens so one has to be careful.


Thanks Ulrika !

Again Thanks ! That is the point I am concerned.


Also thanks all those who spent their precious time in solving the problem .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!