Video in progress

Explain the difference between overloading and overriding.

Overloading / Overriding — Where: Usually in one class / In a parent and a child class; Signature: The number/types of parameters differ / The signature is the same; Return type: Doesn't matter (if it's the only difference, it's an error) / The same or a narrower type (covariant); Resolved: At compile time / At…

📖 Answer

Overloading Overriding
Where Usually in one class In a parent and a child class
Signature The number/types of parameters differ The signature is the same
Return type Doesn't matter (if it's the only difference, it's an error) The same or a narrower type (covariant)
Resolved At compile time At runtime
static/private Allowed Can't be overridden
class Calculator {
    int add(int a, int b) { return a + b; }
    double add(double a, double b) { return a + b; }   // overloading
}

class Animal { String sound() { return "..."; } }
class Cat extends Animal {
    @Override String sound() { return "miyov"; }      // overriding
}

A common mistake: trying to overload by changing only the return type. int get() and long get() in one class won't compile.