The Wayback Machine - https://web.archive.org/web/20250308002647/https://www.geeksforgeeks.org/different-ways-method-overloading-java/
Open In App

Different Ways of Method Overloading in Java

Last Updated : 05 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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.

Java
// Method overloading by defining multiple 
// add methods with different parameter
class Calculator {
  
    int add(int a, int b) { 
      return a + b; 
    }

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

public class Geeks {
  
    public static void main(String[] args)
    {
        Calculator c = new Calculator();
        System.out.println(c.add(5, 10));
        System.out.println(c.add(5, 10, 15));
    }
}

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.

Java
// method overloading by defining multiple 
// show method with different parameters
class Display {
    void show(int num)
    {
        System.out.println("Integer: " + num);
    }

    void show(String text)
    {
        System.out.println("String: " + text);
    }
    void show(double num)
    {
        System.out.println("Double " + num);
    }
}

public class Geeks {
    public static void main(String[] args)
    {
        Display d = new Display();
        d.show(25);
        d.show("Hello");
        d.show(100.00);
    }
}

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.

Java
// Method overloading by defining multiple 
// print() with different parameter
class Printer {
  
    void print(String text, int copies)
    {
        System.out.println("Printing " + copies
                           + " copies of: " + text);
    }

    void print(int copies, String text)
    {
        System.out.println("Printing " + copies
                           + " copies of: " + text);
    }
}
public class Geeks {
    public static void main(String[] args)
    {
        Printer p = new Printer();
      
        // Calls print(String, int)
        p.print("Document", 5);
      
        // Calls print(int, String)
        p.print(3, "Photo");
    }
}

Output
Printing 5 copies of: Document
Printing 3 copies of: Photo

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
// 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:

Output2

Java Method Overloading – FAQs

What is method overloading in Java and how does it work?

Method overloading in Java allows us to define multiple methods with the same name, but each method must have different parameters. This means we can perform same tasks in different ways, depending on the number or type of input provided to the method.

What are the benefits of using method overloading in Java?

Method overloading improves code readability and reusability by allowing us to use the same method name for performing similar tasks with different inputs. It removes the need for different method names, making the code simpler and more organized.

Can method overloading be done by changing the return type in Java?

No, we can not overload a method just by changing the return type. In Java, the method signature includes the method name and parameters, but not the return type. If two methods have the same name and parameters, changing only the return type will cause an error because the compiler won’t be able to tell the methods apart based on return type alone.

What are the common mistakes to avoid while implementing method overloading in Java?

common mistakes to avoid in method overloading

  • Overloading by return type only: This causes an error because the return type is not part of the method signature.
  • Same parameters: There must be a difference in the number, type, or order of parameters.

How does Java method overloading improve code readability and reusability?

Java method overloading improves code readability and reusability by allowing the same method name to be used for different parameter, thus avoiding the need for multiple method names that perform similar operations. This makes the code cleaner, more organized, and easier to maintain.



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg