What is Polymorphism in C# is a good question. The term polymorphism is one of the pillars of Object-Oriented Programming Concepts. There are other concepts like Encapsulation, Abstraction, Inheritance. Before we start talking about types of polymorphism, let’s talk about what is Polymorphism in general.
What is Polymorphism in C#
Polymorphism is a Greek word which is consists of two words Poly and Morphism. The word Poly means many and Morphism means forms. Polymorphism means many forms. As we discussed in one of the previous articles, Object-Oriented Programming is similar to nature. Polymorphism is also close to nature because everything around us is playing more than one role.
Let’s take the example of your friend. I know, not everybody has real friends. But everybody has friends, you know what I mean ?.
Look at your friend, He is a friend, He is a son. He is a student. He is a lover. At the same time, he is a human being.
Friend as a Friend
Whenever you call him home, he comes and performs the actions of your friend. Like partying together and drinking together and stuff. Sometimes helps you with your problems if he is a real friend (Which is rare nowadays cause I don’t have one ?).
Friend as a Student
Whenever he talks to a teacher he is performing the actions of a student. He talks about school/college projects. He talks about the subjects.
Friend as a Son
Whenever he is home, he performs the actions of a son like doing home chores like buying groceries, doing laundry, cleaning the house, and stuff.
Friend as a Lover
Whenever he is with his girl, he takes her on a date, buys her gifts. Cares for her. Helps her with her life problems.
Friend as a Human Being
Irrespective of all of his roles, there are some tasks that he performs in all roles. Like eating, breathing, speaking etcetera. These are all tasks of being a living human.
Polymorphism in Programming
The concept of Polymorphism in programming is used when we want to achieve the below-mentioned things.
- A Single Class Performs the same tasks in different ways.
- A Single Class Performs different tasks.
A Single Class Performs the same task in different ways
Suppose you have created a class that has the add method in it. This add method adds two numbers. So this method is a part of this class and the adding functionality belongs to the class. We can say the class is designed to add two numbers.
What if the user gave 3 numbers? The class will give an error because it is created to add only two numbers. Now we need to provide another definition of the Add method that definition will prepare the class to take three numbers and add them. You don’t need to tell the class to use a 2 number definition of 3 number definition. The class will choose the right definition based on how many numbers it is receiving from the user and choose the correct definition accordingly. This is how a single class will perform the same addition task in different ways. The programmer is free to provide as many definitions as he wants for the add method.
Do not worry, We will discuss method overloading in a different article with program examples because that requires some code example.
A Single Class Performs different tasks
When we want a class to perform different tasks, we use the concept of inheritance. In Inheritance, we define the relationship between two classes. In case you have no idea about inheritance, click on the hyperlink and read the article, I have already explained the topic in detail. Kindly read about it.
Inheritance in short is a way of transferring the non-private methods and properties from one class to another class.
We manipulate the roles of a class and force it to run parent class functions or child class functions.
We will learn about it in a future article with program examples.
I hope till this point in the article you have understood What is Polymorphism in C#. Let talk about the types of polymorphism and how to implement polymorphism in C# code.
Types of Polymorphism
- Static Polymorphism (Compiletime Polymorphism)
- Dynamic Polymorphism (Runtime Polymorphism)
When we link an object to a function at the compile time, this is called Static Polymorphism. Another name for this type of Polymorphism is early binding.
There are two ways to implement Static Polymorphism.
- Method Overloading
- Operator Overloading
Dynamic Polymorphism happens when we have a parent–child relationship between two classes. Both parent and child have a method with the same name and same signature.
If you create a variable of type child class and hold the instance of child class in it. By default, the child class method will be called. In this situation, the child class method will completely hide the parent class method and run its own method. This process is called method hiding.
The challenge in this situation is to be able to call the method of your choice, whenever you want. You should be using both child and parent class members to your advantage. There are several ways to do it which we will discuss in future articles because this is a bit complicated and I do not want to cover it in short. All you need to understand at this point is, what all the points we will be covering in future articles. Look at the list below.
- Method Hiding.
- Use of Base Keyword in the child-parent relationship.
- Holding child class Instance in parent class variable type.
- Using Virtual and Override keywords.
The answer to what is Polymorphism is a bit complicated, you need to have a lot of patience to understand it. It is used very often when designing virtual reality games like Pubg, America’s Army etcetera. They have a class Gun and the functionality of that class changes when you select a different gun, that is possible because of polymorphism.
I hope till this point you have some good understanding of What is Polymorphism in C#, This article is intended for all of those who had no idea what polymorphism was. Stay tuned because I will be writing more articles on the above-mentioned topics.
Till then Stay Safe Stay Healthy!