Method overloading in Java

Method overloading is a feature in Java that allows you to define multiple methods with the same name but different parameters. This enables you to perform similar operations with different inputs, making your code more modular and readable.

When you overload a method, Java differentiates between the methods based on the number, types, and order of the parameters. The compiler determines the correct method to invoke during the compile-time itself.

Syntax

The syntax for method overloading in Java is as follows:

public returnType methodName(parameter1) {
    // Method implementation
}

public returnType methodName(parameter1, parameter2) {
    // Method implementation
}

// additional overloaded methods

Example

Let’s consider an example where we want to create a Calculator class with overloaded methods for finding the area of different shapes.

public class Calculator {
    public double area(int radius) {
        return Math.PI * radius * radius;
    }

    public double area(int length, int width) {
        return length * width;
    }

    public double area(int base, int height) {
        return 0.5 * base * height;
    }
}

In the above example, we have three methods named area() with different parameters. The first method calculates the area of a circle given its radius, the second method calculates the area of a rectangle given its length and width, and the third method calculates the area of a triangle given its base and height.

Now, we can create an instance of the Calculator class and call the appropriate method based on our requirements:

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

        double circleArea = calc.area(5);
        System.out.println("Area of circle: " + circleArea);

        double rectangleArea = calc.area(4, 6);
        System.out.println("Area of rectangle: " + rectangleArea);

        double triangleArea = calc.area(3, 8);
        System.out.println("Area of triangle: " + triangleArea);
    }
}

Output:

Area of circle: 78.53981633974483
Area of rectangle: 24.0
Area of triangle: 12.0

In the above example, we create an instance of the Calculator class and call the appropriate area() method based on the shape we want to calculate the area for.

Benefits of Method Overloading

By leveraging method overloading, you can write more concise and maintainable code in Java.

#java #methodoverloading