Friday, February 27, 2015

Polymorphism in C# using an example


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


 
public class clsCalculation
{

public int Add(int a, int b)
{
return a + b;
}

public double Add(int z, int x, int c)
{
return z + x + c;
}

}
In above code we have a class "clsCalculation" having two functions with same name "Add" but having different input parameters.
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
{
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;
}

}



As you can see from above code that we have one base class "clsShape" with a "virtual" method "getArea()" and two derived classes "clsCircle" and "clsShape" each having an "override" method "getArea()" with different implementations.


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;
}

}

So as you know that we have one base class  "clsShape" with a "virtual" method "getArea()" and two derived classes "clsCircle" and "clsShape" each having an "override" method "getArea()" but with different implementation.

Now to see the output we will create "Console Application" and try to see the result.
Step 1 - Creating Console Application
class Program
{
static void Main(string[] args)
{

//code goes here
}
}

Step 2 - Creating Base Class Object
In this step we will create base class object and assign it to derived classes.
class Program
{
static void Main(string[] args)
{

clsShape objShape1 = new clsCircle();
clsShape objShape2 = new clsSphere();

}
}

So as you see from above code that we have created two objects "objShape1" for "clsCircle" and "objShape2" for "clsSphere" respectively.

Step 3 - Displaying the Result
class Program
{
static void Main(string[] args)
{

clsShape objShape1 = new clsCircle();
clsShape objShape2 = new clsSphere();
Console.WriteLine("Radius of a Cirle is - {0}", objShape1.getArea());
Console.WriteLine("Radius of a Sphere is - {0}", objShape2.getArea());

}
}

Step 4 - Output

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