← Back to Week 2 Hub

Method Overloading

Same method name, different parameters — Java picks the right one based on what you pass in.

1 — What Is Method Overloading?

A class can have multiple methods with the same name as long as their parameter lists are different. Java uses the arguments you pass to decide which version to call.

Here is a Calculator class with four overloaded add methods:

add Same Name
(int a, int b) Different Params
Returns: int
Signature: add(int, int)
add Same Name
(double a, double b) Different Params
Returns: double
Signature: add(double, double)
add Same Name
(int a, int b, int c) Different Params
Returns: int
Signature: add(int, int, int)
add Same Name
(String a, String b) Different Params
Returns: String
Signature: add(String, String)
2 — Signature Matching

Java determines which overload to call by matching the method signature = name + parameter types. Click a method call to see which method it resolves to.

Click a call below
3 — Try It Yourself

Pick argument types and values, then call add(). Watch which overload Java selects.

calc.add(?, ?)
Result will appear here
4 — The Rules of Overloading

Same Name, Different Parameters

Methods must share the same name but differ in their parameter list — number of parameters, types, or order of types.

add(int, int)
add(int, int, int)
add(double, double)

Return Type Alone Is NOT Enough

Two methods that differ only by return type will NOT compile. Java cannot tell them apart from the call site.

int add(int a, int b)
double add(int a, int b) // ERROR!
// Same params → same signature
🔄

Parameter Order Matters

add(int, String) is a different signature from add(String, int) because the types appear in a different order.

print(String msg, int count)
print(int count, String msg)
// Two valid overloads
Common Mistake: Trying to overload by changing only the return type. int add(int a, int b) and double add(int a, int b) will not compile — the parameter lists are identical, so Java sees them as the same method.
You've already seen overloading! System.out.println() accepts int, double, String, boolean, and more — that's method overloading in action. One name, many parameter types.

Click through the examples in Section 2 and experiment in Section 3 to build intuition.

← Constructor Overloading Object Lifecycle →