24 Oct 2023




Beginner

Compile-time (Static) Polymorphism and Runtime (Dynamic) Polymorphism are two types of Polymorphism :

  1. Compile-time (Static) Polymorphism:
    • Method Overloading: In compile-time polymorphism, it is achieved through method overloading. Method overloading allows you to define multiple methods in the same class with the same name but different parameters (number, type, or order).
    • Compile-Time Resolution: The appropriate method to be called is determined at compile time by the compiler, based on the number and types of arguments provided in the method call.
    • Example: If you have multiple methods with the same name but different parameter lists, the compiler decides which method to invoke based on the method call's arguments. For example, you might have different print methods for different data types like int, float, and string.
using System;

class MathOperations
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public double Add(double a, double b)
    {
        return a + b;
    }
}

class Program
{
    static void Main(string[] args)
    {
        MathOperations math = new MathOperations();

        int result1 = math.Add(5, 7);         // Calls the int version of Add
        double result2 = math.Add(3.5, 2.3);  // Calls the double version of Add

        Console.WriteLine($"Result 1: {result1}");
        Console.WriteLine($"Result 2: {result2}");
    }
}

In the above example, we have two methods named Add in the MathOperations class with different parameter types (int and double). The appropriate method is determined at compile time based on the argument types when calling the Add method.

  1. Runtime (Dynamic) Polymorphism:
    • Method Overriding: In runtime polymorphism, it is achieved through method overriding. Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
    • Dynamic Binding: The method to be executed is determined during runtime based on the actual object's type, not at compile time. This is also known as dynamic binding or late binding. -Example: Suppose you have a base class Shape with a method calculateArea(). Subclasses like Circle and Rectangle can override the calculateArea() method to provide their own specific implementations. When you call calculateArea() on an object, the specific implementation associated with the object's class is executed.
using System;

class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a shape");
    }
}

class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a circle");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Shape shape = new Circle();
        shape.Draw(); // Calls the Draw method of the Circle class at runtime

        shape = new Shape();
        shape.Draw(); // Calls the Draw method of the Shape class at runtime
    }
}

In this example, we have a base class Shape with a virtual method Draw, which is overridden in the derived class Circle. At runtime, the appropriate Draw method is called based on the actual object's type, demonstrating dynamic polymorphism.

object-oriented-programming
polymorphism-types