Polymorphism in C#
Polymorphism is a Greek word that means "one name many forms"
Polymorphism is a Greek word that means "one name many forms"
The word "Poly" means many and "morph"
means forms
In Object oriented programming, there are four pillars.
Polymorphism is one of them.
Please see following article also
What is Encapsulation in C#
What is Inheritance in C#
What is Abstraction in C#
Types of Polymorphism:
Mainly there are two type of polymorphism
1. Static
polymorphism
2. Dynamic
polymorphism
Static Polymorphism
Static
polymorphism is also known as compile time polymorphism or early binding.
The example of static polymorphism is method overloading and
operator overloading
In method overloading method performs the different task at
the different input parameters.
Following is the examples
of methods overloading or static polymorphism:
public int
Add(int a, int b)
{
return a +
b;
}
public float
Add(int a, float b)
{
return a +
b;
}
public float
Add(float a, int b)
{
return a +
b;
}
public int
Add(int a,int b,int c)
{
return a +
b + c;
}
Practical example of Method Overloading (Compile Time
Polymorphism)
using System;
namespace PolymorphismProgram
{
class Program
{
public int
Add(int a, int b)
{
return a +
b;
}
public float
Add(int a, float b)
{
return a +
b;
}
public float
Add(float a, int b)
{
return a +
b;
}
public int
Add(int a, int b, int c)
{
return a +
b + c;
}
static void
Main(string[] args)
{
Program
obj = new Program();
Console.WriteLine("The sum of integer value is= {0}", obj.Add(2, 5));
Console.WriteLine("The sum of integer and float value is= {0}", obj.Add(13, 5.5f));
Console.WriteLine("The sum of float and integer value is= {0}", obj.Add(6.9f, 5));
Console.ReadKey();
}
}
}
Note: In the code if you observe Add method is called three
times. Add method will work according to the number of parameters and type of
parameters.
The use of method overloading
Use method overloading in situation where you want a class
to be able to do something, but there is more than one possibility for what
information is supplied to the method that carries out the task.
In real time scenario, suppose we have one method in business
layer which save data in database after some time we need to pass one more
parameter in method, In this situation instead modify same method we make
another method with same name and add one extra parameter in this method. In
that case there not any impact in our existing application.
Method overloading :-
We can do method overloading mainly four types in C#
1. Number of parameter.
2. Sequence of parameter.
3. Type of parameter.
4. Type of parameter with out keyword.
Dynamic Polymorphism
Dynamic polymorphism is done using inheritance and virtual
functions. Method overriding is called runtime polymorphism. It is also called
late binding or Runtime polymorphism.
Note: Don't confused method overloading with method
overriding, they are different, unrelated concepts. But they sound similar.
Note :- In method overriding, you cannot change method
signatures and method name.
The base class and child class have the same method name and
same signature. At the compile time the compiler does not aware of the method
available for overriding the functionality , so
compiler does not throw an error at compile time. The compiler will
decide which method to call at runtime and if no method is found then it throws
an error.
The complete example of Dynamic polymorphism is shown below
using System;
namespace Test
{
public class
Message
{
public virtual
string Display()
{
return
"I am calling from Message class.";
}
}
class
Program:Message
{
public
override string Display()
{
return
"I am calling from Program class.";
}
static void
Main(string[] args)
{
Message
obj = new Program();
Console.WriteLine(obj.Display());//it will call Program class method
Console.ReadKey();
}
}
}
The compiler requires an Display() method and it compiles
successfully but the right version of the Display() method is not being
determined at compile time but it determined at runtime. Finally the overriding
methods must have the same name and signature (number of parameters and type),
as the virtual method defined in the base class method and that it is
overriding in the derived class.
Conclusion
I hope this would help you in understanding polymorphism.
Thnks!!!
Thnks!!!
No comments:
Post a Comment