Lesson 31 of 50 – Java Method Overloading
62%

Java Method Overloading

Method overloading in Java allows us to create multiple methods with the same name but with different parameter lists. It makes programs easier to read and organize.

1. What Is Method Overloading?

Method overloading means defining two or more methods in the same class with the same name but different parameters.

For example, a class can have methods named add() that accept different numbers of arguments.

class Calculator {

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

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

Both methods are named add(), but one accepts two parameters and the other accepts three.

2. Why Do We Use Method Overloading?
  • To use the same method name for related operations.
  • To make code easier to understand.
  • To support different types or numbers of inputs.
  • To reduce the need for many differently named methods.

For example, instead of creating separate methods such as addTwoNumbers() and addThreeNumbers(), we can use the same method name add().

3. Overloading by Number of Parameters

Methods can have the same name but different numbers of parameters.

class Demo {

    void show(int a) {
        System.out.println("One parameter: " + a);
    }

    void show(int a, int b) {
        System.out.println("Two parameters: " + a + ", " + b);
    }

    public static void main(String[] args) {
        Demo obj = new Demo();

        obj.show(10);
        obj.show(10, 20);
    }
}
Output:
One parameter: 10
Two parameters: 10, 20
Java selects the method that matches the arguments supplied in the method call.
4. Overloading by Data Type

Methods can also be overloaded by changing the parameter data types.

class Demo {

    void display(int number) {
        System.out.println("Integer: " + number);
    }

    void display(String text) {
        System.out.println("String: " + text);
    }

    public static void main(String[] args) {
        Demo obj = new Demo();

        obj.display(100);
        obj.display("Hello Java");
    }
}
Output:
Integer: 100
String: Hello Java
5. Overloading by Parameter Order

Methods can be overloaded by changing the order of different parameter types.

class Demo {

    void show(int number, String text) {
        System.out.println(number + " " + text);
    }

    void show(String text, int number) {
        System.out.println(text + " " + number);
    }

    public static void main(String[] args) {
        Demo obj = new Demo();

        obj.show(10, "Java");
        obj.show("Python", 20);
    }
}
Output:
10 Java
Python 20
6. Can We Overload Methods by Return Type?

No. Changing only the return type does not create a valid overloaded method.

// Invalid Java code

int calculate(int a) {
    return a * 2;
}

// Compilation error: same name and parameter list
double calculate(int a) {
    return a * 2.0;
}
Both methods have the same name and the same parameter list. Java cannot distinguish them based only on their return types.

To overload the method, change its parameter list.

7. Complete Example: Calculator
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 static void main(String[] args) {
        Calculator calc = new Calculator();

        System.out.println(calc.add(10, 20));
        System.out.println(calc.add(2.5, 3.5));
        System.out.println(calc.add(10, 20, 30));
    }
}
Output:
30
6.0
60

The compiler selects the appropriate add() method based on the arguments.

8. Method Overloading with Static Methods

Static methods can also be overloaded. Each overloaded method must have a distinguishable parameter list.

class Demo {

    static void print(int a) {
        System.out.println("Number: " + a);
    }

    static void print(String text) {
        System.out.println("Text: " + text);
    }

    public static void main(String[] args) {
        print(50);
        print("Welcome");
    }
}
Output:
Number: 50
Text: Welcome
9. Method Overloading vs Method Overriding
Method Overloading Method Overriding
Usually happens in the same class. Happens between a parent and child class.
Parameter lists must differ. Method signature must match for a valid override.
Compile-time polymorphism. Runtime polymorphism for instance methods.
Inheritance is not required. Inheritance is normally required.
10. Rules of Method Overloading
  1. Overloaded methods must have the same method name.
  2. They must have different parameter lists.
  3. The number of parameters can be different.
  4. The parameter data types can be different.
  5. The order of different parameter types can be different.
  6. Changing only the return type is not sufficient.
  7. Methods can be overloaded whether they are static or instance methods.
11. Common Mistakes

Mistake 1: Changing only the return type

int getValue(int a) {
    return a;
}

// Invalid: same parameter list
double getValue(int a) {
    return a;
}

Mistake 2: Calling a method with the wrong arguments

void show(int a) {
    System.out.println(a);
}

// show("Hello"); // Compilation error

Always check the method's parameter types and number of arguments before calling it.

12. Real-Life Example: Student Marks
class StudentMarks {

    int calculate(int english, int maths) {
        return english + maths;
    }

    int calculate(int english, int maths, int science) {
        return english + maths + science;
    }

    public static void main(String[] args) {
        StudentMarks student = new StudentMarks();

        System.out.println(
            "Total of two subjects: " +
            student.calculate(80, 75)
        );

        System.out.println(
            "Total of three subjects: " +
            student.calculate(80, 75, 90)
        );
    }
}
Output:
Total of two subjects: 155
Total of three subjects: 245

This example uses the same method name to calculate totals for different numbers of subjects.

13. Practice Assignments
  1. Create a class with two overloaded methods named show(), one accepting an integer and another accepting a string.
  2. Create an add() method that adds two integers and another that adds three integers.
  3. Create overloaded area() methods to calculate the area of a square and a rectangle.
  4. Create overloaded display() methods that accept an integer, a double, and a string.
  5. Create a class with overloaded methods to calculate the total marks of two, three, and five subjects.
Practice Tip: Write and run each program to understand how Java chooses the correct overloaded method.

🧠 Quick Quiz

1. What is method overloading?




2. Can methods be overloaded by changing only the return type?




3. Which is a valid way to overload a method?