🌱BeginnerMethods

Method Overloading

Define multiple methods with the same name but different parameters - Java uses the argument types to choose the right version.

Overview

Method overloading lets you define several methods with the same name but different parameter lists. Java uses the number and types of arguments at the call site to select the correct version at compile time. This is sometimes called compile-time polymorphism. Overloading is common in the standard library - System.out.println() has versions for int, double, String, boolean, char, and more. You don't need to write printInt(), printDouble(), printString() separately.

Key Points

  • •Overloaded methods share a name but differ in parameter count, types, or order
  • •The return type alone cannot differentiate overloaded methods
  • •Java resolves which version to call at compile time based on the argument types
  • •Varargs (int... nums) is a special form that accepts any number of int arguments
  • •Overloading improves usability - one logical operation works with different input types
  • •Does not require any special keyword - just define methods with the same name

Examples

Overloaded add() for different types

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

    static double add(double a, double b) {
        return a + b;
    }

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

    static String add(String a, String b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(3, 4));           // 7 (int version)
        System.out.println(add(2.5, 1.5));       // 4.0 (double version)
        System.out.println(add(1, 2, 3));         // 6 (three-int version)
        System.out.println(add("Hello", " World")); // Hello World
    }
}

Java selects the best match automatically. If you pass two ints, the int version runs.

Overloading with different parameter order

public class Main {
    static String format(String name, int age) {
        return name + " is " + age;
    }

    static String format(int age, String name) {
        return "Age " + age + ": " + name;
    }

    public static void main(String[] args) {
        System.out.println(format("Alice", 25)); // Alice is 25
        System.out.println(format(25, "Alice")); // Age 25: Alice
    }
}

Different parameter orders count as different signatures, but this can be confusing - use clear parameter names.

Varargs - variable number of arguments

public class Main {
    // Accepts any number of ints
    static int sum(int... nums) {
        int total = 0;
        for (int n : nums) {
            total += n;
        }
        return total;
    }

    public static void main(String[] args) {
        System.out.println(sum(1, 2));       // 3
        System.out.println(sum(1, 2, 3));    // 6
        System.out.println(sum(5, 10, 15, 20)); // 50
        System.out.println(sum());           // 0
    }
}

int... declares a vararg. Inside the method it behaves like an int[]. A method can only have one vararg parameter, and it must be last.

Common Mistakes

  • Trying to overload by return type only: `int method()` and `double method()` - compile error, same signature
  • Confusing overloading (same class, different parameters) with overriding (different classes, same signature)
  • Ambiguous calls: if you call sum(1, 2) and there are methods sum(int, int) and sum(double, double), Java picks the most specific one (int, int) here

Note

Java's Math class shows overloading in action: Math.abs(int), Math.abs(long), Math.abs(float), Math.abs(double) - all named abs, different types.

Exercises

Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.

Loading editor…

Completed: 0 / 4 exercises

Knowledge Check

Question 1 of 4

What is method overloading?