← Back to Chapters

Java Method Overloading

? Java Method Overloading

? Quick Overview

Method Overloading in Java allows multiple methods with the same name to exist in a class, as long as their parameter lists are different. It improves code readability and supports compile-time polymorphism.

? Key Concepts

  • Same method name, different parameters
  • Parameters can differ by number, type, or order
  • Return type alone cannot overload a method
  • Resolved at compile time

? Syntax / Theory

In method overloading, Java determines which method to call based on the method signature. The signature includes the method name and parameter list, but not the return type.

? Code Example(s)

? View Code Example
// Demonstrates method overloading using different parameter lists
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 static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(10, 20));
        System.out.println(calc.add(10, 20, 30));
        System.out.println(calc.add(5.5, 4.5));
    }
}

? Live Output / Explanation

Output

30
60
10.0

Java selects the correct add() method based on the number and type of arguments passed during the method call.

? Interactive Simulator

Select a method call below to see which Java method signature gets triggered based on the arguments.

 
Method 1:
int add(int a, int b) { ... }
Method 2:
int add(int a, int b, int c) { ... }
Method 3:
double add(double a, double b) { ... }

✅ Tips & Best Practices

  • Use overloading to improve code clarity
  • Keep overloaded methods logically related
  • Avoid confusing parameter combinations
  • Prefer clear method names when logic differs

? Try It Yourself

  • Create an overloaded method for calculating area of shapes
  • Overload a method using different data types
  • Test overloading by changing parameter order