Polymorphism Real time Example in C# Updated 2020

In this article I would like to explain about Polymorphism with real time example, before we see that, first we will look into Polymorphism definition and types of Polymorphism.

Polymorphism Definition or What is Polymorphism

“Poly” means many
“Morphs” means forms
So the term “Polymorphism” is the combination of “poly” and “morphs” which means many forms.

Polymorphism is the ability of an entity to behave differently in different situations.

Polymorphism Real time Example

Polymorphism real time example

In the above example you can see some of the forms of Shape i.e. Circle, Rectange and Triangle. This is called Polymorphism.

Polymorphism in OOPS

Polymorphism is one of the primary of object oriented programming.

It is the ability of a variable, function or object to take on multiple forms. In a programming language exhibiting polymorphism, class objects belonging to the same hierarchical tree (inherited from a common parent class) may have functions with the same name, but with different behaviors.

Polymorphism allows you to invoke derived class methods through a base class reference during runtime.

In the base class the method is declared virtual, and in the derived class we override the same method.

The virtual keyword indicates, the method can be overridden in any derived class.

Why we use Polymorphism in C#

Why we need Polymorphism we will see with an example. First I would like to give an example without using Polymorphism and we will see the problems behind this approach and we will see how to resolve those problems using Polymorphism.

example without polymorphism

The above example has a problem even it is a classic example. Let’s see what is the problem with this code. The problem is that next time we decide to add support for a different type of shape let’s say a triangle then we have to go there to shape type enumeration add a new member triangle and then we have to go and modify the Draw class and there we are going to have another case statement to draw triangle.

The problem is every time we define a new shape type the DrawShapes method is going to be changed and as a result the class that contains this method has to be recompiled and redeployed which means any class in the application that is dependent on this class also needs to be recompiled and redeployed. Same for the ShapeType every time we modify this enumeration and add a new member any classes that is referencing this agian is going to berecompiled.

So there is a lot of tight coupling here. And there is also another problem, take a look at the Draw class, imagine if you’re going to have support for 10 different types of shapes then DrawShapes method is going to get really really fat. Well you may argue that we can refactor some of this logic here into separte methods but still the parent method here DrawShapes is going to be fact and complex.

Now the reason we have this problem with this code is because the Shape class itself has only properties, it doesn’t have any behavior it doesn’t have any methods like the Encapsulation principle of object oriented programming.

We want to encapsulate data and behavior or methods in the same unit that’s a class. Application design or develop this way like the one you see here have data somewhere whereas behavior is somewhere else. In this example data is in Shape class but behavior is inside Draw class method, so we need to encapsulate them into one unit. In the below coming section I will explain a better way to this an application using Polymorphism.

Polymorphism provides following features:

  • It allows you to invoke methods of derived class through base class reference during runtime.
  • It has the ability for classes to provide different implementations of methods that are called through the same name.

How to achieve Polymorphism in C#

Using Virtual methods we can achieve Polymorphism. We can override the behavior of child classes using virtual methods. If you don’t use virtual methods all you have are types that inherit what is defined in other types, but can’t replace these inherited behaviors with new ones.

Compile time polymorphism can be achieved by using method overloading and the run time polymorphism can be achieved by using method overriding.

Types of Polymorphism in C#

There are two types of Polymorphism in C#:

  1. Static or Compile time Polymorphism
  2. Dynamic or Runtime Polymorphism
polymorphism types

Static Polymorphism or Compile time Polymorphism or Method Overloading

It is also known as Early Binding. Method overloading is an example of Static Polymorphism.

Function overloading and Method overloading terms are used interchangeably.

Method overloading allows a class to have multiple methods with the same name, but, with a different signature. So, in C# functions can be overloaded based on the number, type(int, float etc) and kind(Value, Ref or Out) of parameters.

The signature of a method consists of the name of the method and the type, kind(value, reference, or output) and the number of its formal parameters. The signature of a method does not include the return type and the params modifier. So, it is not possible to overload a function, just based on the return type or params modifier.

Method Overloading Example

method overloading example

Dynamic Polymorphism or Runtime Polymorphism or Method Overriding

It is also known as Late binding. Method overriding is an example of dynamic polymorphism.

A base class reference variable pointing to a child class object, will invoke the overridden Method of a Child class is called Method Overriding.

C# provides an option to override the base class method, by adding the virtual keyword to the method inside the base class, and by using the override keyword for each derived class methods.

Method Overriding basically modifying the implementation of an inherited method.

It can be done using inheritance.

Method Overriding Syntax

method overriding syntax

Method Overriding Example

method overriding example

Polymorphism Real time Example in C#

Above in one of the section we have discussed about the problems we faced while not using Polymorphism. Now I will give the solution for above discussed problems with Polymorphism real time example.

polymorphism real time example in c#

You may also like...

Leave a Reply

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