TExcelTAD
Home › Java › Java Method Overloading

Java Method Overloading

Overloading means giving several methods in the same class the same name but different parameter lists. The compiler picks the right version for each call by looking at the arguments. You have used overloading constantly without noticing: System.out.println has separate versions for int, double, String, char, boolean and more.

Why overload?

Without overloading you would need a different name for every variation:

java
static int addInts(int a, int b) { ... }
static double addDoubles(double a, double b) { ... }

With overloading, callers only need to remember one name:

java · try it yourself
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;
    }

    public static void main(String[] args) {
        System.out.println(add(2, 3));
        System.out.println(add(2.5, 3.25));
        System.out.println(add(1, 2, 3));
    }
}
5
5.75
6

What counts as "different"?

Two overloads must differ in their parameter list — the number of parameters, their types, or the order of types. The return type and parameter names are not enough:

Pair of declarations Valid overload?
area(double r) and area(double w, double h) yes — different count
print(int x) and print(String x) yes — different type
pair(int a, String b) and pair(String a, int b) yes — different order
int size() and long size() no — only the return type differs
max(int a, int b) and max(int x, int y) no — only names differ
java
static int parse(String s) { ... }
static double parse(String s) { ... }   // error: method parse(String) is already defined

A practical example

Overloads often provide convenient defaults by calling the "full" version:

java · try it yourself
class Main {
    static String formatPrice(double amount, String currency, int decimals) {
        return String.format("%." + decimals + "f %s", amount, currency);
    }

    static String formatPrice(double amount, String currency) {
        return formatPrice(amount, currency, 2);
    }

    static String formatPrice(double amount) {
        return formatPrice(amount, "EUR");
    }

    public static void main(String[] args) {
        System.out.println(formatPrice(9.5));
        System.out.println(formatPrice(9.5, "USD"));
        System.out.println(formatPrice(1234.5678, "JPY", 0));
    }
}
9.50 EUR
9.50 USD
1235 JPY

Java has no default parameter values, so this "telescoping" pattern is the usual substitute.

How the compiler chooses

The compiler looks for the most specific match. If there's no exact match, it tries widening conversions (e.g. int → long → double), then boxing (int → Integer), then varargs:

java · try it yourself
class Main {
    static void show(long x)    { System.out.println("long: " + x); }
    static void show(double x)  { System.out.println("double: " + x); }
    static void show(Object x)  { System.out.println("Object: " + x); }
    static void show(int... x)  { System.out.println("varargs: " + x.length + " values"); }

    public static void main(String[] args) {
        show(5);          // int -> long is the closest widening
        show(5.0f);       // float -> double
        show("text");     // String is an Object
        show('A');        // char widens to long
        show(1, 2, 3);    // only varargs accepts three ints
    }
}
long: 5
double: 5.0
Object: text
long: 65
varargs: 3 values

If two overloads are equally good the call is ambiguous and won't compile — for example f(int, double) and f(double, int) called as f(1, 2).

Tip: Keep overloads doing the same thing with different inputs. If the behaviour differs, a different name is clearer.

Overloading vs. overriding

Don't confuse the two:

Overloading Overriding
Where same class (or subclass) subclass redefines a parent method
Parameters must differ must be the same
Decided at compile time at runtime (polymorphism)

Overriding is covered in the Inheritance and Polymorphism chapters.

Test yourself

Which change makes two methods valid overloads?

  1. Different return types only
  2. Different parameter names only
  3. Different parameter types
  4. Different access modifiers only

With f(long x) and f(double x) declared, what does f(10) call?

  1. f(long)
  2. f(double)
  3. Neither, compile error
  4. Both

Which built-in method is a well-known example of overloading?

  1. Math.PI
  2. System.out.println
  3. String.length
  4. main