Friday 10 April 2015

Polymorphism and types of it in c#?


 This blog we discuss what is polymorphism and types of polymorphism c# support.
Polymorphism:-
Polymorphism divided two parts ploy means multiple and morph means forms .So
Polymorphism means many forms. Here in c# it means one name many forms. Essentially it capability of one object to behave in multiple ways. It achieved in c# two ways.
Types of polymorphism:-
 1:-Compile time polymorphism (static binding)
2:- Run time polymorphism (dynamic binding)
Compile time polymorphism:-
 Compile time polymorphism is achieved in C# using concept of overloading. As we   know, with overloading, multiple methods with same name are created. However the parameters on each method vary.
 Example:-
Using System;
Public class myclass
{
Public int sum(int A, int B)
{
Return A + B;
}
Public float Sum(int A, float B)
{
Return A + B;
}
}
The above example Sum method used with different parameters. First it taken int A , int B and in the second method it taken  int A,float B. So it is a perfect example of overloading in c #.
Runtime Polymorphism:-
 Runtime polymorphism is also called as Dynamic polymorphism. This type of polymorphism achieved in c# using overridden concept. Overridden means same and same parameter or signature but different in the implementation. During run time method overriding can be achieved by using inheritance principle and using “Virtual” and “Override” keyword. So this type of polymorphism can also be called as late binding.
  Example of runtime polymorphism:-
Using system;
 Class maruthi
{
Public virtual void Display()
{
Console.writeline(“maruthi car”);
}
}
Class Esteem : Maruthi
{
Public override void Display()
{
Console.writeline(“Maruthi esteem”);
}
}
class testcall
{
Public static void Main()
{
Maruthi m = new Maruthi();
m = new Esteem();
m.Display();
}
}

No comments:

Post a Comment