Header Ads

What is polymorphism?

Generally, it is the ability to appear in different forms. In oops concept, it is the ability to process objects differently depending on their data types. Its the ability to redefine methods for derived classes.
There Are Two Types of Polymorphism

1.Static Polymorphism
2.Dynamic Polymorphism

1.Static Polymorphism :

In Static Polymorphism ,Which method is to be called is decided at compile-time only.
Method Overloading is an example of Static Polymorphism.
Method overloading is a concept where we use the same method name many times in the same class,but different parameters. Depending on the parameters we pass, it is decided at compile-time only, which method is to calles. The same method name with the same parameters is an error and it is a case of duplication of methods which c# does not permits.

*****In Static Polymorphism decision is taken at compile time.


Public Class StaticPolyDemo
{
Public void display(int x)
{
Console.WriteLine(“Area of a Square:”+x*x);
}
Public void display(int x, int y)
{
Console.WriteLine(“Area of a Square:”+x*y);
}
Public static void main(String args[])
{
StaticPolyDemo spd=new StaticPolyDemo();
Spd.display(5);
Spd.display(10,3);
}
}



2.Dyanamic Polymorphism :

In this Mechanism by which a call to an overridden function is resolved at a Run-Time( not at Compile-time). If a BaseClass contains a method that is overridden


Class Test
{
Public void show()
{
Console.WriteLine(“From base class show method”);
}
Public Class DynamicPolyDemo : Test
{
Public void show()
{
Console.WriteLine(“From Derived Class show method”);
}
Public static void main(String args[])
{
DynamicPolyDemo dpd=new DynamicPolyDemo();
Dpd.show();

}
}
}

No comments:


Powered by Blogger.