Open In App

Different Ways of Method Overloading in Java

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
76 Likes
Like
Report

In Java, Method overloading refers to the ability to define multiple methods with the same name but with different parameter lists in the same class. It improves code readability and reusability, Instead of assigning different names to methods performing similar operations, we can use the same name, which makes the program easier to understand.

When we want to perform a single operation in different ways, method overloading simplifies the process. For example, consider a method to perform addition. Instead of naming the methods differently for different inputs (e.g., add(int, int) and add(int, int, int)), overloading allows us to use the same method name but with different parameters

Ways to Overload Methods in Java

Method overloading can be achieved in the following ways:

1. By Changing the Number of Parameters

We can overload a method by providing a different number of parameters in the method signature.

Example: This example demonstrates method overloading by defining multiple add() with different parameter lists in the Calculator class.


Output
15
30

2. By Changing the Data Type of Parameters

We can overload methods by using different data types for the parameters.

Example: This example demonstrates method overloading by defining multiple show() with different parameter type in the Display class.


Output
Integer: 25
String: Hello
Double 100.0

3. By Changing the Order of Parameters

We can overload methods by changing the order of the parameters in the method signature.

Example: This example demonstrates method overloading by defining multiple print() with different parameter order in the Printer class.


Output
Printing 5 copies for: Geek1
Printing 3 copies for: Geek2

Invalid Method Overloading

What happens if we only change the return type of the method?

In this case, method overloading fails. The compiler will give an error as the return value alone is not sufficient for the compiler to figure out which function it has to call. Method overloading only works when there are difference in the method's parameter list such as number,order or type of parameters.

Example: This example demonstrates that method overloading fails if methods have the same parameters but different return types.

// Java Program to demonstrate method overloading fails
class Addition {

    // Adding two integers, returns int
    public int add(int a, int b) { 
      return a + b; 
    }

    // Adding two integers, returns double
    // This will cause a compilation error
    public double add(int a, int b) { 
      return a + b + 0.0; 
    }
}

// Main class
public class Geeks {

    public static void main(String[] args)
    {
        try {
            // Creating an object of Addition class
            Addition ob = new Addition();

            // Calling the method
            int sum1
                = ob.add(1, 2); 
          
            System.out.println("Sum (int): " + sum1);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:

OutputMethodOverloading



Article Tags :

Explore