C# Polymorphism - Static and Dynamic polymorphism

    1 Votes

The polymorphism is a third characteristic of the object oriented programming. The polymorphism means having many forms. The polymorphism can be of two types:

  • Static polymorphism.
  • Dynamic polymorphism.

 

Static Polymorphism

In the static polymorphism the response to the function is determined at the compile time. In this there is a mechanism which is known as early binding in which we link function to an object during compile time and it is also known as static binding. Static Polymorphism has two techniques:
  • Function Overloading
  • Operator Overloading

 

Function Overloading

In the function overloading we have multiple definitions for the same function name in the same scope of the class. The definition of a function should have the different arguments. Let us have an example:

using System;
namespace Application
{
   class Show
   {
      void show(int i)
      {
         Console.WriteLine("The value of int is: {0}", i );
      }
      void show(double f)
      {
         Console.WriteLine("The value of float is: {0}" , f);
      }
      void show(string s)
      {
         Console.WriteLine("The value of string is: {0}", s);
      }
      static void Main(string[] args)
      {
         Show s = new Show();
         // Call show to print integer
         s.show(5);
         // Call show to print float
         s.show(500.2);
         // Call show to print string
         s.show("Hello C#");
         Console.ReadKey();
      }
   }
}

Output:

The value of int is: 5
The value of float is: 500.2
The value of string is: Hello C#

Operator Overloading

In the operator overloading we can overload the pre defined or built in operators which are available in C#. So the programmer can use the operator with the user defined types also. The overload operators are the functions with the special names the keyword operator which is followed by the symbol which is being used for the operator. Let us have an example to understand it:

using System;
namespace OperatorOvlApplication
{
   class Rectangle
   {
      private int length;   
      private int breadth;
      private int height;   
      public int getArea()
      {
         return length * breadth * height;
      }
      public void setLength( int l )
      {
         length = l;
      }
      public void setBreadth( int b )
      {
         breadth = b;
      }
      public void setHeight( int h )
      {
         height = h;
      }
      public static Rectangle operator+ (Rectangle a, Rectangle b)
      {
         Rectangle r = new Rectangle();
         r.length = a.length + b.length;
         r.breadth = a.breadth + b.breadth;
         r.height = a.height + b.height;
         return r;
      }
   }
   class Program
   {
      static void Main(string[] args)
      {
         Rectangle r1 = new Rectangle ();   
         Rectangle r2 = new Rectangle ();   
         Rectangle r3 = new Rectangle ();  
         int area = 0;
         r1.setLength(2);
         r1.setBreadth(3);
         r1.setHeight(4);
         r2.setLength(1);
         r2.setBreadth(5);
         r2.setHeight(6);
         area = r1.getArea();
         Console.WriteLine("Area of r1 : {0}", area);
         area = r2.getArea();
         Console.WriteLine("Area of r2 : {0}", area);
         r3 = r1 + r2;
         area = r3.getArea();
         Console.WriteLine("Area of r3 : {0}", area);
         Console.ReadKey();
      }
   }
}

Output:

Area of r1 : 24
Area of r2 : 30
Area of r3 : 240

Dynamic Polymorphism

In the dynamic polymorphism it is decided at the run time. In C# it allows us to create abstract classes which are used to provide the partial class implementation of an interface. The implementation is completed when the derived class inherits from it. The dynamic polymorphism can be implemented by abstract class. The abstract classes contain abstract methods, which are implemented by the derived class. Let us have an example:

using System;
namespace PolymorphismApplication
{
   abstract class Shape
   {
      public abstract int area();
   }
   class Square:  Shape
   {
      private int side;
      public Square( int a=0)
      {
         side = a;
      }
      public override int area ()
      { 
         Console.WriteLine("square area is :");
         return (side*side); 
      }
   }
   class Program
   {
      static void Main(string[] args)
      {
         Square s = new Square(10);
         int a = s.area();
         Console.WriteLine("Area is: {0}",a);
         Console.ReadKey();
      }
   }
}

Output:

square area is :
Area is: 100