+ 1
Polymorphism and overriding
In order to get it right: what's the difference between polymorphism and overriding? Or is polymorphism achieved through overriding? Or I got it completely wrong?
3 Réponses
+ 12
Let's say a class Vegetable has the eat() function defined.
then the class Potato extends Vegetable and adds other functions.
Despite not defining eat() itself, a Potato object can call eat() because it is also a Vegetable.
That's polymorphism.
If you wanted to specify that potatoes are not eaten the same as any default vegetables though, you could define a new eat() function with the same list of parameters.
From then on, when you would use [Potato object]. eat (), the most immediate matching definition (Potato's) will be used.
That's overriding.
+ 1
Thank you Nsyse. That is very enlightening.
+ 1
you can say that polymorphism use override to achieve his job.this job is to change the behavior at the running time but it also help u to reuse the code by using type caste.