Method Overloading in Java with Example
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
In Java Polymorphism, we heard the term Method Overloading, which allows the methods to have a similar name but with a difference in signatures, which is based on the number or type. Method Overloading in Java supports compile-time (static) polymorphism.
In this article, we will talk about Method Overloading with its rules and methods. We will discuss each and every concept with an example for a clear understanding. So, let’s start!
Example of Methods Overloading in Java-
class Calculator
{
int addition(int operand1, int operand2)
{
return operand1+operand2;
}
int addition(int operand1, int operand2, int operand3)
{
return operand1+operand2+operand3;
}
}
public class CompileTimePolymorphism
{
public static void main(String args[])
{
Calculator obj = new Calculator();
System.out.println("Addition of two operands is "+obj.addition(10, 20));
System.out.println("Addition of three operands is "+obj.addition(10, 20, 30));
}
}
Output-
Different Methods of Method Overloading in Java
Method overloading can be done by 3 methods in Java:
By some parameters in the two methods.
The following example shows how method overloading is done by using a different number of parameters
class Calculator
{
int addition(int operand1, int operand2)
{
return operand1+operand2;
}
int addition(int operand1, int operand2, int operand3)
{
return operand1+operand2+operand3;
}
}
public class CompileTimePolymorphism
{
public static void main(String args[])
{
Calculator obj = new Calculator();
System.out.println("Addition of two operands is "+obj.addition(10, 20));
System.out.println("Addition of three operands is "+obj.addition(10, 20, 30));
}
}Output-
Recommended Reading – DataTypes in Java with Examples
By the data types of the parameters of the methods
In the following example, method addition() is overloaded based on the data type of the parameters. We have two methods with the name addition(), one with the parameter of int type and another method with the parameter of string type.
class Calculator
{
void addition(int operand1, int operand2)
{
System.out.println(operand1+operand2);
}
void addition(String alphabet)
{
System.out.println(alphabet);
}
}
public class CompileTimePolymorphism
{
public static void main(String args[])
{
Calculator obj = new Calculator();
obj.addition(10, 20);
obj.addition("DataFlair");
}
}
By order of the parameters of the methods
Here, both methods have a different sequence of data types in the argument list. The first method has an argument list as (String, char), and the second has (int, String, char). Since the sequence is different, the method can be overloaded without any errors.
It’s the right time to explore Command Line Arguments in Java
public class MethodOverloadingExample2
{
void record(String studentName,char grade)
{
System.out.println("Student name is "+studentName);
System.out.println("Student grade is "+grade);
}
void record(int id,String studentName,char grade)
{
System.out.println("Student ID is "+id);
System.out.println("Student name is "+studentName);
System.out.println("Student grade is "+grade);
}
public static void main(String[]args)
{
MethodOverloadingExample2 Obj=new MethodOverloadingExample2();
Obj.record("Renuka",'B');
Obj.record(8, "Bhumika", 'A');;
}
}Output-
Important Points of Method Overloading in Java
Advantage of Method Overloading
- In method overloading, we don’t have to define different names to the methods.
- The method is called according to the parameter list. For example, an object calling a method containing 2 parameters will call the method having the same parameter list.
- This concept is performed by achieving compile time polymorphism, it means it verifies all the conditions during compile time to safely run the program.
- Debugging is simplified as the errors are detected at compile time.
Have you heard about Method Overriding in Java?
Overloading on methods for a different return type in Java
We cannot perform method overloading for different return types in Java.
Example of method overloading of different types-
public class Main
{
public int number()
{
return 10;
}
// compiler error: number() is already defined
public char number()
{
return 'a';
}
public static void main(String args[])
}Overloading of the main() method in Java
Java supports overloading of the main() method.
Example of overloading the main method in Java-
public class MetodOverloadingExample1
{
// Normal main()
public static void main(String[] args)
{
System.out.println("Hello Readers, Welcome to DataFlair");
}
// Overloaded main methods
public static void main(String arg1)
{
System.out.println("Hi, " + arg1);
MetodOverloadingExample1.main("DataFlair");
}
public static void main(String arg1, String arg2)
{
System.out.println("Hi, " + arg1 + ", " + arg2);
}
}Benefits of Method Overloading in Java
Method overloading offers several advantages in Java programming:
1. Improved Code Readability: By using the same method name for related operations with distinct parameter sets, you enhance code readability. It becomes clear to developers that these methods perform similar tasks but cater to different data types or numbers of arguments.
2. Increased Code Maintainability: Overloading promotes code maintainability as you can modify an overloaded method without affecting code that relies on other versions of the method with different parameter lists.
3. Enhanced Flexibility: Method overloading allows you to design methods that can handle various data types or argument combinations, making your code more adaptable to different use cases.
Summary
Wrapping up, method overloading can be done by 3 methods: by the number of parameters in two methods, the data types of the parameters, and the order of parameters of methods. It is not an easy task; you have to remember some important points, which we discussed above.
Don’t forget to check Constructor Overloading in Java
In order to give suggestions and feedback, please approach our comment section.
Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google






Pages of helpful information. Thanks