Function Overloading
Function overloading is defining multiple functions with the same name in the same scope, distinguished only by their parameter lists (different number, order or types of parameters). It is a form of compile-time polymorphism: the compiler picks the matching version from the call's arguments, and functions differing only in return type cannot be overloads.
Function overloading (also called method overloading) is defining multiple functions with the same name in the same scope, distinguished only by their parameter lists (different number, order, or types of parameters). It's a form of compile-time polymorphism, the compiler decides which specific function to call before the program ever runs, based on the arguments in the call.
How it works
The compiler looks at the number and types of arguments in a function call and matches it against the available overloaded versions, picking the one whose parameter list fits. Two functions cannot be overloads of each other if they differ only in return type, the parameter list itself must differ.
A worked 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;
}
String add(String a, String b) {
return a + b; // string concatenation
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(2, 3)); // calls add(int, int) -> 5
System.out.println(calc.add(2.5, 3.5)); // calls add(double, double) -> 6.0
System.out.println(calc.add(1, 2, 3)); // calls add(int, int, int) -> 6
System.out.println(calc.add("Hello, ", "World!")); // calls add(String, String)
}
}
All four methods are named add, but the compiler picks the correct one for each call based purely on the number and types of arguments provided, add(2, 3) unambiguously matches add(int, int), while add(2.5, 3.5) matches add(double, double).
Where this matters in real systems
Constructor overloading is one of the most common real uses: a class like a date/time library might offer one constructor that takes a full date string, another that takes separate year/month/day integers, and another that defaults to the current date with no arguments at all, all named the same (the constructor name always matches the class name), letting callers construct an object however is most convenient for their situation. Standard library functions frequently overload too, for example println() in many languages has overloads for strings, integers, booleans, and objects, so callers never have to remember a different function name per type.
What interviewers actually ask about function overloading
- "Can two functions be overloads if they only differ by return type?" No, the parameter list itself must differ, the compiler resolves overloads based on the arguments in the call, and return type alone isn't part of that resolution.
- "Is function overloading resolved at compile time or runtime?" Compile time, which is why it's classified as compile-time (static) polymorphism.
- "What happens if a call matches more than one overload ambiguously?" It's a compile error, ambiguous overload resolution isn't automatically resolved in the caller's favor, the code simply won't compile until the ambiguity is fixed.
- "How is overloading different from overriding?" Overloading is multiple methods with the same name but different parameters in the same class, resolved at compile time; overriding is a subclass providing a new implementation of a method with the exact same signature as its superclass, resolved at runtime.
Key takeaways
- Overloading means multiple functions with the same name but different parameter lists.
- The compiler resolves which overload to call based on the arguments in each specific call.
- Overloads must differ in their parameters, return type alone isn't enough to distinguish them.
- Constructor overloading is one of the most common real-world uses, offering multiple convenient ways to construct an object.
More in Polymorphism
Browse all OOP topics
Log in to save which topics you've read across every subject.
Frequently asked questions
Can two functions be overloads if they only differ by return type?
No, the parameter list itself must differ, the compiler resolves overloads based on the arguments in the call, and return type alone isn't part of that resolution.
Is function overloading resolved at compile time or runtime?
Compile time, which is why it's classified as compile-time (static) polymorphism.
What happens if a call matches more than one overload ambiguously?
It's a compile error, ambiguous overload resolution isn't automatically resolved in the caller's favor, the code simply won't compile until the ambiguity is fixed.
How is overloading different from overriding?
Overloading is multiple methods with the same name but different parameters in the same class, resolved at compile time; overriding is a subclass providing a new implementation of a method with the exact same signature as its superclass, resolved at runtime.