C# Polymorphism

Polymorphism is a part of Object-Oriented Programming (OOP) principles. Polymorphism in C# is the ability of a class to provide different implementations of a method that is defined in a base class. It allows one interface (method or property) to be used for different types of objects. Polymorphism can be achieved through method overloading (compile-time polymorphism) and method overriding (run-time polymorphism).

There are two types of Polymorphism

1. Compile-time Polymorphism (also called Static Binding or Method Overloading)

2. Run-time Polymorphism (also called Dynamic Binding or Method Overriding)

1. Compile-time Polymorphism (Method Overloading)

In method overloading, you can have multiple methods with the same name but different parameters (either in type or number of parameters). The compiler decides which method to call at compile time based on the method signature.

Example:


using System;
class Calculator {
    // Method to add two integers
    public int add(int a, int b) {
        return a + b;
    }

    // Method to add three integers
    public int add(int a, int b, int c) {
        return a + b + c;
    }

    // Method to add two double values
    public double add(double a, double b) {
        return a + b;
    }
}

public class MyProgram {
    static void Main() {
        Calculator calc = new Calculator();
        Console.WriteLine(calc.add(10, 20));        // Calls the method with two integers
        Console.WriteLine(calc.add(10, 20, 35));    // Calls the method with three integers
        Console.WriteLine(calc.add(10.5, 20.4));    // Calls the method with two doubles
    }
}

Output:

30
65
30.9

Explanation:

In this example, the add() method is overloaded to accept different numbers and types of arguments, demonstrating compile-time polymorphism.

Run-time Polymorphism (Method Overriding)

In method overriding, a method in a base class is redefined in a derived class using the override keyword. The method in the derived class provides a new implementation, and the version of the method that is invoked is determined at runtime based on the object type (whether it’s a base or derived class object).

Example:


using System;
// Parent class
class Vehicle {
    //Virtual method that can be overridden by derived classes
    public virtual void usefuel() {
        Console.WriteLine("Vehicle use many type of fuels.");
    }
}

// Child class
class PetrolCar : Vehicle {
    // Override the usefuel method
    public override void usefuel() {
        Console.WriteLine("Petrol car use petrol fuel to run.");
    }
}

// Child class
class DieselCar : Vehicle {
     // Override the usefuel method
    public override void usefuel() {
       Console.WriteLine("Diesel car use diesel fuel to run.");
    }
}

class ElectricCar : Vehicle {
    // Override the usefuel method
    public override void usefuel() {
        Console.WriteLine("Electric car does not use fuel.");
    }
}

public class MyProgram {
     static void Main() {

        // PetrolCar object assigned to Vehicle reference
        PetrolCar petrolCar = new PetrolCar();
        petrolCar.usefuel();  // Outputs: Petrol car use petrol fuel to run.

        // DieselCar object assigned to Vehicle reference
        DieselCar dieselCar = new DieselCar();
        dieselCar.usefuel();  // Outputs: Diesel car use diesel fuel to run
        
        // ElectricCar object assigned to Vehicle reference
        ElectricCar electricCar = new ElectricCar();
        electricCar.usefuel();  // Outputs: Electric car does not use fuel.
    }
}

Output:

Petrol car use petrol fuel to run.
Diesel car use diesel fuel to run.
Electric car does not use fuel.

Explanation:

  1. Vehicle class defines a method usefuel().
  2. PetrolCar, DieselCar and ElectricCar classes override the usefuel() method.
  3. At runtime, the usefuel() method of the appropriate class (PetrolCar or DieselCar or ElectricCar) is invoked, demonstrating runtime polymorphism.

What is Virtual Method in Base Class?

In the base class, you declare a method as virtual to indicate that it can be overridden by a derived class.

What is Override Method?

In the derived class, you use the override keyword to define a new implementation of the method.

Advantages of Polymorphism

1. Flexibility and Maintainability: You can write more general and reusable code.

2. Extensibility: You can extend the program by adding new derived classes without modifying existing code.

3. Easier to use inheritance: Polymorphism simplifies the use of base class references to handle different derived class objects.