Polymorphism in C# vs Python

Polymorphism in C#

Polymorphism is a greek work meaning “many forms”. Lets us try to understand the concept of polymorphism in the context of programming with the help of another game development example. Suppose we have a class named Monsters containing the attributes and methods/functions for all the monsters in our game.

The  Main() function simply creates a new monster object and passes it to the activate() function. The activate() function then calls the object’s run, attack and die methods.


Overriding methods

Imagine that half way through the development of our game, we need to add a new kind of monster called FireMonster. The FireMonster has all the attributes and methods of a Monster but the FireMonster’s attack is different from that of a Monster. Therefore, the attack() method must be implemented differently for the FireMonster. The FireMonster class can derive from the  Monster class and override the attack() function using the new keyword. The new keyword before the attack() function in the derived class tells the compiler that we are intentionally overriding the attack() function in the base class.

One must ask, how can we pass an object of type  FireMonster as argument to our  activate() function when the function declares a parameter of type  Monster? This is one aspect of polymorphism in action. The class  FireMonster derives from the Monster class which means that a FireMonster ‘is a’ Monster and can be treated as such when passed to a function as argument. Similarly, if we create a collection (such as a list) of type  Monster, the list can contain objects of any classes derived from Monster.

The more important questions is: when a FireMonster is passed to the  activate() function, does the activate() function call  Monster.attack() method or  FireMonster.attack() method? When we pass an argument of type FireMonster, the run-time type of the monsta parameter is  FireMonster. However, since it’s declared type in function definition is Monster, the  attack() method in the Monster class is called as evident from the output.



This is obviously not what we required in our example. We can also tell C# to use the run-time type of the monsta parameter, instead of using the declared type, to decide which method to call. This ability to decide which method to call at run-time is the second aspect of polymorphism.

Polymorphism using virtual methods

We define the  attack() function in the base class using the  virtual keyword. This tells C# that this method might be overridden in the derived classes and the run-time type of an object should be used to decide which version of the method to call. Consequently, when an object of type FireMonster is passed to the  activate() function, the  attack() method in the  FireMonster class is used.


Output: Using virtual and override keywords

Polymorphism in Python

Python uses the run-time type of the parameter monsta by default and therefore does not require a keyword analogous to virtual in C#.

< Pass by value, pass by reference          File handling >

You may also like: