Method overloading
Lesson content
- What is method overloading
- Most common errors
What is method overloading
Attribute names within a class must be unique - two attributes cannot have the same name.
With methods, this is not the case. Namely, it is possible to write two or more methods with the same name within a class.
However, in order to distinguish between them, each of them must have a different list of parameters — whether it is the number of parameters or their type (or order, if changing the order results in a different list of parameters, for example, (int, String) vs. (String, int)). When calling a method, the method whose parameters exactly match the input values will be invoked. In this case, it is method overloading.
Furthermore, the return type of the method is not relevant for overloading. If two methods in the same class have the same name and the same list of parameters, differing only in the return type, overloading will not be possible, and Java will report a syntax error.
Example 1
Create the Calculator class that has four methods:
- A method named addIntegers that takes two integers as parameters and returns their sum (an integer).
- A method named addDecimals that takes two real numbers as parameters and returns their sum (a real number).
- A method named addIntegersThree that takes three integers as parameters and returns their sum (an integer).
- A method named addDecimalsThree that takes three real numbers as parameters and returns their sum (a real number).
1 class Calculator {
2
3 int addIntegers (int x, int y){
4 int result = x+y;
5 return result;
6 }
7
8 double addDecimals (double x, double y){
9 double result = x+y;
10 return result;
11 }
12
13 int addIntegersThree (int x, int y, int z){
14 int result = x+y+z;
15 return result;
16 }
17
18 double addDecimalsThree (double x, double y, double z){
19 double result = x+y+z;
20 return result;
21 }
22 }
It is important to note that these four methods are essentially almost identical, but they have been given different names. If there were a need to create more versions of these methods for summing, additional names would have to be added.
The following example contains the code for a different version of the Calculator class - with method overloading used.
Example 2
Create the Calculator2 class that has four overloaded methods with the same name - add:
- The first method takes two integers as parameters and returns their sum (an integer).
- The second method takes two real numbers as parameters and returns their sum (a real number).
- The third method takes three integers as parameters and returns their sum (an integer).
- The fourth method takes three real numbers as parameters and returns their sum (a real number).
1 class Calculator2 {
2
3 int add (int x, int y){
4 int result = x+y;
5 return result;
6 }
7
8 double add (double x, double y){
9 double result = x+y;
10 return result;
11 }
12
13 int add (int x, int y, int z){
14 int result = x+y+z;
15 return result;
16 }
17
18 double add (double x, double y, double z){
19 double result = x+y+z;
20 return result;
21 }
22 }
Write a TestCalculator class that calls all of these methods.
1 class TestCalculator {
2
3 public static void main(String[] args) {
4 Calculator2 c = new Calculator2();
5
6 double r = c.add(12.0, 15.0);
7 System.out.println(r);
8 //It will print:
9 //27.0
10
11 int r2 = c.add(12, 15);
12 System.out.println(r2);
13 //It will print:
14 //27
15
16 double r3 = c.add(12.0, 15.0, 34.0);
17 System.out.println(r3);
18 //It will print:
19 //61.0
20
21 int r4 = c.add(12, 15, 34);
22 System.out.println(r4);
23 //It will print:
24 //61
25 }
26 }
The first call to the add method activates the method that sums two real numbers and returns a real number as a result.
The second call to the add method activates the method that sums two integers and returns an integer as a result.
In the first case, real numbers were entered as parameters, and in the second case, integers.
Similarly, the next two calls activate the overloaded versions of the add method that sum three real or three integer numbers respectively and return a real or integer number as the result.
What is the purpose of method overloading? In cases where multiple similar versions of a method are needed (with the same or almost the same functionality), overloading is used so that:
- You don’t have to come up with new names for each
- version (e.g., “addIntegers”, “addReals”, “addIntegersThree”…)
- It shows the relationship between these methods through the same name.
- It makes it easier to find the most suitable version of the method that needs to be called.
Most common errors
Some of the most common syntax errors when writing overloaded methods include:
- Writing overloaded methods (with the same name) that do NOT differ in parameters (number, type, and/or order), but only in the return value, for example:
1 int add (int a, int b){
2 return a + b;
3 }
4
5 double add (int a, int b){
6 return a + b;
7 }
instead of (correctly):
1 int add (int a, int b){
2 return a + b;
3 }
4
5 double add (double a, doubl b){
6 return a + b;
7 }