Method Overloading Rules in Java
Fast recall
Quick revision
Core answer
throws clause does not create an overload.Knowing the rules for overloading and the common gotcha about return type
Thinking you can overload by changing only the return type — this causes a compile error
Mention that overloading is resolved at compile time (static dispatch) and that primitive widening can cause surprising method selection
Interview-ready guide
Interview answer
Method overloading means defining the same method name with different parameter signatures. For example, print(int value) and print(String value) are valid overloads, and the compiler chooses one from the compile-time types at the call site. Different parameter order is simply a different sequence of parameter types; it is not a separate language mechanism.
Changing only the return type does not work. int parse(String) and long parse(String) have the same signature for overloading and cannot both be declared. Parameter names and a different throws clause also do not create an overload. I keep overloads conceptually related and avoid combinations where null, autoboxing, or varargs make the intended call unclear.
Method Overloading Rules in Java
Direct Answer
Method overloading means declaring methods with the same name but different parameter signatures. The compiler selects an applicable overload from the compile-time argument types, using rules for exact matches, primitive widening, boxing, and varargs. Changing only the return type, parameter names, or throws clause does not create an overload.
Code Example
class Logger {
// ── Valid overloads — parameter lists differ ──────────────────────────────
// 1. Different number of parameters
void log(String message) {
System.out.println("[INFO] " + message);
}
void log(String message, int level) {
System.out.println("[LEVEL " + level + "] " + message);
}
// 2. Different parameter types
void log(int code) {
System.out.println("[CODE] " + code);
}
void log(double metric) {
System.out.println("[METRIC] " + metric);
}
// 3. Different parameter order
void log(String message, double value) {
System.out.println(message + ": " + value);
}
void log(double value, String message) {
System.out.println(value + " — " + message);
}
// ── INVALID — return type alone cannot distinguish overloads ──────────────
// String log(String message) { return message; } // COMPILE ERROR:
// 'log(String)' is already defined — same parameter list as void log(String)
}
public class OverloadingDemo {
public static void main(String[] args) {
Logger logger = new Logger();
logger.log("Server started"); // picks log(String)
logger.log("Auth failed", 3); // picks log(String, int)
logger.log(404); // picks log(int)
logger.log(3.14); // picks log(double) — auto-widens float if needed
logger.log("Response time", 0.42); // picks log(String, double)
}
}
// Output:
// [INFO] Server started
// [LEVEL 3] Auth failed
// [CODE] 404
// [METRIC] 3.14
// Response time: 0.42
Common Mistakes & Pitfalls
- Overloading by return type alone — same name and parameters but a different return type is always a compile error; the compiler can't choose when the result is ignored.
- Thinking a different access modifier or
throwsclause makes an overload — neither is part of the signature, so changing only those is a duplicate-method error. - Ignoring primitive widening — calling
add(5)when onlyadd(long)exists silently promotes theint; with mixed numeric overloads you can hit a version you didn't expect. - Confusing overloading with overriding — overloading is one class, different parameters, chosen by the compiler; overriding is parent-child, same signature, chosen by the JVM at runtime.
Check your understanding
Answer these without looking back at the article.
- Can you overload a method by changing only the return type?
- What happens when overloading causes ambiguity with null arguments?
- What is the difference between overloading and overriding?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.