Summary

Overloading in Java is a feature that allows a class to have more than one method with the same name, provided their parameter lists are different. This can include differences in the number of parameters, types of parameters, or both. Overloading provides a way to define multiple behaviors for a single method name, making code more readable and easier to manage.

Example Code

public class MathUtils {
    // Method to add two integers
    public int add(int a, int b) {
        return a + b;
    }

    // Overloaded method to add three integers
    public int add(int a, int b, int c) {
        return a + b + c;
    }

    // Overloaded method to add two doubles
    public double add(double a, double b) {
        return a + b;
    }

    public static void main(String[] args) {
        MathUtils math = new MathUtils();

        // Calling different overloaded methods
        System.out.println(math.add(2, 3)); // Output: 5
        System.out.println(math.add(2, 3, 4)); // Output: 9
        System.out.println(math.add(2.5, 3.5)); // Output: 6.0
    }
}