Is it ok if i understand classes like Polymorphisim Abstractions etc but i dont know how to use them
Like i understand what polymorphisim is and different classes but when i get to the writing them myself part i freeze and dont know what exactly to do
3 Replies
it is just a term to describe you have a base class reference and the base class defining some virtual method, you can call it and let the actual child class does the work without knowing what child class actually is
it requires some sort of foresee, if you know you gonna to have multiple implementations of some type then you will use it
In my opinion, if you don't know how to use it then you don't truly have a deep understanding. The quickest fix for this is to go online and find a few different examples of polymorphism in action and recognize the patterns between them.
https://www.w3schools.com/cs/cs_polymorphism.php
This page uses an animals example. Try to figure out a few different scenarios where polymorphism might be useful and try implementing those
Sorta like how a computer and a car get turned on in two different ways. A computer's
turnOn()
method might just involve pressing the start button. A car's implementation of turnOn()
might involve turning the key in the ignition. Both a computer and car might inherit from a Machine
base class