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:
In
previous posts I explained OOPS examples in c#, difference b/w array and arraylist in
c#,
difference b/w constant and readonly in
c#,
difference b/w view and stored procedure
in sql,
difference between wcf and web
application,
interview questions in asp.net, sql
server, c#
and many articles relating to interview questions in c#,
asp.net,
sql server, javascript,
jquery. Now I will explain what
is polymorphism in c#.net with example and
different types of polymorphism in c#.net with example.
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. |
|||
|
|||
38 comments :
But sir fr the first time output shold b derived class and thn base class if nt thn why??
Muito bom seus exemplos, gostei. obrigado por compartilhar conosco seus conhecimentos....
pls give us an simple example program for operator overloading..
Hi sir,
Please explain why compiler unable to detect in case of run time polymorphism.
Thanks,
Ajay
Nice Explination Suresh.
But the output the above program is wrong.
Could u tell me what is correct output?
No this suresh answer is perfectly right...both the time derived method will call
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"
Output is correct.
Thanks Suresh for such a nice article.......
Above output is correct. If you check Bclass instance is created by Dclass only. So it will call the same method again.
tyutii
Guys Output is 100 % correct Override means override base class method in child class that's the reason output returning only child class method
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
Yes Correct
The information was very much helpful .. thanks ...
But why would you use it? If you are not using the first Sample1() in the base class, why have it.
Thanks buddy
The type you have explained are types of Ad-Hoc polymorphism....
polymorphism is actually is of 4 type
Yes this is correct and excellent example...
Simple explanation ..
output should be
Derived Class
Derived Class
output should be
Derived Class
Base Class
thanks .....
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();
}
}
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();
}
}
its really simple to understand.
Nice Post.
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.
very superficial
Good example. Thanks.
The above output is 100 % correct . If U want to know the actual result u can run using visual studio. try that .
but u did not explained what is early and late binding..
thank you its really understandable...........
yes,its correct solution and easy to understand.thank you
derived
base
yes, output is correct
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?
can any one explain why polymorphism required.what is needs?
Note: Only a member of this blog may post a comment.