Home > DeveloperSection > Blogs > Polymorphism

Polymorphism


.NET .NET 
Ratings:
0 Comment(s)
 1771  View(s)
Rate this:

Polymorphism

Polymorphism means having more than one form. Overloading and overriding are used to implement polymorphism. Polymorphism is classified into compile time polymorphism or early binding or static binding and Runtime polymorphism or late binding or dynamic binding.

Compile time Polymorphism or Early Binding:
The polymorphism in which compiler identifies which polymorphic form it has to execute at compile time itself is called as compile time polymorphism or early binding. 
Advantage of early binding is execution will be fast. Because everything about the method is known to compiler during compilation itself and disadvantage is lack of flexibility.
Examples of early binding are overloaded methods, overloaded operators and overridden methods that are called directly by using derived objects.

Runtime Polymorphism or Late Binding:
The polymorphism in which compiler identifies which polymorphic form to execute at runtime but not at compile time is called as runtime polymorphism or late binding. 
Advantage of late binding is flexibility and disadvantage is execution will be slow as compiler has to get the information about the method to execute at runtime.
Example of late binding is overridden methods that are called using base class object.

Polymorphism Examples:

1.      Method Overloading

2.      Method Overriding

Method Overloading:-

The process of creating more than one method in a class with same name or creating a method in derived class with same name as a method in base class is called as method overloading. While overloading methods, a rule to follow is the overloaded methods must differ either in number of arguments they take or the data type of at least one argument or sequence of parameter.

Example:-

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Polymorphism

{

    class Program

    {

         static void Main(string[] args)

        {

            Method_overloading objOverloading = new Method_overloading();

            Console.WriteLine("Addition of two integers................" + objOverloading.Addition(2, 5));

            Console.WriteLine("Addition of two double type values......" + objOverloading.Addition(0.40f, 0.50f));

            Console.WriteLine("Addition of three integers.............." + objOverloading.Addition(2, 5, 5));

            Console.WriteLine("Addition of three double type values...." + objOverloading.Addition(0.40f, 0.50f, 0.60f));

            Console.ReadLine();

        }

    }

    class Method_overloading

    {

            public int Addition(int a, int b) // function with two parameters

            {

                int x;

                return x=a+b;

            }

            public float Addition(float a, float b) // function with two parameters

            {

                float u;

                return u = a + b;

            }

            public int Addition(int a, int b, int c) // function with three parameters

            {

                int y;

                return y = a + b+ c;

            }                  

            public float Addition(float a, float b, float c) // function with three parameters

            {

               float v;

               return v = a + b+ c;

            }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick