Tuesday, March 31, 2026

Method Overloading vs Method Overriding in Java

1. Introduction

In Java, method overloading and method overriding are key concepts of polymorphism.

  • Overloading → Same method name, different parameters (Compile-time polymorphism)

  • Overriding → Same method, same parameters, different implementation (Runtime polymorphism)




2. Method Overloading

Explanation

  • Multiple methods with the same name but different parameter lists

  • Occurs within the same class

  • Decision is made at compile-time

Rules

  • Method name must be same

  • Parameters must differ (type, number, or order)

  • Return type alone is NOT enough


3. Example of Method Overloading

class Calculator {

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

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

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

public class OverloadingExample {
    public static void main(String[] args) {
        Calculator c = new Calculator();

        System.out.println(c.add(10, 20));
        System.out.println(c.add(10, 20, 30));
        System.out.println(c.add(10.5, 20.5));
    }
}

4. Explanation of Code

  • Same method add() used in different ways

  • Compiler decides which method to call based on arguments

Output:

30
60
31.0

5. Method Overriding

Explanation

  • Subclass provides a specific implementation of a method already defined in parent class

  • Occurs between parent and child classes

  • Decision is made at runtime

Rules

  • Method name must be same

  • Parameters must be same

  • Must have inheritance

  • Cannot reduce access level


6. Example of Method Overriding

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

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

public class OverridingExample {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}

7. Explanation of Code

  • Dog overrides sound() method

  • Method call is decided at runtime → dynamic binding

Output:

Dog barks

8. Key Differences Table




9. Real-Time Example

  • Overloading → Calculator methods (add, multiply with different inputs)

  • Overriding → Payment system:

    • Parent: pay()

    • Child: UPI, Card → different implementations


10. Summary

  • Overloading → Same name, different parameters (Compile-time)

  • Overriding → Same method, different behavior (Runtime)

  • Overloading improves readability

  • Overriding supports runtime flexibility


Java Full Stack Developer Roadmap

To master OOP concepts like polymorphism:

👉 https://www.ashokit.in/java-full-stack-developer-roadmap


Promotional Content

Want to master Java OOP concepts like Overloading and Overriding?

Best Core JAVA Online Training in ameerpet

No comments:

Post a Comment

To build frictionless production-ready Java applications in 2026, developers must move beyond traditional coding styles and adopt modern pra...