Method Overloading in Java – A Complete Guide

Introduction

Method Overloading is a feature in Java that allows a class to have more than one method with the same name, but with different parameter lists. It is a part of compile-time polymorphism or static polymorphism. Method overloading improves code readability and usability.


1. Key Rules for Method Overloading

  1. Method name must be same.
  2. Parameter list must be different:
    • Different number of parameters
    • Different data types of parameters
    • Different sequence of parameters
  3. Return type can be different, but it alone cannot distinguish overloaded methods.
  4. Access modifiers can differ.
  5. Overloaded methods can throw different exceptions.

2. Scenarios for Method Overloading

Let’s discuss all possible scenarios with examples.

2.1. Overloading by Number of Parameters

class Calculator {
    // Method with 2 parameters
    int add(int a, int b) {
        return a + b;
    }

    // Method with 3 parameters
    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(10, 20));       // Output: 30
        System.out.println(calc.add(10, 20, 30));   // Output: 60
    }
}

Explanation:
Java determines which method to call based on the number of arguments.

2.2. Overloading by Data Type of Parameters

class Calculator {
    int multiply(int a, int b) {
        return a * b;
    }

    double multiply(double a, double b) {
        return a * b;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.multiply(5, 10));      // Output: 50
        System.out.println(calc.multiply(5.5, 2.0));   // Output: 11.0
    }
}

Explanation:
The compiler chooses the method based on parameter type matching.

2.3. Overloading by Sequence of Parameters

class Calculator {
    void display(int a, double b) {
        System.out.println("int-double: " + a + ", " + b);
    }

    void display(double a, int b) {
        System.out.println("double-int: " + a + ", " + b);
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        calc.display(10, 5.5);  // Output: int-double: 10, 5.5
        calc.display(5.5, 10);  // Output: double-int: 5.5, 10
    }
}

Explanation:
Even with the same number and type of parameters, order matters.

2.4. Overloading with Varargs

class Calculator {
    int sum(int... numbers) {
        int total = 0;
        for (int n : numbers) total += n;
        return total;
    }

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

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.sum(10, 20));       // Calls sum(int a, int b) → 30
        System.out.println(calc.sum(10, 20, 30));   // Calls sum(int... numbers) → 60
    }
}

Explanation:
Varargs methods can accept any number of arguments, but the compiler prefers exact match first.

2.5. Overloading with Different Return Types (Not Alone)

class Test {
    int compute(int a, int b) {
        return a + b;
    }

    // int compute(int a, int b) { return a * b; } // ❌ Not allowed

    double compute(int a, double b) {
        return a * b;
    }
}

Explanation:
Java cannot differentiate methods by return type alone. You must change the parameters to overload.

2.6. Overloading with Access Modifiers & Exceptions

class Example {
    public void show(int a) {
        System.out.println("Public method: " + a);
    }

    private void show(int a, int b) {
        System.out.println("Private method: " + (a + b));
    }

    void show(String s) throws Exception {
        System.out.println("Throws exception: " + s);
    }
}

Explanation:
Access modifiers and exceptions do not affect overloading. Only the parameter list matters.

2.7. Real-Time Example – Online Shopping

class ShoppingCart {
    void addItem(String item) {
        System.out.println(item + " added to cart.");
    }

    void addItem(String item, int quantity) {
        System.out.println(quantity + " " + item + "(s) added to cart.");
    }

    void addItem(String item, double price) {
        System.out.println(item + " added with price $" + price);
    }
}

public class Main {
    public static void main(String[] args) {
        ShoppingCart cart = new ShoppingCart();
        cart.addItem("Book");               // Single item
        cart.addItem("Pen", 10);            // Multiple quantity
        cart.addItem("Laptop", 1500.50);    // With price
    }
}

Explanation:
Overloading provides a flexible API for different input scenarios.

3. Points to Remember

  1. Method overloading is resolved at compile time (Static Polymorphism).
  2. Cannot overload methods only by return type.
  3. Can overload constructors as well.
  4. Can combine varargs, different types, and number of parameters for flexible API design.

4. Overloading vs Overriding

FeatureOverloadingOverriding
Occurs inSame classSubclass
Method nameSameSame
ParameterMust differMust be same
Return typeCan differ (but not alone)Must be same or covariant
Compile/RuntimeCompile timeRuntime
InheritanceNot requiredRequired

5. Conclusion

Method overloading in Java enhances code readability and flexibility, allowing the same method to handle different types or numbers of inputs. It is a core part of compile-time polymorphism in object-oriented programming.

Java developer with 9+ years of IT experience, sharing tutorials and tips to help learners master Java programming.

Leave a Reply

Your email address will not be published. Required fields are marked *