Method Overloading
Part of the Fundamentals section of Coddy's Java journey — lesson 54 of 73.
In Java, method overloading allows you to define multiple methods with the same name but with different parameters. This means you can have several methods that perform a similar function but operate on different data types or a different number of arguments.
The key is that each overloaded method must have a unique method signature. The method signature is determined by the method's name and the type and order of its parameters.
Here are the key rules for method overloading:
- Same Name: All overloaded methods must have the same name.
- Different Parameters: Each version of the method must have a different number of parameters, different types of parameters, or both.
- Return Type is Not Enough: Overloaded methods cannot differ only by their return type. They must have different parameter lists.
Here's an example to illustrate method overloading:
public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
public static int add(int a, int b, int c) {
return a + b + c;
}
}In this example, we have three methods named add. The first adds two integers, the second adds two doubles, and the third adds three integers.
Java determines which method to call based on the arguments passed during the method call.
Method overloading makes code more readable and reusable. Instead of creating different names for methods that perform similar tasks (e.g., addInts, addDoubles, addThreeInts), you can use a single name, add, and let the compiler choose the appropriate method based on the context.
Remember: overloading requires different parameter types or numbers, not just different parameter names or return types.
Cheat sheet
Method overloading allows multiple methods with the same name but different parameters:
Rules for method overloading:
- Same method name
- Different parameters (number, type, or order)
- Return type alone is not sufficient for overloading
public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
public static int add(int a, int b, int c) {
return a + b + c;
}
}Java determines which method to call based on the arguments passed during the method call.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison10Methods (Functions)
Declaring MethodsMethod ParametersReturn TypesMethod OverloadingRecap - Sigma FunctionRecap - Validation FunctionVoid Methods5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else