Home > DeveloperSection > Blogs > Polymorphism in c#

Polymorphism in c#


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

Polymorphism in c#

in this blog I am trying to explore the concept of Polymorphism in c#.

The word polymorphism means having many forms. In object oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'.

Primary concept of object-oriented programming is Polymorphism. It allows you to invoke derived class methods through a base class reference during run-time. This is handy when you need to assign a group of objects to an array and then invoke each of their methods. They won't necessarily have to be the same object type. Then if they all share the same method name, that method of each object can be invoked.

Polymorphism can be static or dynamic. In static polymorphism the response to a function is determined at the compile time. In dynamic polymorphism it is decided at run time.

Static  Polymorphism

The mechanism of linking a function with an object during compile time is called early binding. It is also called static binding. These are: 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 every thing about the method is known to compiler during compilation itself and disadvantage is lack of flexibility. C# provides two techniques to implement static polymorphism.
These are overloaded methods, overloaded operators and overridden methods that are called directly by using derived objects.

Dynamic Polymorphism

In C# it allows you to create abstract classes that are used to provide partial class implementation of an interface. This is completed when a derived class inherits from it. Abstract classes contain abstract methods which are implemented by the derived class. The derived classes have more specialized functionality, but only he/she wants to do so. However derived class must be define all abstract method of base class abstract.

In dynamic polymorphism the method binding is resolved by the compiler at run time unlike static polymorphism. In dynamic polymorphism the one type (object reference) is work as anonymous type. I.e. this reference is able to call method of derived class with the object reference of base class.

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.

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace polymorphysmExample

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.Clear();

            PolyExample exam = new PolyExample();// creating object of polymorphism class

            Rectangle rect = new Rectangle(20, 10);//creating object of rectangle class

            Triangle trin = new Triangle(10, 5);// creating the object of triangle class

            // here we pass the object of rectangle class, which is

            //actually hold by the base class type

            exam.CallArea(rect);

            //here again we pass the object of triangle class,

            //which is actually hold by the base class type

            exam.CallArea(trin);

            Shape sh = new Shape(5, 8);

            //calling area of it's own method i.e base class calls base method

            int area=sh.area();    

            Console.WriteLine(area);

            // assign the object of rectangle class to base(Shape class)

            //type and calling method of derivd class from base class

            sh =new Rectangle(8,4);

            area=sh.area();

            Console.WriteLine(area);

            // assign the object of triangle class to base(Shape class)

            //type and calling method of derivd class from base class

            sh = new Triangle(2, 8);

            area=sh.area();

            Console.WriteLine(area);          

    // i.e one interface type(shape class object reference) have many form

   //(viz. some time it work as triange class object and some time as rectangle as well)

            abs ob = (a)exam;

            exam.showMessage();

            Console.ReadKey();

        }

        // creating a class shape

        class Shape

        {

            protected int width, height;

            public Shape(int a = 0, int b = 0)

            {

                width = a;

                height = b;

            }

            // using virtual keyword indicate it can be override in derived class

            public virtual int area()

            {

                Console.WriteLine("Parent class area:");

                return 0;

            }

        }

        // derived class rectangle inherite basse class shape

        class Rectangle : Shape

        {

            public Rectangle(int a = 0, int b = 0)//caling base class constructor

                : base(a, b)

            {

            }

            // overridden method area() of base class shape

            public override int area()

            {

                Console.WriteLine("Rectangle class area :");

                return (width * height);

            }

        }

        // derived class triangle inherite basse class shape

        class Triangle : Shape

        {

            public Triangle(int a = 0, int b = 0)//caling base class constructor

                : base(a, b)

            {

            }

            // overridden method area() of base class shape

            public override int area()

            {

                Console.WriteLine("Triangle class area :");

                return (width * height / 2);

            }

        }

        class PolyExample:abs

        {

       //this function have shape class reference as holder to the derived class oject

            public void CallArea(Shape sh)

            {

                int a;

// this calls right method the recieved objet

//type, which actually shows the polymorphism.

                a = sh.area();

                Console.WriteLine("Area: {0}", a);

                Console.ReadKey();

            }

            public override void showMessage()

            {

 Console.WriteLine("hi method has overriden : and it is called from base class boject");

            }

        }

        // abstact class showa how to implement runtime polymorphism

        abstract class abs                           

        {

            public abstract void showMessage();

        }

    }

}


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

Follow MindStick