Overloaded methods in Java are methods that have the same name but different the parameter list. In this tutorial, we will learn how to create and use the overloaded methods in Java.
Some of the guidelines below for defining our overloaded methods are:
- Overloaded methods must be of the same name but differ in parameters.
- They can be defined with the same or different types of data returned.
- They can be defined the same or different access modifier.
- Methods are not called overloaded if they differ only in the return data type or access modifier.
In the example below we have two overloaded methods named printMessage():
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.huongdanjava; public class Example { public void printMessage(String name) { System.out.println("Name=" + name); } public void printMessage(int age) { System.out.println("Age=" + age); } } |
And the same parameter, but the data types of those parameters are different: a String type, an int type.
Parameters in overloaded methods
The list of parameters in the overloaded methods must be different, which means:
- The number of parameters is different.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.huongdanjava; public class Example { public void printMessage(String name) { System.out.println("Name=" + name); } public void printMessage(String name, int age) { System.out.println("Name=" + name + ", age=" + age); } } |
- The number of parameters may be the same but different in terms of data type in order of parameter list.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.huongdanjava; public class Example { public void printMessage(String name, int age) { System.out.println("Name=" + name + ", age=" + age); } public void printMessage(int age, String name) { System.out.println("Name=" + name + ", age=" + age); } } |
One thing to keep in mind is that there are data types that can be converted to other types of data, for example from int to double, so even though we can declare overloaded methods with them, there are cases where we will have compile errors.
For example, we have two printMessage() methods defined as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.huongdanjava; public class Example { public void printMessage(String name, int age, double mark) { System.out.println("Name=" + name + ", age=" + age + ", mark=" + mark); } public void printMessage(String name, double mark, int age) { System.out.println("Name=" + name + ", age=" + age + ", mark=" + mark); } } |
But when used in the case below, will be compile error:
Since the int type can be converted to double, in this case, the compiler does not know what method we are calling.
To be usable, we need to explicitly declare the type of data we pass into:
Data type returned
Methods are not called overloaded if they differ only in the type of data returned and in this case our code cannot be compiled at all.
For example:
Access modifier
Similar to the return data type, the methods are also not called overloaded if they differ only in the access modifier and our code also has a compile error if we define it as such.
For example: