Differentiate between method overloading and method oversiding with example.

Differentiate Between Method Overloading and Method Overriding in Java

In Java, method overloading and method overriding are two core concepts of polymorphism. Overloading is compile-time polymorphism within the same class, while overriding is runtime polymorphism across a parent–child (superclass–subclass) relationship. Below is a simple, exam-ready comparison with examples.

Definitions

  • Method Overloading: Multiple methods in the same class share the same name but have different parameter lists (type, number, or order). The compiler decides which method to call based on the arguments.
  • Method Overriding: A subclass provides its own implementation of a method already defined in the superclass, keeping the same method signature. The JVM decides which method to call at runtime based on the actual object.

Key Differences at a Glance

  • Purpose:
    • Overloading: Improves code readability and convenience by offering multiple ways to call a method.
    • Overriding: Enables dynamic behavior and polymorphism using inheritance.
  • Where It Happens:
    • Overloading: Within the same class (or with inheritance but still resolved by different parameter lists).
    • Overriding: Between superclass and subclass.
  • Signature Rule:
    • Overloading: Must differ in parameter list. Return type alone is not enough.
    • Overriding: Must have the same method signature; return type can be covariant (a subclass of the original return type).
  • Binding Time:
    • Overloading: Compile-time binding (static polymorphism).
    • Overriding: Runtime binding (dynamic polymorphism).
  • Access and Modifiers:
    • Overloading: No special access rules; each method is independent.
    • Overriding: Cannot reduce visibility (e.g., public cannot become protected). Methods marked final or private cannot be overridden; static methods are hidden, not overridden.
  • Exceptions (Checked):
    • Overloading: Exception declarations are unrelated.
    • Overriding: Overriding method cannot throw broader checked exceptions than the superclass method.
  • Use Cases:
    • Overloading: Utility methods like print(), add() with different parameter types.
    • Overriding: Frameworks, callbacks, and polymorphic behavior (e.g., different animals making different sounds).

Example: Method Overloading (Compile-Time Polymorphism)

// Demonstrates method overloading: same name, different parameter lists
class Calculator {
    int add(int a, int b) {              // 2 integers
        return a + b;
    }

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

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

public class OverloadingDemo {
    public static void main(String[] args) {
        Calculator c = new Calculator();
        System.out.println(c.add(5, 7));          // calls int, int -> 12
        System.out.println(c.add(3.5, 2.1));      // calls double, double -> 5.6
        System.out.println(c.add(1, 2, 3));       // calls int, int, int -> 6
    }
}

Example: Method Overriding (Runtime Polymorphism)

// Demonstrates method overriding: subclass redefines superclass method
class Animal {
    void sound() {
        System.out.println("Some generic animal sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() { // same signature as in Animal
        System.out.println("Bark");
    }
}

public class OverridingDemo {
    public static void main(String[] args) {
        Animal a1 = new Animal();
        Animal a2 = new Dog();  // reference of Animal, object of Dog

        a1.sound(); // calls Animal.sound()
        a2.sound(); // calls Dog.sound() due to runtime dispatch (overriding)
    }
}

Expected Output

12
5.6
6
Some generic animal sound
Bark

Quick Tips for Exams

  • Overloading: Same method name, different parameters; decided at compile time.
  • Overriding: Same signature in subclass; decided at runtime; use @Override to catch mistakes.
  • Return type alone cannot distinguish overloaded methods, but covariant return is allowed in overriding.
  • final/private methods cannot be overridden; static methods are hidden (not true overriding).
  • Constructors can be overloaded but never overridden.