Overloading vs Overriding

Overloading

Overloading refers to defining multiple methods in the same scope, usually within a class, with the same name but different parameters (either in type or number). The compiler determines which method to invoke based on the method signature (name and parameters).

Characteristics of Overloading:

  • Method Name: Same name for all overloaded methods.

  • Parameters: Different parameter lists (different type, number, or both).

  • Return Type: Can be the same or different, but the return type alone cannot differentiate overloaded methods.

  • Compile-Time: Overloading is resolved at compile time.

Example of Overloading in C#:

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

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

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

class Program
{
    static void Main()
    {
        Calculator calc = new Calculator();
        
        Console.WriteLine(calc.Add(1, 2));           // Calls Add(int, int)
        Console.WriteLine(calc.Add(1.5, 2.5));       // Calls Add(double, double)
        Console.WriteLine(calc.Add(1, 2, 3));        // Calls Add(int, int, int)
    }
}

Overriding

Overriding refers to redefining a method in a derived class that is already defined in its base class. The method in the derived class should have the same name, return type, and parameters as the method in the base class. Overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.

Characteristics of Overriding:

  • Method Name: Same name as the method in the base class.

  • Parameters: Same parameter list as the method in the base class.

  • Return Type: Must be the same as the return type of the method in the base class.

  • Run-Time: Overriding is resolved at run time using dynamic binding.

  • Keyword: The override keyword is used in the derived class, and the method in the base class must be marked with the virtual or abstract keyword.

Example of Overriding in C#:

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

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

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

class Program
{
    static void Main()
    {
        Animal myDog = new Dog();
        Animal myCat = new Cat();
        
        myDog.MakeSound(); // Output: Dog barks
        myCat.MakeSound(); // Output: Cat meows
    }
}

Differences Between Overloading and Overriding

FeatureOverloadingOverriding

Definition

Multiple methods with the same name but different parameters within the same class.

A derived class provides a specific implementation for a method already defined in its base class.

Parameters

Must differ in type, number, or both.

Must be the same as the method in the base class.

Return Type

Can be different, but it doesn't differentiate methods.

Must be the same as the method in the base class.

Method Signature

Includes the method name and parameter list.

Includes the method name, parameter list, and return type.

Resolution

Determined at compile time.

Determined at run time using dynamic binding.

Keywords Used

No specific keywords required.

override keyword in the derived class, and virtual or abstract keyword in the base class.

Purpose

Provides multiple ways to perform a similar operation based on different inputs.

Allows a subclass to provide a specific implementation of a method defined in the base class.

Use Cases

  • Overloading:

    • When you need to provide multiple versions of a method that perform similar tasks but with different types or numbers of parameters.

    • Example: Different ways to calculate the sum of integers, doubles, or arrays.

  • Overriding:

    • When you need to extend or modify the behavior of a base class method in a derived class.

    • Example: Different animals making different sounds while having a common interface for making sounds.

In summary, overloading and overriding are two different techniques to achieve polymorphism in object-oriented programming. Overloading allows methods with the same name to handle different types or numbers of parameters, while overriding allows a derived class to provide a specific implementation for a method defined in its base class. Both concepts enhance code flexibility and reusability.

Last updated