blog

Home / DeveloperSection / Blogs / Polymorphism in C#

Polymorphism in C#

Sumit Kesarwani4067 13-May-2013

In this blog, I’m trying to explain the concept of polymorphism with examples of method overloading and method overriding.

Polymorphism is the fundamental concept of OOP. It means one name multiple forms. In object oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'.

Polymorphism provides following features: 

It allows you to invoke methods of derived class through base class reference during runtime.

It has the ability for classes to provide different implementations of methods that are called through the same name

Polymorphism is of two types: 

Compile time polymorphism/Overloading

Runtime polymorphism/Overriding

Compile time Polymorphism or Early Binding
The polymorphism in which compiler identifies which polymorphic form it has to execute at compile time 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 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. 

Practical example of Method Overloading (Compile Time Polymorphism)

using System;
namespacePolymorphismMethodOverloadingConsoleApplication
{
    classAdd
    {
        /// <summary>
        /// addition(int a, int b) to add two numbers
        /// </summary>
        publicvoidaddition(int a, int b)
        {
            Console.WriteLine("addition of two numbers :" +(a+b));
        }
 
        /// <summary>
        /// addition(int a, int b,int c) to add three numbers
        /// </summary>
        publicvoidaddition(int a, int b,int c)
        {
            Console.WriteLine("addition of three numbers :" + (a + b + c));
        }
 
        /// <summary>
        /// addition(double a, double b) to add two numbers with decimals
        /// </summary>
        publicvoidaddition(double a, double b)
        {
            Console.WriteLine("addition of two numbers :" + (a + b));
        }
        /// <summary>
        /// addition(double a, double b,double c)  to add three numbers with decimals
        /// </summary>
        publicvoidaddition(double a, double b,double c)
        {
            Console.WriteLine("addition of two numbers :" + (a + b + c));
        }
    }
    classProgram
    {
        staticvoidMain(string[] args)
        {
            Addaobj = new Add();//create object
            //calls the addition() method with different values
            aobj.addition(4,6);
            aobj.addition(4, 6, 12);
            aobj.addition(2.34, 6.67);
            aobj.addition(3.56, 4.90, 1.56);
        }
    }
}

Practical example of Method Overriding (Run Time Polymorphism)

using System;
namespacePolymorphismMethodOverridingConsoleApplication
{
    classBaseClass
    {
        /// <summary>
        /// create display() with virtual keyword
        /// </summary>
        publicvirtualvoiddisplay()
        {
            Console.WriteLine("Base Class Method");
        }
    }
    classDerivedClass : BaseClass//inherits base class
    {
        /// <summary>
        /// display() with override keyword
        /// </summary>
        publicoverridevoiddisplay()
        {
            Console.WriteLine("Derived Class Method");
        }
    }
    classProgram
    {
     staticvoidMain(string[] args)
        {
            DerivedClassdc = new DerivedClass(); // create object
            dc.display(); // calls the method
        }
    }
}

You can also read these related post

https://www.mindstick.com/Articles/32/polymorphism

https://www.mindstick.com/Blog/18/polymorphism

https://www.mindstick.com/interview/81/what-is-meant-by-polymorphism


Updated 18-Sep-2014

Leave Comment

Comments

Liked By