Junior — Middle
Is method overloading an example of implementing the principle of polymorphism in object-oriented programming?
sobes.tech AI
Answer from AI
Yes, method overloading is an example of polymorphism in object-oriented programming, specifically compile-time polymorphism (static polymorphism).
Overloading occurs when multiple methods in the same class have the same name but different parameters (type, number, or order). The compiler selects the appropriate method at compile time based on the arguments passed.
Example:
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
Here, the add method is overloaded to work with both integers and floating-point numbers.