The family recipe, tweaked
Your grandmother's cake recipe gets passed down, and each generation adds its own twist without rewriting the whole thing. Inheritance lets a class build on another, and polymorphism means the same 'bake' instruction can behave differently for each version.
Build on what exists
Inheritance lets one class (the subclass) reuse the fields and methods of another (the superclass) with `extends`. The subclass can add new behavior or override inherited methods to behave differently.
class Animal { void speak() { print("..."); } }
class Dog extends Animal {
void speak() { print("Woof"); } // override
} Lab · Who speaks?
- Animal has speak() printing '...'.
- Dog extends Animal and overrides speak() to print 'Woof'.
- Call speak() on a Dog object.
What you should see: The Dog's overridden speak() runs and prints 'Woof' — that's polymorphism in action.