Polymorphism in c#,Overloading and Overriding examples.

Polymorphism means many forms.In simple words multiple implementation of same behavior or method.
 It can be achieved through two ways.

1)Static Polymorphism/Compile Time polymorphism(Overloading)
2)Dynamic Polymorphism/ Run Time polymorphism (Overriding)

1.Static Polymorphism:
It's also called compile time polymorphism. same method name with different argument is called method overloading its a way to achieve static polymorphism.
please go through the example where the method Printing is having 3 different set of arguments. each time that method get overload and it will print the corresponding the values.
(early binding) compiler know what will be the outcome of the method and what all the objects are going to create etc.
Overloading.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shape
{
    class Overloading
    {
        void Printing(int i)
        {
            Console.WriteLine("Print Int: {0}", i);
        }
        void Printing(int i, int j)
        {
            Console.WriteLine("Print sum  :{0}", i+j);
        }
        void Printing(string s)
        {
            Console.WriteLine("Print string: {0}", s);
        }
        static void Main(string[] args)
        {
            Overloading p = new Overloading();

            p.Printing(5);
            
            p.Printing(10,20);

            p.Printing("String  C#");
            Console.ReadLine();
        }
        
    }
}


2) Dynamic Polymorphism 
Same method name same parameters but different implementation this type of method's will override the behavior of abstract class or virtual class.
so dynamic polymorphism also called as run time polymorphism.
It's late binding because in runtime only compiler  know's  what out put its going to achieve. what all the objects it's going to bind. etc.

 eg 1: Shapes example overriding the abstract method findarea

Overriding.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shape
{
    class Overriding
    {
        abstract class Shape
        {
            public abstract int findArea();
        }

        class Rectangle : Shape
        {
            private int length;
            private int width;
            public Rectangle(int a = 0, int b = 0)
            {
                length = a;
                width = b;
            }
            public  override int  findArea()
            {
                return (width * length);
            }
        }

         class Circle : Shape
        {

            private int radious;
            public Circle(int a = 0)
            {
                radious = a;
               
            }

            public override int findArea()
            {
                    return Convert.ToInt32(radious * radious * System.Math.PI);
            }
        }

         static void Main(string[] args)
         {
             Circle c = new Circle(10);
             Rectangle r = new Rectangle(10, 20);


             int a = c.findArea();
             Console.WriteLine("Area of Circle: {0}", a);

             int b = r.findArea();
             Console.WriteLine("Area of Rectangle: {0}", b);
             Console.ReadLine();
         }
        
    }
}


Here same method findArea having different implementation

eg 2: Overriding virtual method findArea
here we are given the constructors in base method and we are extending it to the derived classes and there we are overriding the virtual method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shape
{
    class Overriding
    {
       
        class Areavirtual
        {
            protected int width, height;
     
            public Areavirtual(int a = 0, int b = 0)
            {
            width = a;
            height = b;
            }
            public virtual int FindArea()
            {
            return 0;
            }
        
        }

        class Rectangle : Areavirtual
        {
            public Rectangle(int a = 0, int b = 0): base(a, b)
            {

            }
            public override int FindArea()
            {
               
                return (width * height);
            }
        }
        class Square : Areavirtual
        {
            public Square(int a = 0) : base(a)
            {

            }
            public override int FindArea()
            {

                return (width * width);
            }
        }

        class Circle : Areavirtual
        {
        
            public Circle(int a): base(a)
            {
            }

            public override int FindArea()
            {

                return Convert.ToInt32((width * width * 3.14));

            }
        }

        static void Main(string[] args)
        {
            Circle r = new Circle(10);
            Rectangle s = new Rectangle(10, 7);
            Square t = new Square(10);

            double a = r.FindArea();
            double b = s.FindArea();
            double c = t.FindArea();
            Console.WriteLine("Area of Circle: {0}", a);
            Console.WriteLine("Area of Triangle : {0}", b);
            Console.WriteLine("Area of Rectangle  : {0}", c);
            Console.ReadLine();
        }
    }
}
/* androidsharp */

0 comments: