Junior — Middle
Can you explain the concept of method overloading in Java?
sobes.tech AI
Answer from AI
Method overloading in Java is the ability to create multiple methods with the same name but different parameters (type, number, or order). This allows using a single method name for different behaviors.
For example:
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
Here, the add method is overloaded for different types and numbers of arguments. When called, the compiler selects the appropriate method based on the signature.
Overloading improves code readability and convenience, allowing the use of a single name for similar operations.