Java Method Overloading

Summary: in this tutorial, you’ll learn about Java method overloading and how to use it to write cleaner Java code.

Introduction to Java method overloading

In Java, method overloading allows you to define multiple methods with the same name in the same class but with different parameter lists. These methods are called overloaded methods.

When you call an overloaded method of a class, Java determines which method to execute based on the number and types of arguments that you pass to it.

When defining overloading methods, you need to follow these rules:

  • Method name: All overloaded methods must share the same name.
  • Parameter list: The overloaded methods must have a different number of parameters or types of parameters.
  • Return type: The return type doesn’t play a role in method overloading. Java cannot differentiate overloaded methods based on the only return type.

Java overloading method examples

Let’s take some examples of using overloading methods.

1) Overloading methods with different parameter types

The following illustrates how to use method overloading to define overloaded methods with the same number of parameters but different types:

public class Calculator {
    public static int add(int a, int b) {
        return a + b;
    }

    public static double add(double a, double b) {
        return a + b;
    }
}Code language: Java (java)

In this example, the Calculator class has two add() static methods with different parameter types (int and double).

When you call the add() method, Java uses the type of arguments that you pass to it to determine which method to execute. For example:

public class App {
    public static void main(String[] args) {
        int a = 10, b = 20;
        int result = Calculator.add(a, b);
        System.out.println(result);
    }
}Code language: Java (java)

Output:

30Code language: Java (java)

In this example, we pass two integers a and b to the add() method of the Calculator class. Java calls the add() method with the type int.

However, if you pass double arguments to the add() method, Java will call the add() with the type double instead:

public class App {
    public static void main(String[] args) {
        double a = 10.0, b = 20.0;
        double result = Calculator.add(a, b);
        System.out.println(result);
    }
}Code language: Java (java)

Output:

30.0Code language: Java (java)

2) Overloading methods with different numbers of parameters

The following example illustrates how to use method overloading to define overloaded methods that have different numbers of parameters:

public class PriceCalculator {
    public static double calculate(double price, double tax) {
        return price * (1 + tax);
    }

    public static double calculate(double price, double tax, double discount) {
        var priceAfterDiscount = price * (1 - discount);
        return calculate(priceAfterDiscount, tax);
    }
}Code language: Java (java)

The PriceCalculator class has two calculate() static methods with different numbers of parameters.

The first calculate() method accepts a price and tax and returns the price plus the tax amount.

The second calculate() method has three parameters: price, tax, and discount. It returns the price that includes both the discounted price and the tax amount.

Also, the second calculate() method calls the first calculate() method to get the price including tax. This means that the second calculate() method reuses the price calculation logic defined by the first calculate() method.

When you call the calculate() method, based on the number of arguments that you pass to the calculate() method, Java will invoke the corresponding method.

For example, the following calls the calculate() method with two arguments. Therefore, Java will call the first calculate() method of the Calculator class:

public class App {
    public static void main(String[] args){
        var price = PriceCalculator.calculate(100,0.08);
        System.out.println(price);
    }
}Code language: Java (java)

Output:

108.0Code language: Java (java)

The following example invokes the calculate() method with three arguments, Java will call the second calculate() method that calculates the price including tax after the discount:

public class App {
    public static void main(String[] args){
        var price = PriceCalculator.calculate(100,0.08, 0.05);
        System.out.printf("%.2f",price);
    }
}Code language: Java (java)

Output:

102.60Code language: Java (java)

3) Overloading method with different parameter orders

The following example illustrates the overloaded methods with different parameter orders to offer flexibility to the callers of the class:

public class SalaryCalculator {
    public static double increase(double amount, int percent) {
        return amount * (1 + (double) percent / 100);
    }

    public static double increase(int percent, double amount) {
        return increase(amount, percent);
    }
}
Code language: Java (java)

In this example, we define the SalaryCalculator class that includes two static methods for calculating an increase in an amount based on a given percentage.

These methods allow you to calculate the increased amount whether you provide the percentage as the first or second argument.

The first method calculates the increase in the amount by a given percent and returns the new amount after applying the increase:

public static double increase(double amount, int percent) {
    return amount * (1 + (double) percent / 100);
}Code language: Java (java)

The second method acts as a convenience method that ensures you can call the calculation in either order without explicitly converting the parameters. The second increase() method resues the calculation logic of the first increase() method:

public static double increase(int percent, double amount) {
    return increase(amount, percent);
}Code language: PHP (php)

The following program shows how to use the calculate() method to perform the same calculation of an increased amount using the original amount or the percentage as the first argument:

public class App {
    public static void main(String[] args) {
        var result = SalaryCalculator.increase(100.0, 10);
        System.out.printf("%.2f", result);

        System.out.println();

        result = SalaryCalculator.increase(10, 100.0);
        System.out.printf("%.2f", result);
    }
}Code language: Java (java)

Benefits of method overloading in Java

Java method overloading offers the following benefits:

  • Improved Code Readability: Method overloading allows you to use the same method name for operations that conceptually do the same thing but with different input types or numbers of parameters. This makes the code more intuitive and easier to read.
  • Flexibility: Method overloading provides flexibility when dealing with different data types or variable numbers of arguments. You can define methods that cater to various use cases without having to clutter your code with multiple method names.
  • Code Reusability: Overloaded methods can share common functionality, reducing code duplication. You can have a base method that handles the core logic and then overloaded versions that adapt it for different scenarios.

Summary

  • Use Java method overloading to define multiple methods in the same class with the same name but different parameter lists.
  • Overloaded methods must have a different number or type of parameters.
  • Method overloading improves code readability, reusability, and flexibility.
Was this tutorial helpful ?