Polymorphism in C# with Real-Time Examples
In this article, I am going to discuss Polymorphism in C# with Real-Time Examples. Please read our previous where we discussed Abstract Class and Abstract Methods in C# with Examples. At the end of this article, you will understand the following polymorphism pointers in detail.
- What is Polymorphism?
- Why do we need Polymorphism?
- Types of Polymorphism in C#?
- What is Compile-Time Polymorphism?
- What is Runtime Polymorphism?
Note: Polymorphism is one of the primary pillars of object-oriented programming.
What is Polymorphism in C#?
The word polymorphism is derived from the Greek word, where Poly means many and morph means faces/ behaviors. So, the word polymorphism means the ability to take more than one form. Please have a look at the following diagram. As shown in the below diagram, the vehicle is something that has various forms; two-wheeler, three-wheeler, and four-wheeler, and so on. So this is one example of polymorphism.
Technically we can say that when a function shows different behaviors when we passed different types and numbers of input values, then it is called Polymorphism in C#. So behaving in different ways depending on the input received is known as polymorphism i.e. whenever the input changes, automatically the output or the behavior also changes.
Types of Polymorphism in C#
There are two types of polymorphism in C#
- Static Polymorphism / Compile-Time Polymorphism / Early Binding
- Dynamic Polymorphism / Run-Time Polymorphism / Late Binding
The following diagram shows different types of polymorphisms in C# with their examples.
The polymorphism in C# can be implemented using the following three ways.
Note: While working with Polymorphism in C# we need to understand two things i.e. what happens at the time of compilation and what happens at the time of execution for a method call. Is the method going to be executed from the same class at run-time which is bounded to the class at the compile-time or is the method going to be executed from a different class at run-time rather than the class bounded at compile time? Let us proceed and see the answer.
What is Compile-Time Polymorphism in C#?
The function call bounded to the class at the time of compilation, if the function is going to be executed from the same bounded class at run-time, then it is called Compile-Time Polymorphism in C#. This happens in the case of Method Overloading because, in case of overloading each method will have a different signature, and based on the method call, we can easily recognize the method which matches the method signature.
It is also called Static Polymorphism or Early Binding as at the Compilation time we will be able to know from which class the method going to be executed.
What is Runtime Polymorphism in C#?
The function call bounded to the class at the time of compilation, if the function is going to be executed from a different class (Parent Class) at run-time rather than the class bounded at compilation-time, then it is called Run-Time Polymorphism. This happens in the case of Method Overriding because, in the case of Overriding, we have multiple methods with the same signature i.e. Parent Class and the Child class having the same method implementation. So, in this case, we will be able to know at runtime from which class the method is going to be executed.
It is also called Dynamic Polymorphism or Late Binding as at Run-time we will be able to know from which class the method is going to be executed.
In the next article, I am going to discuss Function Overloading in C# with real-time examples. Here, in this article, I try to explain what exactly Polymorphism in C# and their types and when to use Polymorphism in real-time application.