Overloading
Overloading
Overloading in Java
In this tutorial, we shall learn about Overloading in Java. Overloading is the ability to use same name for
different methods with different set of parameters. We shall go through some Java Example Programs in detail
to understand overloading in Java.
If a method can expect different types of inputs for the same functionality it implements, implementing different
methods (with different method names) for each scenario may complicate the readability of code.
For example, we need a functionality to add two numbers. Now these two numbers could be integer values, float
values, long values or double values. If we implement methods like addTwoInts(int a, int b), addTwoFloats(float a,
float b), addIntFloat(int a, float b), etc., it becomes very difficult to use these methods in other classes because
of lack of readability. Java provides the ability to overload a method based on the type of arguments passed in
the function call, provided the methods have a same name.
1. All the methods that take park in Overloading should have the same name.
2. No two methods should have the same set of parameters. They should differ either in
the number of parameters they have in their definition or
the type of parameters they have in their definition
If a method is not found for a set of arguments, the compiler does Type Promotion and checks for a suitable
method definition. We shall learn in detail about Type Promotion with an example in due course of this tutorial.
Note : Also, please note that, different return types do not make two methods different with respect to
Overloading.
/**
* Example program to demonstrate Overloading in Java
*/
public class Calculation {
Run the above program, and you shall get the following output in console.
Output
1. All the five methods that take part in overloading have same method name “add”.
2. No two definitions of add function have same set of arguments. One add method have (int, int), another has
(int, float), etc.
If a method is not found for a set of arguments, the compiler does Type Promotion and checks for a suitable
method definition. Following diagram shows different type promotions that are possible.
If there is forward path between two data types, then the starting datatype in the path can be type promoted to
the datatype at end of the path.
We shall use the same Calculation class, but remove the definition add(int, int). As we have add(int, float), even
we make a call add(int, int), as int can be promoted to float, double or long, we have add(int, float) and add(int,
double) which are potential overloadable methods.
Calculation.java
/**
* Example program to demonstrate Overloading in Java with Type Promotion
*/
public class Calculation {
Run the above program and you shall get the following output in console.
Output
Calculation.java
/**
* Example program to demonstrate Overloading in Java
*/
public class Calculation {
public static void main(String[] args) {
Calculation calc = new Calculation();
System.out.println(calc.add(4, 8));
System.out.println(calc.add(4, 8.5));
System.out.println(calc.add("25", "85"));
System.out.println(calc.add(25, 88888888888888888888888.0));
System.out.println(calc.add(25));
}
For both add(int, int) and add(int, float); add(int, double) has been called i.e., int and float are promoted to
double.
You may experiment with other type promotions and verify the results with the type promotions presented in the
picture.
Conclusion
In this Java Tutorial, we learned about Overloading in Java with an Example program of adding two numbers for
different set of input parameters. Also, we learned type promotions that are possible with different data types in
Java for overloading methods.
✦ Java Tutorial
✦ Java Introduction
✦ Java Installation
✦ Java Datatypes
✦ Java Operators
✦ Java Loops
✦ Java Array
✦ Java OOPs
✦ Java String
✦ Java MySQL
✦ Java Random
✦ Java Math