1. OnlineBuff

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.

Click here to know more How to Implement Inheritance in C#.

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 using c sharp


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.

Author: Gurunatha Dogi

is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more. Follow me @ and twitter.... read more

Comments

64x64
By Mahesh on 2016-04-19
class Program { static void Main(string[] args) { clsShape objShape2 = new clsShape(); clsShape objShape3 = new clsShape(); clsCircle objShape4 = new clsCircle(); clsSphere objShape5= new clsSphere(); } } when to create above object i'm bit in confusion.please help me
64x64
By Muraly on 2014-12-01
what is the difference between static and dynamic overloading.
64x64
By Ramadile on 2014-07-15
why create base class object and assign it to derived classes not the other way round?
64x64
By THEJA BODDU on 2014-06-29
excellent gurunatha
64x64
By BRAJESH on 2014-05-29
Nice...... My all concept clear thanks..
64x64
By Behzad Qureshi on 2014-05-15
Cool! Got what I was searching for. Very nicely explained. Kindly share link of delegates too what same type of explanation.
64x64
By Stephen bentil on 2014-05-08
Good explanations
64x64
By Vijay.N on 2014-04-24
Good Explanations
64x64
By Naveed on 2013-11-18
nice explaination...
64x64
By Daniel on 2013-10-17
Good explanation & examples. Thanks
64x64
By Harshal on 2013-10-08
awsm man nice tuts
64x64
By Bindu on 2013-09-19
very nice........ i have a small doubt that is concepts like (encapsulation,polymorphm and all other) in java(opp) will have same definition and same procedure as in like c,c#,c++.PLEASE explain briefly and with easy example. THANK U........
64x64
By Thiru on 2013-09-11
Hi, These many i know what is polymorphism and its types and how to use it... but for the very first time i know what exactly it means compile time poly and run time poly.. well explained with simple example... thanks for the post...
64x64
By Rohit on 2013-09-02
can we override the method in same class it is defined
64x64
By Rashmi Singh on 2013-08-17
sir i have read polymorphism in many time but i am totally confused ican not understood it properly. and forget it . so please can you tell me any simple way to understand it. thank you.
64x64
By Nirmala on 2013-07-18
Could you please an article on "Delegates and Events" in C# with suitable example. It will be very helpful. Thanks in advance
64x64
By Syed on 2013-06-04
please send me about polymorphism

Add a Comment