Part 10 - Polymorphism, or Inherited Methods
| Definition: Polymorphism The ability for a new object to implement the base functionality of a parent object in a new way. |
Two keywords are used to make Polymorphism happen: virtual and override.
You need to describe a method as virtual if you want the ability to override its capabilities.
Note that, because Rectangle does not have a default, parameterless constructor, we must invoke Rectangle's constructor in the first line of Square's constructor (the super(width, width) part). Once again: when calling a superclass's constructor, it must be done on the first line.
24.0 25.0 25.0
Even when casted to a Rectangle, s's .GetArea() functioned like if it were a Square.
An easier example to see is this:
From Base From Derived From Derived
If I were to leave out the virtual and {{override} keywords,
From Base From Derived From Base
This happens because unless the base method is virtual or abstract, the derived method cannot be declared as override.
| Recommendation Although you do not have to explicitly declare a method as |
In order to override, the base function must be declared as virtual or abstract, have the same return type, and accept the same arguments.
Polymorphism is very handy when dealing with multiple types derived from the same base.
Woof Meow *Noise of a Hippo*
Very handy.
