Home > DeveloperSection > Blogs > Polymorphism in C#

Polymorphism in C#


C# C# 
Ratings:
0 Comment(s)
 1667  View(s)
Rate this:

Polymorphism in C#

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;

namespace PolymorphismMethodOverloadingConsoleApplication

{

    class Add

    {

        /// <summary>

        /// addition(int a, int b) to add two numbers

        /// </summary>

        public void addition(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>

        public void addition(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>

        public void addition(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>

        public void addition(double a, double b,double c)

        {

            Console.WriteLine("addition of two numbers :" + (a + b + c));

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Add aobj = 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;

namespace PolymorphismMethodOverridingConsoleApplication

{

    class BaseClass

    {

        /// <summary>

        /// create display() with virtual keyword

        /// </summary>

        public virtual void display()

        {

            Console.WriteLine("Base Class Method");

        }

    }

    class DerivedClass : BaseClass //inherits base class

    {

        /// <summary>

        /// display() with override keyword

        /// </summary>

        public override void display()

        {

            Console.WriteLine("Derived Class Method");

        }

    }

    class Program

    {

     static void Main(string[] args)

        {

            DerivedClass dc = new DerivedClass(); // create object

            dc.display(); // calls the method

        }

    }

}


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

Follow MindStick