Method Overloading in Java
Learn how Java allows multiple methods to have the same name with different parameter lists.
What is Method Overloading?
Method overloading means defining multiple methods with the same name in the same class, but with different parameter lists.
It is an example of compile-time polymorphism because the compiler determines which method should be called based on the arguments supplied.
Basic Example
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
Both methods are named add, but their parameter
lists are different.
Calling Overloaded Methods
Calculator calculator = new Calculator();
System.out.println(
calculator.add(10, 20)
);
System.out.println(
calculator.add(10, 20, 30)
);
30
60
Ways to Overload a Method
Methods can be overloaded by changing their parameter list.
- Changing the number of parameters.
- Changing the data types of parameters.
- Changing the order of parameters when their types differ.
1. Different Number of Parameters
static void display(int a) {
System.out.println(a);
}
static void display(int a, int b) {
System.out.println(a + " " + b);
}
static void display(int a, int b, int c) {
System.out.println(a + " " + b + " " + c);
}
2. Different Parameter Types
static void print(int value) {
System.out.println("Integer: " + value);
}
static void print(double value) {
System.out.println("Double: " + value);
}
static void print(String value) {
System.out.println("String: " + value);
}
3. Different Order of Parameters
The order can also be different when the parameter types are different.
static void display(int number, String text) {
System.out.println(number + " " + text);
}
static void display(String text, int number) {
System.out.println(text + " " + number);
}
Can We Overload Only by Return Type?
No. Java does not allow methods to be overloaded only by changing the return type.
int calculate(int a) {
return a;
}
double calculate(int a) {
return a;
}
Static Method Overloading
Static methods can also be overloaded.
class MathUtil {
static int multiply(int a, int b) {
return a * b;
}
static int multiply(int a, int b, int c) {
return a * b * c;
}
}
Constructor Overloading
Constructors can also be overloaded by providing different parameter lists.
class Student {
String name;
int age;
Student() {
name = "Unknown";
age = 0;
}
Student(String name) {
this.name = name;
age = 0;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Method Overloading and Type Promotion
Java may perform primitive type widening when looking for a matching overloaded method.
static void show(long value) {
System.out.println("long");
}
public static void main(String[] args) {
int number = 10;
show(number);
}
The int value can be widened to
long, so the method is valid.
Ambiguous Method Calls
Overloaded methods can become ambiguous when more than one method is equally suitable for the supplied argument.
static void show(int value) {
System.out.println("int");
}
static void show(long value) {
System.out.println("long");
}
static void show(float value) {
System.out.println("float");
}
Choosing suitable parameter types carefully helps avoid confusing overloaded APIs.
Compile-Time Polymorphism
The compiler selects the appropriate overloaded method based on the method signature and arguments available at compile time.
static int calculate(int a, int b) {
return a + b;
}
static double calculate(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(calculate(10, 20));
System.out.println(calculate(10.5, 20.5));
}
Overloading vs Overriding
| Feature | Overloading | Overriding |
|---|---|---|
| Purpose | Same method name with different parameters. | Subclass provides a new implementation. |
| Parameter List | Must be different. | Normally remains the same. |
| Binding | Compile time. | Runtime. |
| Inheritance Required? | No. | Yes, normally. |
| Polymorphism | Compile-time polymorphism. | Runtime polymorphism. |
Practical Example: Area Calculator
class AreaCalculator {
double area(double radius) {
return Math.PI * radius * radius;
}
double area(double length, double width) {
return length * width;
}
double area(int side) {
return side * side;
}
}
public class Main {
public static void main(String[] args) {
AreaCalculator calculator = new AreaCalculator();
System.out.println(calculator.area(5));
System.out.println(calculator.area(10.0, 20.0));
System.out.println(calculator.area(5.0));
}
}
Best Practices
- Keep overloaded methods logically related.
- Use clear and consistent parameter types.
- Avoid overloads that create confusing or ambiguous calls.
- Do not overload methods only by changing the return type.
- Keep method behavior predictable across overloads.
Interview Questions
Summary
Method overloading allows multiple methods to share the same name while having different parameter lists. It is a form of compile-time polymorphism and improves API usability when related operations require different inputs.