• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt about polymorphism and interfaces  RSS feed

 
Emanuele Mazzante
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I'm studying for the exam by a good manual but it left me a doubt about the polymorphism and interfaces. Start with the classic example:

Then when I do something like:

I understand the meaning of polymorphism and dynamic binding. Now I come to my doubt, in the text is written that polymorphism can also be implemented using interfaces and is showed in a example like the following:

and after:

Why did the author extends the base class? He also specifies that polymorphism comes into the picture when there's an inheritance relationship between classes and both the base and derivied classes define methods with the same name.
If you use only the interfaces and their implementation like following, is this also called polymorphism?

Thanks for the help and sorry if I have dwelt and for not perfect English
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before we can answer, please tell us which book you are using.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuele Mazzante wrote:I understand the meaning of polymorphism and dynamic binding....

Hmm. I wonder...   If you do, then your next sentence would seem contradictory.

However, a mantra that helped me when I was going through this stuff was:
ALL non-final instance methods in Java are virtual.

Now, Java doesn't have a 'virtual' keyword, but what it means is that unless a method has the final qualifier (which means it can't be overridden any further), the JVM will always look for the LOWEST version in the hierarchy to execute; and it will do so according to the runtime type of the object, not its defined type. Indeed, this is precisely how polymorphism works.

If you use only the interfaces and their implementation like following, is this also called polymorphism?

Yes.

Winston
 
Emanuele Mazzante
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Before we can answer, please tell us which book you are using.


Sorry, in fact I forgot to mention the reference text which is OCA Jave SE7 by Mala Gupta, from Review Notes pag. 336

Winston Gutkowski wrote:
Emanuele Mazzante wrote:I understand the meaning of polymorphism and dynamic binding....

Hmm. I wonder...   If you do, then your next sentence would seem contradictory.


Thank you for your help and patience. I wanted to say that the first example exhibits a good first concept of polymorphism that a beginner like me can understand without much difficulty, but it only involves the inheritance between two classes. When the author introduces polymorphism with interfaces, it seems that must be present a relationship of inheritance between two or more classes, togheter with interface implementation, as shown in my second example. That example, along with the quotation, put me in doubt. I humbly think that it would be better to use an interface and two classes (no relation to each other) that implements it, like my last example. This would have broadened the concept of polymorphism showing another part of its potential to those who must learn this language. But this is just my poor opinion.

Thank you very much for the help and the time you gave me


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuele Mazzante wrote:Thank you for your help and patience...

No probs. Glad we could help.

If you feel strongly about it, you might e-mail the publishers. They usually quite like that sort of feedback.

Winston

 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why did the author extends the base class?


The author extended the base class to show you that an inheritance relationship can co-exist with polymorphism relationship (between parent and child, although this point is not mentioned explicitly). BTW, though it is not relevant to this question you have, keep handy the Errata of that book. I am reading the same book for the same exam, so i understand your plight :-)
 
Emanuele Mazzante
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much to everyone for the help. I appreciate it very much and I hope I can reciprocate.
N Sam wrote:The author extended the base class to show you that an inheritance relationship can co-exist with polymorphism relationship (between parent and child, although this point is not mentioned explicitly).

This is an equally valid point of view but the important thing is that I get the idea I was looking for. The book is very good and I like it a lot, but sometimes I've had to investigate a question because at some point it seemed superficial and sometimes controversial (compared to other sources).
Still on the polymorphism (quoted pag.328) the author says "Can polymorphism work with overloaded methods? No, polymorphism works only with overridden methods" I think it would be better to say that the dynamic binding does not work with overloaded methods. In another manual I found this concept explained better (in my opinion): "overloading methods is one of the ways in which Java supports polymorphism because it is a way to implement the paradigm of an interface multiple methods" (from Java: the complete reference 8th edition).

I hope that this thread will be useful to those who are in my same situation (although it is difficult to find a beginner like me...). Thanks again!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuele Mazzante wrote:Still on the polymorphism (quoted pag.328) the author says "Can polymorphism work with overloaded methods? No, polymorphism works only with overridden methods" I think it would be better to say that the dynamic binding does not work with overloaded methods...

I hate to say, but it's still a bit of a "fuzzy" area. Indeed I've seen the phrase "static polymorphism" used when referring to method overloading, which always sounds to me like an oxymoron.

There's nothing particularly special about overloading in Java, apart from the fact that the language allows two or more methods with the same name but different signatures. However, as far as it's concerned, they are simply different methods; any "polymorphic" behaviour will be in the eye of the designer or the user.

Overriding, OTOH, is inimical to polymorphism and, as far as I'm concerned, the only thing I think of when the I hear the term.

HIH

Winston
 
Emanuele Mazzante
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the explanation. I was just reading over internet that the issue is controversial. My desire is still to have a good and solid base knowledge to go into more and more in the language. I agree on the fact that overloading is not so special and for me is quite easy to understand. So it remains only a matter of definition linked to the polymorphism that however may be important during the examination. For example assuming a question of the type "select the correct answers from the following: "
a - overloading methods is an aspect of polymorphism
b - overwriting methods is an aspect of polymorphism
c - overloading and overwriting methods are aspects of polymorphism
what should be the correct answer (only one)?

(please be patient for my bad english)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuele Mazzante wrote:For example assuming a question of the type "select the correct answers from the following: "...what should be the correct answer (only one)?

And this is the problem: My answer would be 'b', since I don't consider overloading to be polymorphic; but others might say 'c' (and BTW, it's "overriding", not "overwriting").

However, it's a matter of opinion, not unassailable fact, so to mark either answer 'incorrect' would, IMO, be wrong. The only thing I can suggest, if this is something like the SCJP exam, is to look at your study guide.

Winston
 
Emanuele Mazzante
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the reply Winston, I appreciate it.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuele Mazzante wrote:Thank you very much for the reply Winston, I appreciate it.

You're most welcome. Hope it helped.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!