What is Polymorphism
Polymorphism is one of the principle of object oriented programming. "Poly" means many and "morph" means forms hence the name polymorphism. Polymorphism also refered to as one name many forms or having one name with multiple functionality.In simple words you can use same method name with different signature or same signature but in different class.So depending on a data type it processes objects differently and an ability to redefine methods for a derived classes.
What are types of Polymorphism
There are basically two types of polymorphism in c# i.e.Static Polymorphism
Dynamic Polymorphism
Static Polymorphism
Static polymorphism is also called as Compile Time polymorphism. In Static polymorphism methods are overloaded with same name but having different signatures.So it is called as method overloading.Example of Static Polymorphism
|
|
First function is with 2 parameters and second function having 3 parameters. So this type of polymorphism is also known as method overloading.
It is a compile time polymorphism because compiler already knows what type object it is linking to, what are the methods it is going to call it. So linking a method during a compile time also called as Early binding.
Dynamic Polymorphism
Dynamic polymorphism is also called as Run Time polymorphism. In this type of polymorphism methods have the same name, same signature but different in the implementation.
In Dynamic polymorphism methods are overridden so it also called as method overriding.
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.
In late binding compiler doesn't know what kind of methods it has to call and which can be achieved only during the run time.so it is called as run time polymorphism.
Example of Dynamic Polymorphism
public class clsShape |
|
So that you have understood about Polymorphism, Static Polymorphism and Dynamic Polymorphism. Now let's see a simple example of polymorphism.
Simple example of polymorphism
In this example we will illustrate an example of dynamic polymorphism.we will take up above the example and try to implement it.
public class clsShape
{
public int _radius = 5;
public virtual double getArea()
{
return 0;
}
}
public class clsCircle : clsShape
{
public override double getArea()
{
return 3.14 * _radius * _radius;
}
}
public class clsSphere : clsShape
{
public override double getArea()
{
return 4 * 3.14 * _radius * _radius;
}
}
Now to see the output we will create "Console Application" and try to see the result.
Step 1 - Creating Console Application
class Program |
In this step we will create base class object and assign it to derived classes.
class Program |
Step 3 - Displaying the Result
class Program |
Radius of a Cirle is - 78.5
Radius of a Sphere is - 314
So this how to implement polymorphism using c sharp. if you have any doubts or query let me know through your comment. Happy Coding.
Now over to you:
"A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work, you can appreciate by leaving your comments. Stay tuned and stay connected for more technical updates."
No comments:
Post a Comment