aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Types of Polymorphism in C#.Net with Example | Basic Polymorphism in C#.NET

Sep 18, 2013

Here I will explain what is polymorphism in with example and different types of polymorphism (compile time & runtime polymorphism) in with example. 



Polymorphism means many forms (ability to take more than one form). In Polymorphism poly means “multiple” and morph means “forms” so polymorphism means many forms.

In polymorphism we will declare methods with same name and different parameters in same class or methods with same name and same parameters in different classes. Polymorphism has ability to provide different implementation of methods that are implemented with same name.

In Polymorphism we have 2 different types those are

        -   Compile Time Polymorphism (Called as Early Binding or Overloading or static binding)

        -   Run Time Polymorphism (Called as Late Binding or Overriding or dynamic binding)

Compile Time Polymorphism

Compile time polymorphism means we will declare methods with same name but different signatures because of this we will perform different tasks with same method name. This compile time polymorphism also called as early binding or method overloading.

Method Overloading or compile time polymorphism means same method names with different signatures (different parameters)


public class Class1
public void NumbersAdd(int a, int b)
Console.WriteLine(a + b);
public void NumbersAdd(int a, int b, int c)
Console.WriteLine(a + b + c);
In above class we have two methods with same name but having different input parameters this is called method overloading or compile time polymorphism or early binding. 

Run Time Polymorphism

Run time polymorphism also called as late binding or method overriding or dynamic polymorphism. Run time polymorphism or method overriding means same method names with same signatures.

In this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using “virtual & override” keywords.

In base class if we declare methods with virtual keyword then only we can override those methods in derived class using override keyword


//Base Class
public class Bclass
public virtual void Sample1()
Console.WriteLine("Base Class");
// Derived Class
public class DClass : Bclass
public override void Sample1()
Console.WriteLine("Derived Class");
// Using base and derived class
class Program
static void Main(string[] args)
// calling the overriden method
DClass objDc = new DClass();
// calling the base class method
Bclass objBc = new DClass();
If we run above code we will get output like as shown below


Derived Class
Derived Class

In this way we can implement polymorphism concept in our applications.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


udit saxena said...

But sir fr the first time output shold b derived class and thn base class if nt thn why??

Alexssandrolima Dutra said...

Muito bom seus exemplos, gostei. obrigado por compartilhar conosco seus conhecimentos....

chandana said...

pls give us an simple example program for operator overloading..

Anonymous said...

Hi sir,

Please explain why compiler unable to detect in case of run time polymorphism.


Santosh A said...

Nice Explination Suresh.
But the output the above program is wrong.

Pankaj Sharma said...

Could u tell me what is correct output?

Pankaj Sharma said...

No this suresh answer is perfectly right...both the time derived method will call

Tez Wingfield said...

Please format your code correctly, indentations!

Your code is incorrect full stop (within the example your trying to give)

This is wrong, the true essence of run time method polymorphism is to override i'ts base class method.

So when instantiating the base class and calling the Sample1() method, the Console will write "Base Class" but what if you want to override that method in a Derived Class? We use polymorphism, which then the Console will write "Derived Class" if you have instantiated the derived class and called Sample1()

Output should be:

"Base Class"
"Derived Class"

Rakesh Tripathi said...

Output is correct.
Thanks Suresh for such a nice article.......

vipul sachan said...

Above output is correct. If you check Bclass instance is created by Dclass only. So it will call the same method again.

ery said...


Suresh Dasari said...

Guys Output is 100 % correct Override means override base class method in child class that's the reason output returning only child class method

Varun Nayak said...

Here out Put Is correct as per Above Code,
The Reason Is:
Focus When Here We create Object Of Base Class,
Bclass objBc = new DClass();
If we Create Base Class Object as We Create Normally Like,
Bclass objBc = new Bclass ();
Then the Output Will be,
Derived Class
BASE Class

Anonymous said...

Yes Correct

Shariful Islam said...

The information was very much helpful .. thanks ...

Anonymous said...

But why would you use it? If you are not using the first Sample1() in the base class, why have it.

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Thanks buddy

Anonymous said...

The type you have explained are types of Ad-Hoc polymorphism....

polymorphism is actually is of 4 type

Basavaraj said...

Yes this is correct and excellent example...

prami said...

Simple explanation ..

Abhijit JD said...

output should be
Derived Class
Derived Class

Anonymous said...

output should be
Derived Class
Base Class

Anonymous said...

Thanks for sharing

Vishnu P.k said...

thanks .....

Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.