Overloading methods with variable number of parameters

In object-oriented programming, method overloading is a feature that allows a class to have multiple methods with the same name but different parameter lists. This allows developers to conveniently provide different ways to invoke a method based on the number or type of parameters.

However, there are situations where the number of parameters for a method is unknown or can vary. Java provides a mechanism to handle this scenario through the use of variable argument lists, also known as varargs.

Syntax of Varargs in Java

To overload a method with a variable number of parameters, we can use the ... notation in the method declaration. Here’s the syntax:

public void methodName(type... parameterName) {
    // method body
}

The ... is placed after the parameter type, indicating that the method can accept a variable number of arguments of that type.

Example

Let’s understand this concept with an example. Consider a Calculator class that provides various methods to perform arithmetic operations. We’ll overload the add method to handle different numbers of integers.

public class Calculator {
    public int add(int... numbers) {
        int sum = 0;
        for (int num : numbers) {
            sum += num;
        }
        return sum;
    }

    public void displayResult(int... numbers) {
        int result = add(numbers);
        System.out.println("The sum is: " + result);
    }
}

In the Calculator class, we have defined two methods: add and displayResult. The add method accepts a variable number of integers and returns their sum. The displayResult method calls the add method and prints the result.

Usage

We can now use the Calculator class to perform addition with different numbers of integers:

public class Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();

        // Adding two numbers
        int sum1 = calculator.add(5, 10);
        System.out.println("Sum: " + sum1);

        // Adding three numbers
        int sum2 = calculator.add(2, 4, 6);
        System.out.println("Sum: " + sum2);

        // Adding four numbers
        int sum3 = calculator.add(1, 2, 3, 4);
        System.out.println("Sum: " + sum3);

        // Displaying result using varargs
        calculator.displayResult(10, 20, 30);
    }
}

Conclusion

Method overloading with variable number of parameters using varargs is a powerful feature in Java. It allows developers to write more flexible and reusable code by providing methods with different numbers of parameters. By using varargs, we can easily handle situations where the number of parameters may vary or is unknown.

#java #programming