Method overloading in Java allows us to create multiple methods with the same name but with different parameter lists. It makes programs easier to read and organize.
Method overloading means defining two or more methods in the same class with the same name but different parameters.
For example, a class can have methods named
add() that accept different numbers of
arguments.
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 one
accepts two parameters and the other accepts three.
For example, instead of creating separate methods such
as addTwoNumbers() and
addThreeNumbers(), we can use the same
method name add().
Methods can have the same name but different numbers of parameters.
class Demo {
void show(int a) {
System.out.println("One parameter: " + a);
}
void show(int a, int b) {
System.out.println("Two parameters: " + a + ", " + b);
}
public static void main(String[] args) {
Demo obj = new Demo();
obj.show(10);
obj.show(10, 20);
}
}
One parameter: 10
Two parameters: 10, 20
Methods can also be overloaded by changing the parameter data types.
class Demo {
void display(int number) {
System.out.println("Integer: " + number);
}
void display(String text) {
System.out.println("String: " + text);
}
public static void main(String[] args) {
Demo obj = new Demo();
obj.display(100);
obj.display("Hello Java");
}
}
Integer: 100
String: Hello Java
Methods can be overloaded by changing the order of different parameter types.
class Demo {
void show(int number, String text) {
System.out.println(number + " " + text);
}
void show(String text, int number) {
System.out.println(text + " " + number);
}
public static void main(String[] args) {
Demo obj = new Demo();
obj.show(10, "Java");
obj.show("Python", 20);
}
}
10 Java
Python 20
No. Changing only the return type does not create a valid overloaded method.
// Invalid Java code
int calculate(int a) {
return a * 2;
}
// Compilation error: same name and parameter list
double calculate(int a) {
return a * 2.0;
}
To overload the method, change its parameter list.
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 static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20));
System.out.println(calc.add(2.5, 3.5));
System.out.println(calc.add(10, 20, 30));
}
}
30
6.0
60
The compiler selects the appropriate
add() method based on the arguments.
Static methods can also be overloaded. Each overloaded method must have a distinguishable parameter list.
class Demo {
static void print(int a) {
System.out.println("Number: " + a);
}
static void print(String text) {
System.out.println("Text: " + text);
}
public static void main(String[] args) {
print(50);
print("Welcome");
}
}
Number: 50
Text: Welcome
| Method Overloading | Method Overriding |
|---|---|
| Usually happens in the same class. | Happens between a parent and child class. |
| Parameter lists must differ. | Method signature must match for a valid override. |
| Compile-time polymorphism. | Runtime polymorphism for instance methods. |
| Inheritance is not required. | Inheritance is normally required. |
Mistake 1: Changing only the return type
int getValue(int a) {
return a;
}
// Invalid: same parameter list
double getValue(int a) {
return a;
}
Mistake 2: Calling a method with the wrong arguments
void show(int a) {
System.out.println(a);
}
// show("Hello"); // Compilation error
Always check the method's parameter types and number of arguments before calling it.
class StudentMarks {
int calculate(int english, int maths) {
return english + maths;
}
int calculate(int english, int maths, int science) {
return english + maths + science;
}
public static void main(String[] args) {
StudentMarks student = new StudentMarks();
System.out.println(
"Total of two subjects: " +
student.calculate(80, 75)
);
System.out.println(
"Total of three subjects: " +
student.calculate(80, 75, 90)
);
}
}
Total of two subjects: 155
Total of three subjects: 245
This example uses the same method name to calculate totals for different numbers of subjects.
show(), one accepting an integer and
another accepting a string.
add() method that adds two
integers and another that adds three integers.
area() methods to
calculate the area of a square and a rectangle.
display() methods
that accept an integer, a double, and a string.
1. What is method overloading?
2. Can methods be overloaded by changing only the return type?
3. Which is a valid way to overload a method?