Method overloading and method overriding are two fundamental concepts in object-oriented programming (OOP), and they differ in purpose, implementation, and usage. Here’s a breakdown:

1. Method Overloading

Definition: Method overloading allows multiple methods in the same class to have the same name but with different parameter lists (type, number, or order of parameters).

Key Points

  • Occurs within the same class.
  • Method signature (method name + parameters) must be different.
  • Can vary in:
    • Number of parameters.
    • Type of parameters.
    • Order of parameters.
  • Return type can vary but does not contribute to method signature uniqueness.
  • Compile-time polymorphism: The method to call is determined at compile time.

Example

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

    double add(double a, double b) {
        return a + b;
    }

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

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(2, 3));       // Calls add(int, int)
        System.out.println(calc.add(2.5, 3.5));   // Calls add(double, double)
        System.out.println(calc.add(1, 2, 3));    // Calls add(int, int, int)
    }
}

2. Method Overriding

Definition: Method overriding allows a subclass to provide a specific implementation of a method already defined in its parent class.

Key Points

  • Occurs between superclass and subclass.
  • The method signature (name, parameters) must be exactly the same.
  • Return type must either be the same or a covariant return type (a subtype of the parent method’s return type).
  • The access modifier in the overridden method must be same or more accessible.
  • Runtime polymorphism: The method to call is determined at runtime, based on the object’s actual type.
  • Requires inheritance.

Example

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Dog();
        animal.sound();  // Calls Dog's overridden method
    }
}

Key Differences

Feature Method Overloading Method Overriding
Purpose Achieves method polymorphism in a class with multiple behaviors of the same name. Provides specific behavior for a method in the subclass.
Scope Same class. Different classes in an inheritance hierarchy.
Parameters Must be different (type, number, order). Must be the same.
Return Type Can differ (but doesn’t define uniqueness). Must be the same or covariant.
Polymorphism Compile-time (static). Runtime (dynamic).
Annotation Used No special annotation required. Commonly uses @Override.

Interview Questions

  1. Can overloaded methods have different return types?

  2. Why can’t static methods be overridden?

  3. What is the role of the @Override annotation in overriding?

  4. Can we overload and override the same method simultaneously?

  5. What happens if you try to override a private method?