Polymorphism and overriding | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

6th Jan 2017, 12:52 PM
Efi Laza
Efi Laza - avatar
3 Answers
+ 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.
6th Jan 2017, 4:34 PM
Nsyse
Nsyse - avatar
+ 1
Thank you Nsyse. That is very enlightening.
7th Jan 2017, 9:52 AM
Efi Laza
Efi Laza - avatar
+ 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.
10th Jan 2017, 2:01 PM
Magician
Magician - avatar