1. Introduction
Method overloading occurs when a class defines two or more methods with the same name but different parameter lists. It is a form of compile-time (static) polymorphism, since the compiler decides which overloaded method to call based on the arguments passed.
Cricket analogy: Sachin Tendulkar playing a cover drive against a spinner and a different cover drive against a pace bowler are both called "drive," but the choice of technique based on the bowler type facing him is like overload resolution picking the right method.
Overloading improves readability by letting related operations share one intuitive method name, such as print() for different data types.
Cricket analogy: Just as commentators use "out" for a catch, a bowled, or an LBW without needing separate words for each dismissal type, overloading lets print() handle strings, ints, or objects under one intuitive name.
2. Syntax
class Calculator {
int add(int a, int b) {
return a + b;
}
// different number of parameters
int add(int a, int b, int c) {
return a + b + c;
}
// different parameter type
double add(double a, double b) {
return a + b;
}
// different parameter order
String add(String a, int b) {
return a + b;
}
}3. Explanation
Methods can be overloaded by changing the number of parameters, the type of parameters, or the order of parameter types. The method name and the class remain the same; only the parameter list differs.
Cricket analogy: A team can field different combos, one all-rounder here, two spinners there, all under the same "playing XI" label at the same ground, just as overloaded methods vary in parameter count, type, or order while keeping the same name and class.
The return type alone is NOT sufficient to overload a method — two methods with identical parameter lists but different return types will cause a compile-time error, because the compiler cannot determine which method to call based on return type alone at the call site.
Cricket analogy: You can't have two "review" calls that differ only in whether the umpire's decision comes back as "out" or as a number of runs, the system needs a different call signature, not just a different return, or it won't compile, just like overloading fails on return type alone.
Because overload resolution happens at compile time based on the declared (static) types of the arguments, method overloading is also called static binding or early binding.
Cricket analogy: The team sheet is finalized before the toss based on each player's declared role, not what happens mid-match, just as overload resolution locks in at compile time based on the declared static type of the arguments, static binding.
Exam tip: int calc(int a) and double calc(int a) in the same class is a compile-time error — 'method calc(int) is already defined'. Only the parameter list, not the return type, determines overload uniqueness.
When multiple overloaded methods could match due to automatic widening (e.g., int to long to double) or autoboxing, Java picks the most specific match available without widening or boxing first, then tries widening, then boxing, then varargs — as a last resort.
4. Example
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(2, 3));
System.out.println(calc.add(2.5, 3.5));
System.out.println(calc.add(1, 2, 3));
}
}5. Output
5
6.0
66. Key Takeaways
- Overloading means same method name, different parameter list, within the same class.
- Parameters can differ in number, type, or order.
- Return type alone cannot distinguish two overloaded methods.
- Overload resolution happens at compile time (static binding).
- Overloading improves readability by grouping related operations under one name.
Practice what you learned
1. Which of the following can be changed to overload a method?
2. Is changing only the return type enough to overload a method with the same parameter list?
3. When is method overloading resolved?
4. Which term describes method overloading's binding behavior?
5. Which of these is a valid overload of `void show(int a)`?
Was this page helpful?
You May Also Like
Method Overriding in Java
Understand how Java subclasses override inherited methods, the rules governing overriding, and how runtime polymorphism resolves the actual method called.
Constructors in Java
Understand Java constructors — default, parameterized, overloaded, and chained — with syntax rules and worked examples.
Polymorphism in Java
Explore compile-time and runtime polymorphism in Java, including method overloading, overriding, and dynamic dispatch.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics