Method Overloading
Method Overloading
When the same action makes sense for different inputs, such as adding two whole numbers, adding three, or adding two decimals, Java lets you give every version the same name. The compiler tells the versions apart by the arguments in each call. You already use this whenever you call System.out.println with an int one moment and a String the next. AP and university exams regularly ask which version a given call runs.
What makes two methods different
Every method has a method signature: its name together with the number, types and order of its parameters. Method overloading means declaring two or more methods in the same class with the same name but different signatures. In short:
public class Calculator {
static int add(int a, int b) { return a + b; }
static int add(int a, int b, int c) { return a + b + c; }
static double add(double a, double b) { return a + b; }
static String add(String s, int n) { return s + n; }
}All four methods are called add, yet each has a different parameter list, so the class compiles. The table shows which differences count.
Difference between two headers | Legal overload? | Example pair |
|---|---|---|
Number of parameters | Yes | add(int, int) and add(int, int, int) |
Parameter types | Yes | add(int, int) and add(double, double) |
Order of parameter types | Yes | add(String, int) and add(int, String) |
Parameter names only | No | add(int a, int b) and add(int x, int y) |
Return type only | No | int add(int, int) and double add(int, int) |
Modifiers only | No | a static and a non-static add(int, int) |
How the compiler chooses a method
Java chooses the overload at compile time, using the declared types of the argument expressions. This process, called overload resolution, runs in up to three phases and stops at the first phase that finds at least one applicable method:
- Allow only exact type matches and automatic widening: byte to short to int to long to float to double, char to int, and a subclass reference to a superclass type.
- If nothing applies, also allow boxing and unboxing between primitives and wrapper classes such as int and Integer.
- If still nothing applies, also allow varargs methods such as add(int... nums).
Narrowing, such as double to int, never happens automatically, so a method with an int parameter cannot accept 3.5 without a cast.
When several methods apply in the same phase, the compiler picks the most specific one: the method whose parameter types could all be passed to the other candidates. Using the Calculator class:
- add(2, 3) matches add(int, int) exactly and add(double, double) by widening. The int version is more specific because an int can widen to a double but not the reverse, so it runs and returns 5.
- add(2, 3.5) cannot use add(int, int), because 3.5 would need narrowing. The 2 widens to 2.0, add(double, double) runs, and the result is 5.5.
- add("Lap ", 4) matches only add(String, int) and returns "Lap 4".
If two candidates apply and neither is more specific, the call is ambiguous and will not compile. With show(int, double) and show(double, int), the call show(1, 2) fits both by widening one argument, so neither wins.
What to remember
- Overloaded methods share a name and differ in the number, types or order of their parameters.
- Return type, parameter names and modifiers alone never distinguish overloads.
- The compiler tries exact matches and widening first, then boxing and unboxing, then varargs.
- Among applicable methods, the most specific one wins; a tie is a compile-time ambiguity error.
Example: Choosing overloads in a geometry helper
A geometry helper class contains four static methods named area:
public class Geometry {
static int area(int side) { // square
return side * side;
}
static int area(int width, int height) { // rectangle
return width * height;
}
static double area(double radius) { // circle
return Math.PI * radius * radius;
}
static double area(double base, double height) { // triangle
return 0.5 * base * height;
}
}For each call, identify which overload the compiler selects and find the value it returns. Give an area to two decimal places when it is not a whole number.
a) Geometry.area(9) and Geometry.area(4, 7)
The call area(9) has one argument, so only the one-parameter overloads area(int) and area(double) are candidates. The literal 9 has type ________, so area(int) matches exactly, while area(double) would need widening. Both apply in the first phase, and area(int) is more specific, so the square formula runs:
The call area(4, 7) has ________ arguments, so the candidates are area(int, int) and area(double, double). Both apply, since each int can widen to double, but area(int, int) is more specific. It returns the int value
b) Geometry.area(1.5)
The literal 1.5 has type double. The overload area(int) is not applicable, because turning a double into an int is narrowing, which Java never does automatically. Only area(double) applies, so the circle formula runs. The radius squared is ________, so
The returned value has type double.
c) Geometry.area(3L)
The suffix L makes 3L a long. A long cannot narrow to int, so area(int) is not applicable. A long can widen to double, so the argument becomes ________ and area(double) runs:
If the programmer meant a 3-by-3 square, this call still compiles but silently computes a circle. Overload resolution follows types, not intentions, so check the declared type of every argument.
d) Geometry.area(6, 2.5)
There are two arguments, so the candidates are area(int, int) and area(double, double). The first is not applicable, because 2.5 would need narrowing to int. The second applies once 6 widens to ________. The triangle formula runs: 0.5×6.0 is ________, and
A single double argument is enough to rule out the all-int overload.
A Java checkout helper calculates totals in cents. Its one-parameter overload must charge for exactly one item by calling the two-parameter overload, keeping the multiplication in one place.
class Checkout {
static int total(int unitPriceCents, int quantity) {
return unitPriceCents * quantity;
}
static int total(int unitPriceCents) {
return total(PRICE_ARGUMENT, COUNT_ARGUMENT);
}
}The uppercase identifiers are placeholders. Complete the replacement instructions using the existing parameter name and a decimal integer literal: replace PRICE_ARGUMENT with and replace COUNT_ARGUMENT with .
A student wants to place each of the following pairs of Java method headers in the same class. Which pairs are legal overloads that will compile together? Select all that apply.