POLYMORPHISM

Polymorphism is a way of having the same name with different forms. The best example would be a Console.WriteLine function. With it, you can write a string or an int or a float to the

Polymorphism is a way of having the same name with different forms. The best example would be a Console.WriteLine function. With it, you can write a string or an int or a float to the console. So it has the same name but different forms.

Polymorphism are of 2 types:

  1. Static Polymorphism aka compile Time Polymorphism aka Early Binding: It is achieved using Function or Operator Overloading.
  2. Dynamic Polymorphism aka RunTime Polymorphism aka Late Binding: It is achieved using Function Overriding.

Static Polymorphism: Function Overloading

using System;

namespace PolymorphismUnderstanding
{
    class variableOutput
    {
        public int var1 { get; set; }
        public double var2 { get; set; }

        public void printoutput(int i)
        {
            var1 = i;
            Console.WriteLine("value of var1: {0}",var1);
        }
        public void printoutput(double i) //same name different argument
        {
            var2 = i;
            Console.WriteLine("value of var2: {0}", var2);
        }
    }
    
    class Program
    { 
        static void Main(string[] args)
        {
            variableOutput var = new variableOutput();
            var.printoutput(5);
            var.printoutput(0.99);

            Console.ReadKey();
        }
    }
}

Static Polymorphism: Operator Overloading

using System;

namespace PolymorphismUnderstanding
{
    class ObjectClass
    {
        public int var1 { get; set; }
        public int var2 { get; set; }
        public int var3 { get; set; }

        public void variable1(int i)
        {
            var1 = i;
        }
        public void variable2(int i)
        {
            var2 = i;
        }
        public void variable3(int i)
        {
            var3 = i;
        }

        public static ObjectClass operator + (ObjectClass A, ObjectClass B)
        {
            ObjectClass obj = new ObjectClass();
            obj.var1 = A.var1 + B.var1;
            obj.var2 = A.var2 + B.var2;
            obj.var3 = A.var3 + B.var3;

            return obj;
        }
    }
    
    class Program
    { 
        static void Main(string[] args)
        {
            ObjectClass var1 = new ObjectClass();
            var1.variable1(5);
            var1.variable2(3);
            var1.variable3(2);

            ObjectClass var2 = new ObjectClass();
            var2.variable1(15);
            var2.variable2(23);
            var2.variable3(20);

            ObjectClass var3 = new ObjectClass();
            var3 = var1 + var2;

            Console.WriteLine("value of object 3 = {0}", var3.var1);
            Console.WriteLine("value of object 3 = {0}", var3.var2);
            Console.WriteLine("value of object 3 = {0}", var3.var3);
            Console.ReadKey();
        }
    }
}

Dynamic Polymorphism: Function Overriding

In Dynamic Polymorphism, the method name and method signature i.e arguments can be the same but the internal implementation may vary from one another. Method Overriding is done by Inheritance.

using System;

namespace PolymorphismUnderstanding
{
    public class Shape
    {
        public virtual double Area()
        {
            return 0;
        }
    }

    public class Circle:Shape
    {
        public double Radius { get; set; }

        public Circle()
        {
            Console.WriteLine("Circle Area");
            Console.WriteLine("Please enter the radius");
            Radius = Convert.ToDouble(Console.ReadLine());
        }

        public override double Area()
        {
            return (3.14) * Math.Pow(Radius,2);
        }
    }

    public class Square:Shape
    {
        public double length { get; set; }
        
        public Square()
        {
            Console.WriteLine("Square Area");
            Console.WriteLine("Please enter the length");
            length = Convert.ToDouble(Console.ReadLine());
        }

        public override double Area()
        {
            return Math.Pow(length, 2);
        }
    }

    public class Rectangle : Shape
    {
        public double length { get; set; }
        public double Breadth { get; set; }

        public Rectangle()
        {
            Console.WriteLine("Rectangle Area");
            Console.WriteLine("Please enter the length");
            length = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Please enter the breadth");
            Breadth = Convert.ToDouble(Console.ReadLine());
        }

        public override double Area()
        {
            return length* Breadth;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Shape s = new Shape();
            Console.WriteLine("Shape {0}", s.Area());

            Circle c = new Circle();
            Console.WriteLine("Circle {0}", c.Area());

            Square sq = new Square();
            Console.WriteLine("Square {0}", sq.Area());

            Rectangle rect = new Rectangle();
            Console.WriteLine("Rectangle {0}", rect.Area());

            Console.WriteLine("End of traditional way of creating objects");

            Shape ns = new Circle();
            Console.WriteLine("Circle {0}", ns.Area());

            ns = new Square();
            Console.WriteLine("Square {0}", ns.Area());

            ns = new Rectangle();
            Console.WriteLine("Rectangle {0}", ns.Area());

            Console.ReadKey();
        }
    }
}

Some Interesting points :

  1. Class shape, circle, square and rectangle all share the same method Area().
  2. Area Method of the base class is declared as virtual. So in the derived class, we had the ability to override that method with a new definition. Now when objects were created and the method was called we were able to get the correct method.
  3. Now at the time of the creation of objects we see the following piece of code “Shape ns = new Circle();”
  4. What that line means is that we have created an ns variable of data type Shape and in that variable, we are storing the reference of an object of the class circle which we have created. Another way of putting is that an object of a circle class is created and is assigned to the memory location of the shape variable.
  5. Some People refer to it as superclass reference variable which can hold the reference of sub-class object without casting.
  6. Please Note a variable of shape class can hold the reference of circle class object but vice-versa is not correct. i.e. “Circle c= new Shape()”

3 thoughts on “POLYMORPHISM

Leave a Reply

Your email address will not be published. Required fields are marked *