Java Method Overloading
Sometimes the same job needs slightly different inputs: adding two whole numbers, adding two decimal numbers, adding three numbers. Instead of inventing names like addInt, addDouble and addThree, Java lets you give all of them the same name. This is called method overloading. You have already used it: System.out.println is overloaded for text, whole numbers, decimals, characters and booleans.
Same name, different parameters
Overloaded methods must differ in their parameter list: the number of parameters, their types, or their order. Java chooses the method whose parameters match the arguments of the call:
public class Main {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
System.out.println(add(2, 3));
System.out.println(add(2.5, 3.1));
System.out.println(add(1, 2, 3));
}
}5 5.6 6
The first call has two int arguments, so the first method runs. The second has two double arguments, and the third has three int arguments.
The return type is not enough
Two methods that differ only in their return type cannot be overloaded: from a call like area(5) Java could not tell which one you mean.
public class Main {
static int area(int side) {
return side * side;
}
static double area(int side) {
return side * side * 1.0;
}
public static void main(String[] args) {
System.out.println(area(5));
}
}Main.java:6: error: method area(int) is already defined in class Main
Overloading with different meanings
Overloading is ideal when the methods do the same kind of job on different inputs. Here describe works for a number, a piece of text and an array:
public class Main {
static String describe(int n) {
return n + " is " + (n % 2 == 0 ? "even" : "odd");
}
static String describe(String s) {
return "\"" + s + "\" has " + s.length() + " characters";
}
static String describe(int[] values) {
return "an array of " + values.length + " numbers";
}
public static void main(String[] args) {
System.out.println(describe(7));
System.out.println(describe("Java"));
System.out.println(describe(new int[] {1, 2, 3}));
}
}7 is odd "Java" has 4 characters an array of 3 numbers
How Java chooses with automatic conversion
If no method matches the argument types exactly, Java tries the automatic widening conversions you learned in Type Casting, such as int to long or double:
public class Main {
static void show(long value) {
System.out.println("long: " + value);
}
static void show(double value) {
System.out.println("double: " + value);
}
public static void main(String[] args) {
show(10);
show(10.0f);
}
}long: 10 double: 10.0
The int 10 widens to long (the closest match), and the float widens to double. When two methods match equally well, the call is ambiguous and does not compile, so keep overloads clearly different. Overloading is also how classes offer several constructors.
Practice exercises
- Write two methods
max, one for twointvalues and one for twodoublevalues. - Overload
printBoxso it prints a square of stars for one parameter and a rectangle for two. - Write
area(double radius)for a circle andarea(double width, double height)for a rectangle.