Java Method Overloading – Learn with Simple Examples
Published: 13 Oct, 2026 | Author: CCI Admin | Category: Java / Programming
Method overloading is an important concept in Java programming. It allows us to create multiple methods with the same name but with different parameter lists. This makes programs easier to read, understand, and maintain.
In this guide from CCI Computer Education, Pudukkottai, you will learn method overloading through simple Java programs and practical examples.
1. What Is Method Overloading in Java?
Method overloading means defining two or more methods in the same class with the same name but different parameter lists.
The parameters can differ in:
- Number of parameters
- Types of parameters
- Order of parameter types
Method overloading is an example of compile-time polymorphism because Java determines which overloaded method to call during compilation.
2. Simple Example of Method Overloading
Consider a class that contains two methods named display(). One method has no parameters, while the other accepts a name.
class Student {
void display() {
System.out.println("Welcome to Java");
}
void display(String name) {
System.out.println("Welcome, " + name);
}
public static void main(String[] args) {
Student s = new Student();
s.display();
s.display("Arun");
}
}
Output:
Welcome to Java
Welcome, Arun
Both methods have the same name, display(), but their parameter lists are different. Java selects the appropriate method based on the arguments supplied in the method call.
3. Method Overloading by Number of Parameters
We can overload a method by changing the number of parameters.
class Addition {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Addition obj = new Addition();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
}
}
Output:
30
60
The first method accepts two integers, while the second accepts three integers. Both methods are named add().
4. Method Overloading by Data Type
Another way to overload methods is by changing the data types of their parameters.
class Calculator {
void show(int number) {
System.out.println("Integer: " + number);
}
void show(double number) {
System.out.println("Double: " + number);
}
public static void main(String[] args) {
Calculator c = new Calculator();
c.show(10);
c.show(10.5);
}
}
Output:
Integer: 10
Double: 10.5
When an integer argument is supplied, Java selects the show(int) method. When a double argument is supplied, Java selects the show(double) method.
5. Method Overloading by Order of Parameters
Methods can also be overloaded by changing the order of different parameter types.
class Details {
void display(String name, int age) {
System.out.println(name + " is " + age + " years old");
}
void display(int age, String name) {
System.out.println("Age: " + age + ", Name: " + name);
}
public static void main(String[] args) {
Details d = new Details();
d.display("Arun", 20);
d.display(21, "Priya");
}
}
Output:
Arun is 20 years old
Age: 21, Name: Priya
The first method takes a String followed by an int. The second takes an int followed by a String.
6. Rules of Method Overloading
- Overloaded methods must have the same method name.
- Their parameter lists must differ by number, type, or order.
- Changing only the return type does not create a valid overload.
- Overloaded methods may have different return types when their parameter lists are also different.
- Both static and instance methods can be overloaded.
Important Example: Return Type Alone Is Not Enough
int calculate(int a) {
return a;
}
// Invalid overload: same name and same parameters
double calculate(int a) {
return a;
}
This code will not compile because the methods have identical parameter lists. Changing only the return type is not sufficient to overload a method.
7. Method Overloading vs Method Overriding
| Feature | Overloading | Overriding |
|---|---|---|
| Meaning | Same method name with different parameter lists | Subclass provides a new implementation of an inherited method |
| Classes | Usually within the same class | Requires inheritance |
| Parameters | Must differ | Must match the overridden method's signature |
| Polymorphism | Compile-time | Runtime for instance methods |
8. Real-World Example of Method Overloading
Imagine a calculator application. Users may want to add two numbers or three numbers. Method overloading allows the calculator to use the same method name for both operations.
class SimpleCalculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
SimpleCalculator calc = new SimpleCalculator();
System.out.println("Two numbers: " + calc.add(5, 10));
System.out.println("Three numbers: " + calc.add(5, 10, 15));
}
}
Output:
Two numbers: 15
Three numbers: 30
This approach keeps related operations under one meaningful method name while allowing different inputs.
9. Common Mistakes to Avoid
- Changing only the return type and expecting a valid overload.
- Using the same parameter types in the same order for two methods.
- Confusing method overloading with method overriding.
- Assuming Java always selects a method based only on the return type.
- Creating ambiguous calls when multiple overloads are equally suitable.
10. Faculty Tip
At CCI Computer Education, students can understand method overloading by creating a calculator class with overloaded methods for addition, subtraction, and displaying different types of values. Encourage students to change the arguments and observe which method Java selects.
11. Practice Task for Students
- Create a class with two overloaded methods named
multiply()to multiply two and three integers. - Create overloaded methods named
printData()that accept an integer and a string. - Write a program that overloads a method using different parameter types.
- Try creating two methods that differ only by return type. Observe the compilation error and explain why it occurs.
12. Frequently Asked Questions (FAQs)
1. What is method overloading in Java?
Method overloading allows multiple methods in a class to share the same name while having different parameter lists.
2. Is method overloading compile-time polymorphism?
Yes. Java generally resolves an overloaded method call during compilation based on the method signature and the supplied arguments.
3. Can we overload a method by changing only its return type?
No. The parameter list must also differ. Changing only the return type does not create a valid overload.
4. Can static methods be overloaded?
Yes. Static methods can be overloaded if their parameter lists differ.
5. What is the difference between overloading and overriding?
Overloading uses the same method name with different parameter lists. Overriding allows a subclass to provide its own implementation of an inherited instance method.
Conclusion
Method overloading is a useful Java feature that allows programmers to define multiple methods with the same name for different inputs. By changing the number, type, or order of parameters, developers can make code more readable and reusable.
Practise these examples and strengthen your Java programming skills with CCI Computer Education, Pudukkottai.
What to Learn Next
- Java Method Overriding Explained
- Java Inheritance Explained with Examples
- Java Polymorphism – Compile-Time and Runtime
Related CCI Blogs
- Introduction to Java Programming
- Java Variables and Data Types
- Java Classes and Objects
- Java Constructors Explained with Simple Examples
- Java Methods – Create Reusable Code with Simple Examples
Learn Java Programming with CCI Computer Education
Want to learn Java programming from the basics to advanced concepts? Join CCI Computer Education, Pudukkottai, and develop your programming skills through practical examples and projects.
Call: 98427 97945 | Website: www.ccipudukkottai.com
WhatsApp Channel: Follow CCI Computer Education
CCI Computer Education – Learn Today, Build Tomorrow.