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?
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.
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).