C# Polymorphism

C# polymorphism in any programming language stands for “many forms”. Polymorphism in C# happens when there are many classes in any program that are related to each other by inheritance. It can be Static and Dynamic. When the response to a function is determined at the compile time is known as Static Polymorphism and when its determined at the run time is known as Dynamic Polymorphism.

C# Polymorphism
There are 2 types of C# Polymorphism:
  1. Static Polymorphism / Compile-Time Polymorphism / Early Binding
  2. Dynamic Polymorphism / Run-Time Polymorphism / Late Binding

Static Polymorphism:

Static C# Polymorphism is also known as Early Binding. In polymorphism when a function is linked with an object while compilation time is called Static Polymorphism. Static Polymorphism is implemented by different techniques.

Static Polymorphism or Compile-Time Polymorphism in C# refers to a function call bound to a class at the time of compilation that is going to be executed from the same bounded class at run-time. This occurs in the case of Method Overloading because each method has a different signature, and we can readily determine the method that matches the method signature based on the method call.

Note: When working with Polymorphism in C#, it’s important to understand two things: what happens during compilation and what happens during method execution. Is the method going to be executed from the same class at run-time that is bound to the class at compile-time, or will it be executed from a different class at run-time than the compile-time bound class? Let’s go ahead and find out what the answer is.

Operator Overloading:

Operator Overloading is defined as the functions with special names the keyword operator followed by the symbol for the operator being defined. An overloaded operator always has a return type and a parameter list.

Function Overloading:

You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.

using System;

namespace Polymorphism {
 public class Printdata {
      void print(int i) {
         Console.WriteLine("Print int value: {0}", i );
      }
     void print(double f) {
         Console.WriteLine("Print float value: {0}" , f);
      }
      void print(string s) {
         Console.WriteLine("Print string value: {0}", s);
      }
    public static void Main(string[] args) {
         Printdata p = new Printdata();
         
         // Calling print for printing integer
         p.print(11);
         
         // Calling print for printing float
         p.print(33.98);
         
         // Call print to print string
         p.print("Function overload");
         
      }
   }
}

Dynamic Polymorphism:

Run-Time Polymorphism or Dynamic Polymorphism is defined as a function call bound to a class at the time of compilation that is executed from a different class (Parent Class) at run-time rather than the class bound at compilation time. This occurs when we utilise Method Overriding because we have multiple methods with the same signature, i.e. the Parent Class and the Child Class both have the same method implementation. So, in this situation, we’ll be able to tell which class the method will be invoked from at runtime.

Dynamic Polymorphism is implemented using Virtual and Override keywords. In it, we override the base class function using the virtual or override keyword. In dynamic polymorphism. we can’t create an instance of an abstract class and can’t declare an abstract method outside an abstract class. When a class is declared sealed, it can’t be inherited, and abstract classes can’t be declared sealed.

using System;

namespace DynamicPolymorphism {
	   public abstract class Side {
      public abstract   int area();
   }
   
 public class  Square: Side  {
      private int Side;
      
      public  Square( int x = 0) {
         Side = x;

      }
      public override int area () { 
         Console.WriteLine("SquareArea :");
         return (Side * Side); 
      }
   }
   public class SquareTester {
     public static void Main(string[] args) {
         Square r = new Square(18);
         double a = r.area();
         Console.WriteLine("SquareArea: {0}",a);
        
      }
   }
}