Loading…
Loading…
Charting your lesson…
Opening your lesson…
Inheritance
Lesson 3 of 5
Deriving a class from another to reuse and extend behavior via an is-a relationship — plus its pitfalls vs composition.
6 steps•about 10 min•8 flashcards
Step 1/ 61 min
Inheritance is the OOP pillar most abused by beginners and most scrutinized by interviewers. Knowing when to inherit — and when to prefer composition — is what separates "I know the syntax" from "I can design."
Step 2/ 61 min
Inheritance lets a class (the subclass/derived/child) acquire the fields and methods of another class (the superclass/base/parent), then extend or override them. It models an is-a relationship: a Dog is an Animal, so Dog inherits from Animal. The goal is code reuse and building specialized types from general ones.
A subclass inherits the parent's members and can:
A constructor in the subclass typically calls the parent constructor first to initialize inherited state.
Some languages allow only single inheritance of classes (Java, C#) to avoid ambiguity, while others allow multiple inheritance (C++). The classic problem with multiple inheritance is the diamond problem: if B and C both inherit from A and D inherits from both, which A does D get? Languages solve this via interfaces (Java), virtual inheritance (C++), or MRO rules (Python).
Inheritance is tight coupling: subclasses depend on the parent's internals, and deep hierarchies become fragile (a change in the base ripples everywhere — the "fragile base class" problem). Misusing is-a where the relationship is really has-a breaks the Liskov Substitution Principle (a subclass must be usable anywhere its parent is). The widely-taught guidance is "favor composition over inheritance": instead of inheriting, hold a reference to a helper object and delegate. Use inheritance only for genuine is-a specialization; use composition for has-a and for combining behaviors flexibly.
Step 3/ 61 min
Animal defines speak() and eat(). Dog extends Animal, inherits eat(), and overrides speak() to return "Woof". Cat extends Animal and overrides speak() to "Meow". Code that holds an Animal can call speak() on any of them. But if you tried to make a Stack "inherit" from a List just to reuse its methods, you'd expose insert-anywhere operations that violate stack semantics — that's a case for composition (Stack has-a List) instead.
Step 4/ 61 min
Use inheritance when there's a true is-a relationship and subclasses are proper substitutes for the base. Prefer composition when you're reusing code without an is-a bond, when you'd otherwise build a deep/rigid hierarchy, or when behaviors should be mixed and matched at runtime.
Step 5/ 61 min
Remember "inherit for is-a, compose for has-a." If you can't say "the child IS A kind of the parent" truthfully, don't inherit.
Don't inherit just to reuse a few methods. Reaching for inheritance where the relationship isn't truly is-a creates fragile, tightly coupled hierarchies and can violate the Liskov Substitution Principle. Prefer composition (has-a) for pure code reuse.
Step 6/ 6· Worked examples1 min
Problem
Given the classes below, what do d.eat() and d.sound() print, and why?
class Animal {
void eat() { print("eating"); }
String sound() { return "..."; }
}
class Dog extends Animal {
String sound() { return "Woof"; } // override
}
Dog d = new Dog();
Try it on paper first.
Recall
Keep this
Fast revision of inheritance.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.