Polymorphism in C#

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.

  1. What is Polymorphism?
  2. Why do we need Polymorphism?
  3. Types of Polymorphism in C#?
  4. What is Compile-Time Polymorphism?
  5. 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.

What is 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#

  1. Static Polymorphism / Compile-Time Polymorphism / Early Binding
  2. Dynamic Polymorphism / Run-Time Polymorphism / Late Binding

The following diagram shows different types of polymorphisms in C# with their examples.

Types of Polymorphism in C#

The polymorphism in C# can be implemented using the following three ways.

  1. Function Overloading
  2. Function Overriding
  3. Function Hiding

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.

2 thoughts on “Polymorphism in C#”

  1. blank

    >>> In case of RUNTIME polymorphism for a given method call, we can recognize which method has to be executed exactly at runtime but not in compilation time because in case of overriding and HIDING we have multiple methods with the same signature.

    (From function hiding arcticle)>>>In method HIDING a base class reference variable pointing to a child class object will invoke the hidden method in the BASE class.

    Then method hiding is compile-time polymorphism, isn’t it?

Leave a Reply

Your email address will not be published. Required fields are marked *