Skip to content

Module 4 · Methods — topic 2 of 3

Method Overloading

Defining several methods with the same name and different parameter lists, how Java chooses between them, and using overloading to provide default values.

Beginner 12 min read·Lesson 14 of 39 in Java Programming

Learning objectives

  • Write overloaded methods
  • Predict which overload the compiler chooses
  • Use overloading to simulate default arguments

Definition

Method overloading is declaring multiple methods in the same class with the same name but different parameter lists (number, types or order of parameters). The compiler selects the most specific applicable method at compile time.

Introduction

System.out.println works with an int, a double, a String or an object. That isn't magic — PrintStream declares many methods all called println, each taking a different type. This is overloading, and you can use it in your own classes to offer convenient variations of an operation.

Why it matters

Overloading produces natural APIs: area(radius), area(width, height).

Understanding how Java resolves overloads explains surprising calls, such as remove(1) on a List<Integer> removing by index rather than by value.

Detailed explanation

What counts. Overloads must differ in the number, types or order of parameters. The return type and parameter names do not count: two methods that differ only in return type are a compile error.

Resolution happens at compile time. The compiler looks for the most specific method that accepts the arguments, in three phases: first without boxing or varargs (exact types and widening such as int → long → double), then allowing boxing/unboxing (int ↔ Integer), and finally varargs.

Widening beats boxing. Given f(long) and f(Integer), the call f(5) chooses f(long), because widening is tried before boxing.

Ambiguity. If two candidates are equally specific — for example f(int, double) and f(double, int) called as f(1, 2) — the call doesn't compile.

Default values. Java has no default arguments, so a common pattern is to overload a method and have the shorter versions call the longest one with default values.

Overloading is sometimes called *compile-time* or *static* polymorphism, in contrast with *overriding* (module 5), which is resolved at run time.

Syntax

Java
static double area(double r) { return Math.PI * r * r; }
static double area(double w, double h) { return w * h; }
static String greet(String name) { return greet(name, "Hello"); }
static String greet(String name, String g){ return g + ", " + name + "!"; }
  • Same name + different parameter lists = overloads.
  • Return type alone cannot distinguish methods.
  • Shorter overloads usually delegate to the most general one.

Code examples

Overloads by number and type

Java
public class Area {
static double area(double radius) { return Math.PI * radius * radius; }
static double area(double w, double h) { return w * h; }
static int area(int side) { return side * side; }
public static void main(String[] args) {
System.out.printf("%.2f%n", area(2.0));
System.out.println(area(3.0, 4.5));
System.out.println(area(5));
}
}
Output
12.57
13.5
25

area(5) picks the int version (exact match); area(2.0) picks the double version.

Default values through delegation

Java
public class Greeting {
static String greet(String name) { return greet(name, "Hello"); }
static String greet(String name, String word) { return greet(name, word, 1); }
static String greet(String name, String word, int times) {
return (word + ", " + name + "! ").repeat(times).trim();
}
public static void main(String[] args) {
System.out.println(greet("Asha"));
System.out.println(greet("Ravi", "Welcome"));
System.out.println(greet("Team", "Go", 3));
}
}
Output
Hello, Asha!
Welcome, Ravi!
Go, Team! Go, Team! Go, Team!

More examples

Which overload is chosen?

Java
public class Resolution {
static String f(int x) { return "int"; }
static String f(long x) { return "long"; }
static String f(double x) { return "double"; }
static String f(Integer x) { return "Integer"; }
static String f(Object x) { return "Object"; }
static String f(int... xs) { return "varargs"; }
static String g(long x) { return "g(long)"; }
static String g(Integer x) { return "g(Integer)"; }
public static void main(String[] args) {
byte b = 1;
char c = 'A';
System.out.println(f(5) + " " + f(5L) + " " + f(5.0f) + " " + f(b) + " " + f(c));
System.out.println(f(Integer.valueOf(5)) + " " + f("text") + " " + f() + " " + f(1, 2));
System.out.println(g(5));
java.util.List<Integer> list = new java.util.ArrayList<>(java.util.List.of(10, 20, 30));
list.remove(1); // remove(int index)
list.remove(Integer.valueOf(10)); // remove(Object o)
System.out.println(list);
}
}
Output
int long double int int
Integer Object varargs varargs
g(long)
[30]

Step-by-step explanation

  • f(5) matches f(int) exactly; f(5L) matches f(long); a float widens to double.
  • A byte and a char both widen to int, the closest primitive overload.
  • An Integer argument matches f(Integer) exactly; a String only fits f(Object); zero or two ints need varargs.
  • g(5) chooses g(long): widening (phase 1) is preferred over boxing to Integer (phase 2).
  • On a List<Integer>, remove(1) calls remove(int index), removing 20; to remove a value you must pass an Integer object, which calls remove(Object).

Expected output

Running “Which overload is chosen?” prints exactly:

TextOutput
int long double int int
Integer Object varargs varargs
g(long)
[30]

Practical applications

  • println, Math.max and String.valueOf are heavily overloaded in the JDK
  • Constructors are often overloaded to allow optional settings
  • Builder-like APIs offer shorter overloads with sensible defaults

Best practices

  • Overloads should do the same conceptual thing.
  • Implement shorter overloads by calling the most complete one.
  • Avoid overloads that differ only by a boxed versus primitive type; they confuse readers.

Common mistakes

Overloading by return type only

Avoid

static int parse(String s) { ... }
static double parse(String s) { ... }

Prefer

static int parseInt(String s) { ... }
static double parseDouble(String s) { ... }

The compiler can't tell which one a call means, so methods that differ only in return type are rejected. Give them different names.

Ambiguous overloads

Avoid

static void p(int a, double b) {}
static void p(double a, int b) {}
p(1, 2); // ambiguous

Prefer

p(1, 2.0); // clearly p(int, double)

Both methods need one widening conversion, so neither is more specific and the call fails to compile.

Important notes

  • Overload resolution uses the static (declared) types of the arguments, not their run-time types.
  • Varargs overloads are chosen last, which is why f(int...) lost to every single-argument overload above.

Key takeaways

  • Overloads share a name but differ in parameter lists.
  • Resolution order: exact/widening, then boxing, then varargs.
  • Use overloading to provide default values.

Try it yourself

  1. 1
    Write overloaded max methods for two ints, three ints and an int array.
  2. 2
    Write log(String message) and log(String message, String level) where the first defaults to "INFO".

Finished reading? Mark it complete to update your dashboard.

Suggested practice

5 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhich pair of methods is NOT a valid overload?
  • BeginnerWhat is printed?
  • ModerateWhat is printed?
Practise this topic