Updated March 17, 2023
Introduction to Overloading in Java
Java is an Object-Oriented programming language, and it follows basic OOPs concepts. Overloading is one of the important concepts in Java. Overloading allows different methods having the same name in a class but with different signatures. Signature includes the number of parameters, the data type of parameters and the sequence of parameters passed in the method. In Java, the method and the constructors both can be overloaded. Overloading of methods is done at compile-time, and hence it is known as compile-time polymorphism. Overloading of methods in a class is done to increase the readability of programs so that the programmer can write an enhanced implementation of any method for different scenarios.
Consider a scenario in which the programmer wants to find out the volume of various geometric figures like the cube, cylinder, and rectangular prism as three of them have a different formula for finding the volume and have a different number of parameters. The volume of the cube will need only one parameter, the cylinder will take two, and the rectangular prism will take three parameters. But the main purpose is to find the volume of figures only. So we can create separate methods of volume for different figures but with the same name. In this way, Method Overloading is used in practical scenarios.
How does Overloading Work in Java?
Working of Method Overloading is explained with an example:
Code:
class Main{
int volume(int side)
{
return side * side * side;
}
int volume(int length, int breadth, int height)
{
return length*breadth*height;
}
double volume(int radius, int height)
{ return 3.14 * (radius * radius)* height / 3;
}
public static void main(String[] args)
{
Main oe = new Main();
System.out.println("Volume of Cube " +oe.volume(10));
System.out.println("Volume of Rectangular prism " +oe.volume(10,12,30));
System.out.println("Volume of Cone "+oe.volume(5,10));
} }
Output:
Explanation of the above code: In the above example, we need to find the volume of 3 geometric figures, so we have created 3 separate overloaded methods with the same name as ‘volume’, but all the methods have different numbers of arguments. So in the main method, when the one.volume(10) is run, then the method of volume having a single parameter, i.e. int volume(int side), is called. Similarly, for the other methods of volume, on the basis of the number of arguments, different methods are called and processed.
In this example, methods are overloaded on the basis of the number of arguments, but they can also be overloaded on the basis of the data type of parameters and sequence of parameters and hence called according to that only.
Types of Overloading in Java
There are basically 3 ways of Method Overloading in Java:
1. Number of Parameters
Java methods can be overloaded by the number of parameters passed in the method. For example, if the 1 method of volume has 2 parameters and another method has 3 parameters, then it comes under Overloading on the basis of the number of parameters.
Code:
class Multiplication
{
int mult(int a,int b) // method mult having 2 parameters
{
return a*b;
}
//Method Overloading on number of parameters
int mult(int a,int b,int c) // method mult having 3 parameters
{
return a*b*c;
}
}
class Main
{
public static void main(String[] args)
{
Multiplication M = new Multiplication();
System.out.println(M.mult(10,9));
System.out.println(M.mult(10,9,8));
}
}
Output:
2. Datatype of Parameters
The datatype of parameters passed in the method matters a lot, and hence methods can be considered as Overloaded if 2 or methods have the same name having the same or the different number of arguments but different data types of parameters in the different methods.
Code:
class Multiplication
{
int mult(int a,int b) // method mult having 2 parameters
{
return a*b;
}
//Method Overloading on datatype of parameters
double mult(double a,double b) // method mult overloaded
{
return a*b;
}
}
class Main
{
public static void main(String[] args)
{
Multiplication M = new Multiplication();
System.out.println(M.mult(10,9));
System.out.println(M.mult(10.5,9.8));
}
}
Output:
3. Sequence of Parameters
Method Overloading can also be done by changing the sequence of parameters of 2 or more overloaded methods. For example, if the parameters of 1 method are (String x, char y) and the other method is (char x, String y) but both having the same name, then the above 2 methods are considered to be Overloaded by a different sequence of parameters.
Code:
class Employee
{
void details(String name, char rank) // method details having 2 parameters
{
System.out.println("Employee name is "+name);
System.out.println("Employee ranking is "+rank);
} //Method Overloading on sequence of parameters
void details(char rank, String name) // method details overloaded
{
System.out.println("Employee name is "+name);
System.out.println("Employee ranking is "+rank);
}
}
class Main
{
public static void main(String[] args)
{ Employee emp = new Employee();
emp.details("Rajesh", 'A'); // calls the first method (details(String, char))
emp.details("Ram", 'B'); // calls the second method (details(char, String))
}
}
Output:
Explanation of the above code: In the above example, both the methods of ‘details’ are overloaded on the basis of sequence if parameters. When the statement emp.details(‘Rajesh’, ‘A’) is called, then the method having the arguments in the order(String, char), i.e. void details (String name, char rank), is called and processed.
Rules for Overloading in Java
Below are the rules that should be remembered in java overloading:
- The first and foremost rule of Method overloading is that Methods need to have the same name in a single class.
- Two or more methods in a class can be overloaded on the basis of different signatures, and signature includes the number of parameters, data types of parameters, the sequence of parameters as explained above.
- The return type of a method is not a part of the signature, so overloading can never be done on the basis of the return type, and if done, this creates the compile-time error.
Conclusion
Overloading is one of the important concepts in Java and can be done for both methods and constructors. There are some rules of Overloading that should be kept in mind before implementing that in programming.
Recommended Articles
This is a guide to Overloading in Java. Here we discuss some certain rules for overloading that should be considered before implementation important concepts in Java, methods, and constructors. You can also go through our other related articles to learn more –