Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Polymorphism - When does it actually take effect?

 
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
author & internet detective
Posts: 40552
825
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
reply
    Bookmark Topic Watch Topic
  • New Topic