25 Feb 2024




Intermediate

In C#, method overriding and method overloading are both mechanisms provided by the language to facilitate polymorphism, but they serve different purposes and operate in different contexts. Here's the differentiation:

  1. Method Overriding:

    • Method overriding occurs in the context of inheritance, where a subclass provides a specific implementation of a method that is already provided by one of its parent classes.
    • In method overriding, the method signature (name and parameters) in the subclass must exactly match the method signature in the superclass.
    • Method overriding is typically used to provide specialized behavior for a method in a subclass, allowing for polymorphic behavior.
    • It is associated with runtime polymorphism (also known as dynamic polymorphism or late binding), where the method implementation is determined at runtime based on the actual type of the object.

Example:

using System;

class Animal {
    public virtual void MakeSound() {
        Console.WriteLine("Animal makes a sound");
    }
}

class Dog : Animal {
    public override void MakeSound() {
        Console.WriteLine("Dog barks");
    }
}

class Program {
    static void Main(string[] args) {
        Animal animal = new Animal();
        animal.MakeSound();  // Output: Animal makes a sound

        Dog dog = new Dog();
        dog.MakeSound();     // Output: Dog barks
        
        Animal dog1 = new Dog();
        dog1.MakeSound();  // Output: Dog barks
    }
}

dog1.MakeSound(); output will be "Dog barks" because dog1 is declared as an Animal reference variable but instantiated as a Dog object. However, due to polymorphism and method overriding, the MakeSound() method of the Dog class will be invoked since the actual object type is Dog, not Animal. This behavior is determined at runtime, and the method resolution is based on the actual type of the object, not the reference type. Therefore, it will print "Dog barks

  1. Method Overloading:

    • Method overloading occurs when multiple methods in the same class have the same name but different signatures (different parameters).
    • Method overloading allows a class to have multiple methods with the same name, but each method performs a different task based on the parameters it accepts.
    • Method overloading is determined at compile time (also known as static polymorphism or early binding), where the compiler decides which method to call based on the method signature and the arguments passed.

Example:

using System;

class Calculator {
    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) {
        Calculator calculator = new Calculator();
        int sumInt = calculator.Add(5, 3);            // Calls the Add method with int parameters
        double sumDouble = calculator.Add(2.5, 3.7);   // Calls the Add method with double parameters

        Console.WriteLine("Sum of integers: " + sumInt);      // Output: Sum of integers: 8
        Console.WriteLine("Sum of doubles: " + sumDouble);    // Output: Sum of doubles: 6.2
    }
}

In summary, while both method overriding and method overloading facilitate polymorphism in C#, they differ in terms of their contexts, purposes, and the times at which the method resolution occurs (compile time for method overloading and runtime for method overriding).

c-sharp
method-overriding
method-overloading