Method Overloading
Defining multiple methods with the same name but different parameters, and how Java decides which one to call.
2 min read
Java lets you define several methods with the same name in the same class, as long as their parameter lists are different. This is called overloading, and it's how methods like System.out.println can accept an int, a String, a boolean, or an object, all under one familiar name.
What counts as a different signature
Overloads must differ in the number, type, or order of their parameters — the return type alone isn't enough to distinguish them.
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;
}
public static int add(int a, int b, int c) {
return a + b + c;
}
}All three methods are named add, but the compiler picks the right one based on the arguments you pass:
add(2, 3); // calls add(int, int) -> 5
add(2.5, 3.5); // calls add(double, double) -> 6.0
add(1, 2, 3); // calls add(int, int, int) -> 6Why this is useful
Overloading lets you offer several natural ways to call what's conceptually the same operation, without inventing awkward names like addInts, addDoubles, and addThreeInts. The standard library leans on this heavily — String.valueOf() has overloads for int, double, boolean, char[], and Object, all sharing one intuitive name.
Overload resolution and widening
When an exact match isn't available, Java will widen an argument to find one:
public static void show(long value) {
System.out.println("long: " + value);
}
show(5); // 5 is an int, but there's no show(int) overload,
// so it widens to long and calls show(long)This can get confusing when multiple overloads are plausible matches — Java always prefers the most specific match it can find without widening, and only widens as a fallback. If the overloads are ambiguous (for example, two equally-specific matches through different widening paths), the code simply won't compile, forcing you to disambiguate with an explicit cast.
Overloading vs. overriding
Don't confuse this with overriding (covered in the inheritance lesson later in this course): overloading is about multiple methods with the same name in the same class, distinguished by parameters, and resolved at compile time. Overriding is about a subclass replacing a method it inherited from a parent class, and is resolved at runtime based on the object's actual type. They solve different problems and are easy to mix up by name alone.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.