Today we went a little deeper into polymorphism, in terms of how it applies programmatically.
We learned that if you use the keyword “virtual” on a method definition for a base class, children classes can use the keyword “override” to override those methods.
We demonstrated this by passing Animal objects into a method and calling the Speak() method. The HouseCat meowed and the Sparrow chirped without us having to cast them as HouseCats or Sparrows.
We then talked about other demonstrations of polymorphism, especially in games. A game might have a Monster class, and different types of monsters (Bears, Golems, Rats) might have different experience values, or do different damage. Or a game might have a Weapon class, where a Sword class might have a very short rang, a ShotGun class might have a medium range, and a SniperRifle might have a very long range.
After that demonstration and the discusion, the class was tasked with using the PolyObjects project to create their own polymorphism demonstrations.
Towards the end of the class, I did another demonstration, which is included in the following project: