Method overloading in Java means declaring two or more methods in the same class (or hierarchy) with the same name but different parameter lists. The compiler picks the right method at compile time from the number, types and order of the arguments, which is why overloading is called compile-time (static) polymorphism. A different return type alone does not make a valid overload.
Overloading keeps APIs small and predictable: one name such as print or add covers many input shapes. It is different from overriding, which replaces an inherited method at run time.
A method is identified by its name plus its parameter types, called the method signature. Two methods can share a name only if their signatures differ in at least one of these ways:
The return type, parameter names, throws clause and access modifiers are not part of the signature, so changing only those is a compile error.
Each call is bound to a specific method when the code is compiled, using the declared types of the arguments.
This does not compile, because the compiler cannot tell the two methods apart from a call such as parse("42"):
Change the parameters, or give the methods different names such as parseInt and parseLong.
When several overloads could accept a call, Java selects the most specific one in phases:
int to long) without boxing.int to Integer).Phase 1 finds show(long) by widening, so boxing and varargs are never considered. If no single method is clearly the most specific, you get an "ambiguous method call" error. Passing null to overloads taking String and Integer is a classic example.
Even though value holds a String at run time, the choice is made from its declared type Object. Dispatch on the runtime type is overriding, not overloading.
Constructors can be overloaded too. Use this(...) to delegate to a more complete constructor instead of duplicating code.
| Aspect | Overloading | Overriding |
|---|---|---|
| Where | Same class or a subclass | Subclass replaces a superclass or interface method |
| Signature | Must differ | Must be the same |
| Return type | Can differ | Same or a covariant subtype |
| Resolved | At compile time | At run time |
@Override | Not applicable | Recommended |
int and long, or Object and String, because callers cannot easily predict which one runs.User.guest(name).main method?Yes. You can declare extra methods named main with other parameters, but the JVM only starts the program from the standard entry point public static void main(String[] args) (or the instance main forms in newer Java versions).
Yes. Static and instance methods follow the same signature rules, and static methods can be overloaded in the same class and in subclasses.
No. The overload is chosen at compile time, so there is no run-time lookup cost beyond a normal method call.
throws clause?No. Neither is part of the method signature, so the compiler reports a duplicate method.
Method overloading lets one method name serve several parameter lists in Java. The compiler selects the overload at compile time from the declared argument types, trying widening first, then boxing, then varargs. Return type alone never distinguishes overloads, and runtime type-based selection is the job of overriding. Keep overloads consistent and few, and prefer builders or named factories when the variations grow.