Key Points
- Method overloading in Java allows for better organization within a program and makes it easier to interface with existing parts.
- Method overloading is when multiple methods in a class have the same name but different numbers/types of parameters.
- You can overload methods by changing the number of arguments, the data types of the arguments, or the order of the arguments.
Method overloading in Java is among the essential concepts to master if you are trying to learn this programming language. Generally speaking, it allows for better organization within a program. It can also make it more convenient for you to build on the program by making it easier to interface with existing parts.
In today’s article, we’re breaking down method overloading in Java and offering up a few examples to show you exactly how it works. We’ll also go over a few helpful tips and tricks, and important points you should know. Let’s get into it!
What Is Method Overloading in Java?
Method overloading is when multiple methods in a class have the same name but different numbers/types of parameters. This can prove especially helpful when you have methods that perform similar tasks but require different inputs.
Why Is it Useful?
To understand why method overloading is useful, we’ll have to explore a few examples. Let’s take a look at the code for a class called Rectangle:
public class Rectangle {
public double calculateAreaDifferentSides(int length, int width) {
return length * width;
}
public double calculateAreaSameSides(int side) {
return side * side;
}
}
In the class, we have two methods: calculateAreaSameSides for when the rectangle is a square and will have sides that are the same length, and calculateAreaDifferentSides for when it is not.
As can be seen, both of them are calculating the area, but the former only needs one input, while the latter needs two. When a situation like this occurs, method overloading comes in handy. We can instead write:
public class Rectangle {
public double calculateArea(int length, int width) {
return length * width;
}
public double calculateArea(int side) {
return side * side;
}
}
Ways You Can Overload Methods
Method overloading can be achieved by changing the number of parameters or arguments, the data types of the arguments, or the order of the arguments. Let’s take a peek at a few examples to show you the differences in each technique.
Changing the Number of Arguments
Methods can be overloaded by adding or subtracting from the number of arguments. Here’s an example:
public class ChangeArgNumber {
public void display(int number) {
System.out.println("Number: " + number);
}
public void display(int number1, int number2) {
System.out.println("Number 1: " + number1);
System.out.println("Number 2: " + number2);
}
}
Since the number of arguments is different, Java will know which method you are using depending on how many arguments are inputted.
Changing the Types of Arguments
It is also possible to overload methods by changing the data types of the arguments, like so:
public class ChangeArgTypes {
public void display(int number) {
System.out.println("Integer: " + number);
}
public void display(double number) {
System.out.println("Double: " + number);
}
}
When using this method, Java will determine the correct one based on the type of argument inputted.
Changing the Order of Arguments
You do not necessarily have to change the number of arguments or their types to overload a method. You can also reorder the arguments. Here’s an example:
public class ChangeArgOrder {
public void display(int number1, double number2) {
System.out.println("Integer: " + number1);
System.out.println("Double: " + number2);
}
public void display(double number1, int number2) {
System.out.println("Double: " + number1);
System.out.println("Integer: " + number2);
}
}
Although both methods take in a double and an integer, Java will know which one to use based on the order you input them when calling the method.
Can You Change the Return Type?
Sure, you can change the return type when overloading a method. But the return type does not necessarily matter, and won’t overload the method. In fact, Java will give an error when compiling. But why?
Java cannot differentiate methods based on the return type only. This makes sense, as when you are calling a method, only the arguments you pass can vary.
When Could You Use Method Overloading in Java?
So, now that you have a grasp on how method overloading works, when would you put it to use in Java? There are a few times it can come in handy.
Convenience
Method overloading can be used to make classes more convenient. Here’s an example:
public class Converter {
public int convertToInteger(String number) {
return Integer.parseInt(number);
}
public int convertToInteger(double number) {
return (int) number;
}
}
In this class, there is a method convertToInteger that takes in an argument and returns an integer equivalent. There may be multiple situations in which you want to convert something to an integer, but they may not always be the same type.
To save you the trouble of figuring out how to convert it every time, you can just overload a method that takes care of those details for you.
Math
When writing a method that performs some kind of calculation, you may not know if you’ll be dealing with integers, floating point numbers, or some other type.
When performing a task with these methods, you don’t want to have to convert the numbers beforehand, so you could write a method like this:
public class MathUtils {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
Although it’s unnecessary to write a method for adding in Java, this illustrates the point. No matter if you’re dealing with integers or doubles, you can use the add method.

©History-Computer.com
Method Overloading in Java: Wrapping Up
While it may not always be needed, method overloading is an important concept for a Java developer to understand. It is just one more thing you can add to your coding toolbox to make your code easier to read and maintain. Now, let’s address some commonly asked questions about method overloading.
The image featured at the top of this post is ©FOTOGRIN/Shutterstock.com.