In this tutorial, we are going to discuss Polymorphism. Polymorphism is a Greek word which means having multiple forms.

After completing this tutorial you will be able to understand:

  • Polymorphism in C#.
  • Method Overloading in C#.
  • Method Overriding in C#.

Introduction:

Polymorphism is the important part of Object-Oriented-Programming. Polymorphism is Greek word which means that “Many-Forms”. With Polymorphism one function can behave in many ways.

Polymorphism

The example of Polymorphism:

  • A Person can be a son in his house, at the same time a person can also be a father.
  • Your Mobile is a good example of Polymorphism (One Name many forms). You can use your Mobile phone in different ways like:

   1-  As a Phone.

2-  As a Camera.

 3- As a Mp3 Player.

 4- As a Radio.

There are two types of Polymorphism in C#.

Compile time polymorphism or early binding:

In Compile time polymorphism which method is to be executed is decided at compile-time only.

Since in compile time polymorphism compiler specifies that which method is to be executed by itself that is why it is also called as early binding.

Method Overloading is an example of Compile time Polymorphism.

Let’s Create an example of Method Overloading in C#.

An example of Compile Time Polymorphism or Early Binding:

Open Visual Studio and create a Console Application project in C# called MethodOverLoadingDemo.

Polymorphism

Open Program.cs file and declare following methods:

 public void DisplayName(string sName)
 {
 Console.WriteLine("Your Name is "+ sName);
 }
 public void DisplayName(string sFirstName,string sLastName)
 {
 Console.WriteLine("Your Name is "+ sFirstName+" "+sLastName);
 }
 public void DisplayName(string sFirstName,string sMiddleName, string sLastName)
 {
 Console.WriteLine("Your Name is "+ sFirstName+" "+sMiddleName+" "+ sLastName);

 }

create an object of Program.cs file and call the above-declared methods.

Program objProgram = new Program();
 objProgram.DisplayName("Arvind Singh");
 objProgram.DisplayName("Arvind", "Singh");
 objProgram.DisplayName("Arvind", "Kumar", "Singh");

Notice the below snapshot:

Polymorphism

You can see in the above snapshot there are +2 overloaded methods to call with different parameters.

Complete Code for the above example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodOverLoadingDemo
{
 class Program
 {
 public void DisplayName(string sName)
 {
 Console.WriteLine("Your Name is "+ sName);
 }
 public void DisplayName(string sFirstName,string sLastName)
 {
 Console.WriteLine("Your Name is "+ sFirstName+" "+sLastName);
 }
 public void DisplayName(string sFirstName,string sMiddleName, string sLastName)
 {
 Console.WriteLine("Your Name is "+ sFirstName+" "+sMiddleName+" "+ sLastName);
 }
 static void Main(string[] args)
 {
 Program objProgram = new Program();
 objProgram.DisplayName("Arvind Singh");
 objProgram.DisplayName("Arvind", "Singh");
 objProgram.DisplayName("Arvind", "Kumar", "Singh");
 Console.ReadKey();
 }

}
}

Run the application and look at the output screen:

Polymorphism

You can download the complete source code for the application from here.

Advantages and Disadvantages of Compile Time Polymorphism:

Since in Compile time Polymorphism everything about the method is known to the compiler so the execution will be fast.The disadvantage of compile time polymorphism is a

The disadvantage of compile time polymorphism is a lack of flexibility.

Runtime Polymorphism or Late Binding:

Runtime polymorphism is achieved using inheritance or virtual functions. In Runtime polymorphism compiler specifies that which method is to be executed is decided at runtime, not at compile-time. Method Overriding is an example of Runtime Polymorphism or Late Binding.

Advantages and Disadvantages of Runtime Polymorphism:

The Advantage of Runtime Polymorphism is its flexibility and the disadvantage of it is slow performance because the compiler knows which method is to be executed by itself at runtime, not at compile-time.

An example of Runtime Polymorphism:

Open Visual Studio and create a Console Application Project in C# MethodOverridingDemo.

Polymorphism

Open the Program.cs file and write the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodOverridingDemo
{
 class TechnologyBase
 {
 public virtual void MyCurrentTechnology()
 {
 
 Console.WriteLine("Base Class Method: I am Working on .Net");
 }
 }

class TechnologyDerived : TechnologyBase
 {
 public override void MyCurrentTechnology()
 {
 Console.WriteLine("Derived Class Method: Now, I am Working on .Net");
 }
 }
 class Program
 {
 
 static void Main(string[] args)
 {
 TechnologyBase objTechnologyBase;
 objTechnologyBase = new TechnologyBase();
 objTechnologyBase.MyCurrentTechnology();
 objTechnologyBase = new TechnologyDerived();
 objTechnologyBase.MyCurrentTechnology();
 Console.ReadKey();
 }
 }
}

Run the application and look at the output screen:

Polymorphism

 

You can download the complete source code for this example from here.

Conclusion:

Hope you understand the concept of Polymorphism in C#. We will create more example on Method Overloading and Method Overriding in later sessions.

View More:

Thank You.

 

Leave a Comment