Method Overloading
Java allows several methods to share a name when their parameter lists differ. This is overloading.
Overloads are useful when one operation has a few natural input shapes. The compiler chooses an overload from the number and types of arguments.
Reuse a method name
public class Main {
static String label(String title) {
return title;
}
static String label(String title, int version) {
return title + " v" + version;
}
public static void main(String[] args) {
System.out.println(label("Core"));
System.out.println(label("Parser", 42));
}
}
Output:
Core
Parser v42
label("Core") calls the one-parameter method. Adding the integer selects the two-parameter version.
Changing only the return type does not create a valid overload. Java must choose a method before it knows how the caller will use the result.
Overload the badge method so a name and level produce text such as Ravi #7.