Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 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
Introduction:

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

Description:


Polymorphism

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)

Example


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

Example


//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();
objDc.Sample1();
// calling the base class method
Bclass objBc = new DClass();
objBc.Sample1();
}
}
If we run above code we will get output like as shown below

Output

----------------------------------
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 Asp.net, C#.net, 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

38 comments :

Unknown said...

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

AlexssandroLima said...

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

Anonymous 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.

Thanks,
Ajay

Unknown 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

Unknown 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"

Unknown said...

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

Vipul 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...

tyutii

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

Unknown 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

Unknown 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

VISHNU P K said...

thanks .....

BTechWithLaw said...

public class clsOverRiding
{
//virtual
public void GetData()
{
Console.WriteLine("Base Class OverLoading...............!");
}
}
public class ChildOverRiding : clsOverRiding
{
//override
public void GetData()
{
Console.WriteLine("Child Class OverLoading...............!");
}
}

class Program
{

static void Main(string[] args)
{
ChildOverRiding cd= new ChildOverRiding();
cd.GetData();
clsOverRiding BS = new ChildOverRiding();
BS.GetData();
Console.ReadKey();
}
}

BTechWithLaw said...

public class clsOverRiding
{
//virtual
public virtual void GetData()
{
Console.WriteLine("Base Class OverLoading...............!");
}
}
public class ChildOverRiding : clsOverRiding
{
//override
public override void GetData()
{
Console.WriteLine("Child Class OverLoading...............!");
}
}

class Program
{

static void Main(string[] args)
{
ChildOverRiding cd= new ChildOverRiding();
cd.GetData();
clsOverRiding BS = new ChildOverRiding();
BS.GetData();
Console.ReadKey();
}
}

Unknown said...

its really simple to understand.

Anonymous said...

Nice Post.

Anonymous said...

Thank u sir...
I have run your program . This output is absolutely correct.
And it is advise to others not to comment as given output is wrong.

Unknown said...

very superficial

Unknown said...

Good example. Thanks.

Unknown said...

The above output is 100 % correct . If U want to know the actual result u can run using visual studio. try that .

Anonymous said...

but u did not explained what is early and late binding..

Unknown said...

thank you its really understandable...........

Rutvij said...

yes,its correct solution and easy to understand.thank you

derived
base

Unknown said...

yes, output is correct

Akki.7 said...

1st question.
-------------------------------------
DClass objDc = new DClass();
objDc.Sample1();
//Derived Class is called, we can call derived class by using this method.


What is the use of calling derived class's method using base class object.??
// calling the base class method
Bclass objBc = new DClass();
objBc.Sample1();
---------------------------------------
2nd Question

Without inheriting the we can not implement the polymorphism,, ryt?

Unknown said...

can any one explain why polymorphism required.what is needs?

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

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