Sub topic 8
Overload Methods with Different Parameters
Method overloading lets you use the same method name for different sets of inputs. For example, you might want to add two numbers or three numbers while using the name sum for both tasks.
An overloaded method is one of two or more methods with the same name but different parameter lists. A parameter is a variable in a method declaration that receives a value. An argument is the value supplied when you call the method.
Overloading matters because it gives related actions a consistent name. The caller can choose a version by providing different arguments.
Write overloaded methods
Save this program in a file named SumDemo.java:
class SumDemo {
static int sum(int left, int right) {
return left + right;
}
static int sum(int first, int second, int third) {
return first + second + third;
}
static double sum(double left, double right) {
return left + right;
}
public static void main(String[] args) {
System.out.println(sum(2, 3));
System.out.println(sum(2, 3, 4));
System.out.println(sum(1.5, 2.5));
System.out.println(sum(2, 3.5));
}
}A type describes what kind of value a variable holds. Here, int holds whole numbers, and double holds numbers that can have decimal parts. Each sum method is static, meaning it belongs to the class and can be called without creating an object.
Compile and run the program with these commands:
javac SumDemo.java
java SumDemoThe output is:
5
9
4.0
5.5The first call, sum(2, 3), supplies two int arguments. It uses the first method. The second call supplies three int arguments, so it uses the second method. The third call supplies two double arguments and uses the third method.
In the last call, sum(2, 3.5), the first argument is an int and the second is a double. Java can convert the int value 2 to a double, so this call also uses the third method.
Tell signatures apart
A method signature is its name plus its parameter types, in order. Parameter names and the return type are not part of the signature. The return type says what kind of value a method sends back.
| Signature | How it differs |
|---|---|
| `sum(int, int)` | Takes two whole numbers |
| `sum(int, int, int)` | Takes three whole numbers |
| `sum(double, double)` | Takes two decimal-capable numbers |
These signatures are written in a short form for discussion; they are not complete method declarations.
You can overload a method by changing the number, types, or order of its parameters. For example, sum(int, double) and sum(double, int) have different signatures.
Changing only parameter names does not create a new overload. Neither does changing only the return type. You cannot add another sum(int, int) that returns a double; Java treats it as a duplicate signature.
Understand how Java chooses
The compiler is the tool that translates Java source code into a form Java can run. It checks the number and types of arguments to select an overload. In the example, sum(2, 3) matches sum(int, int) directly, so Java uses that version rather than converting both arguments to double.
A call fails to compile if no overload accepts its arguments. For example, sum("2", "3") does not match any method in the program: the quotes make those arguments text, not numbers.
A call can also be ambiguous, meaning Java cannot choose one best overload. If the only available versions were sum(int, double) and sum(double, int), then sum(1, 1) would be ambiguous: either version would need one int converted to double.
Exercise
In the program above, which overload handles sum(4, 6.0), and what does it return?
Show answer
sum(double, double) handles the call. Java converts 4 to a double, and the method returns 10.0.
Exercise
Can you add a method named sum with two int parameters and a double return type to the program? Why or why not?
Show answer
No. It would have the same signature as the existing sum(int, int). Changing only the return type does not create an overload.