Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism - When does it actually take effect?

 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,



I just failed the Chapter 6 test in the Gupta book and am understanding what I missed on most problems but have a question about question 5. I put that this was polymorphic with classes option a, but the actual answer is d none of the above.

The following code is given for the problem:


So if class Person defined a method that was not private and classes Employee and Doctor extended it would this be polymorphism? Or do classes Employee and and Doctor need to override the method defined in the base class Person? Or do classes Doctor and Employee need to simply define the method as defined in class Person without making any changes?

When is it polymorphic?

Regards,

Ted's Excellent Java Adventure



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted,
Right. You'd need the method to be overridden for polymorphism to be illustrated.
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Ted,
Right. You'd need the method to be overridden for polymorphism to be illustrated.


Jeanne,

So if the base class has a method and a sub-class extends this base class then this is not polymorphism unless the sub-class class overrides the method explicitly in code?

So this is not polymorphism to my understanding...

Although this is polymorphism...


Thank-you for the reply Jeanne.

Respectfully,

Ted

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ted,

In my understanding that's also not polymorphism. That's just method overriding, but that's a must requirement to be able to have polymorphism. Polymorphism comes into play when you have a reference variable of a superclass and an instance of a subclass and you invoke an overridden method on the reference variable. So extending your example a little further to illustrate:


So in my opinion just a class hierarchy with overridden methods can't be an exhibition of polymorphism. But with such class hierarchy you can create a polymorphistic example. I don't know if this certification exam cares about this slight difference.

Kind regards,
Roel
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel,

Thank-you for the interesting reply. I see what you mean and agree with you. Superclass object-reference variables that point to sub-class objects on the heap are very polymorphic. I wish there was a more logical definition of what polymorphism is though so I could just input it in a simple math expression or run it through a polymorphism scanner or something.

I found this definition in the Gupta book that is helpful in determining what the exam authors may consider as polymorphism:
“When methods with the same method signature are defined in classes that share an inheritance relationship, the methods are considered polymorphic” (Gupta, p. 345).


Regards,

Ted

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic