Method Overloading vs Overriding
Method overloading means defining several methods with the same name in the same class but with different parameter lists (different number or types of parameters). The compiler chooses which one to call, so it is compile-time polymorphism.
Method overriding means a subclass provides its own implementation of a method already defined in its parent class, with the same name and parameters. The method that runs is chosen at run time from the object's actual type, so it is run-time polymorphism.
class Calc {
int add(int a, int b) { return a + b; } // overloading
double add(double a, double b) { return a + b; }
}
class Animal { String sound() { return "..."; } }
class Cat extends Animal {
@Override
String sound() { return "Meow"; } // overriding
}