Expressing dynamic polymorphism
Solution 1:
I'm not really sure there is a problem to "solve", other than a syntax error in the base class of a while loop floating round at method level and some wonkiness where you calculate some insurance value for a derived type but then return the base type's insurance calc (which is 0).
Your calculate_insurance
methods in Mammal and Reptile should be declared to override
the base method, and should end with return Insurance
(note; it should be called insurance
; we do not name local variables in PascalCase) not returning the result of calling the base..
If you're then asking how to use what you have created. The idea behind polymorphism of this nature is that you can have a variable of the base type and not know or care what actual type is stored inside it, because you're only using functionality the base type claims exists
Animal a = new Mammal();
a.calculate_insurance(1); //returns 1000, the rating for a Mammal of type 1
Animal b = new Reptile();
b.calculate_insurance(1); //returns 200, the rating for a Reptile of type 1