Loading…
Loading…
Charting your lesson…
Opening your lesson…
Inheritance and the 'is-a' Relationship
Lesson 3 of 4
6 steps•about 12 min•12 flashcards
Step 1/ 61 min
Inheritance lets a class reuse and extend another's members. It is the OOP pillar most often misused — beginners inherit for code reuse when they should compose — so interviewers probe whether you know when it actually fits.
Core idea: inheritance creates an "is-a" relationship: a subclass (derived/child) is a specialised kind of its superclass (base/parent), inheriting its fields and methods and optionally adding or overriding behaviour. A Dog is an Animal; a SavingsAccount is a BankAccount.
Step 2/ 61 min
class Animal {
String name;
void eat() { System.out.println(name + " eats"); }
}
class Dog extends Animal { // Dog inherits name + eat()
void bark() { System.out.println("woof"); }
}
Dog d = new Dog();
d.eat(); // inherited
d.bark(); // added
Dog gets everything Animal has, for free, plus its own members.
A subclass can override an inherited method to specialise it, and call the parent version via super:
class Cat extends Animal {
@Override
void eat() {
super.eat(); // reuse parent behaviour
System.out.println("delicately");
}
}
Constructors are not inherited but are chained: a subclass constructor implicitly calls super() first so the parent part of the object is initialised before the child part.
Java/C# allow single class inheritance (one base) to avoid the diamond problem — the ambiguity when two parents provide the same method. C++ allows multiple inheritance and resolves diamonds with virtual base classes. A deeper caution is the fragile base class problem: changing a base class can silently break subclasses that depended on its internals. This is why the guidance is often "favour composition over inheritance" — an object that has-a engine is more flexible than one that is-an engine.
Step 3/ 61 min
// GOOD: genuine is-a
class ElectricCar extends Car { ... } // an ElectricCar IS A Car
// BAD: forcing is-a for reuse
class Stack extends ArrayList { ... } // a Stack is NOT AN ArrayList!
// -> exposes add(index, e), remove(index)... breaking stack invariants
// BETTER: composition (has-a)
class Stack {
private final List<Integer> items = new ArrayList<>(); // Stack HAS A list
void push(int x) { items.add(x); }
int pop() { return items.remove(items.size()-1); }
}
Extending ArrayList leaks unwanted operations that violate LIFO. Composing a private list exposes only stack operations. The test: say "X is-a Y" out loud — if it sounds wrong, use composition.
Step 4/ 61 min
Common asks: "what is the is-a relationship / when to use inheritance vs composition", "the diamond problem and how languages avoid it", "does a subclass inherit constructors?" (no — but they are chained via super), and "what is method overriding?" Expect the phrase "favour composition over inheritance" and be ready to justify it.
Step 5/ 61 min
super() finishes — don't call overridable methods from a constructor.Using inheritance for code reuse when the relationship is not truly "is-a" (e.g. Stack extends ArrayList). The subclass inherits methods that break its own invariants and becomes tightly coupled to the base's internals. If you cannot honestly say "child is-a parent", compose the functionality instead of inheriting it.
super to reuse the parent version.super() runs first).Step 6/ 6· Worked examples2 min
Problem
A team makes Square extend Rectangle because "a square is a rectangle". Show the code that breaks, and explain why inheritance was the wrong tool (the Liskov test).
class Rectangle {
protected int w, h;
void setWidth(int x) { w = x; }
void setHeight(int x) { h = x; }
int area() { return w * h; }
}
class Square extends Rectangle { // "is-a" ... really?
void setWidth(int x) { w = h = x; } // keep sides equal
void setHeight(int x) { w = h = x; }
}
Try it on paper first.
Recall
Keep this
Inheritance is an OOP pillar and a design-judgement question: interviewers ask "inheritance vs composition" to see if you know when not to inherit. The diamond problem and constructor order are common deeper probes.
Inheritance lets a derived (child) class reuse and extend a base (parent) class, modelling an "is-a" relationship (a Dog is an Animal). The child inherits the parent's members and can add or override behaviour.
| Type | Structure |
|---|---|
| Single | one parent → one child |
| Multilevel | A → B → C |
| Hierarchical | one parent, many children |
| Multiple | many parents (classes: C++ only, not Java) |
With multiple inheritance, if two parents share a common grandparent, the child gets an ambiguous duplicate. C++ solves it with virtual inheritance; Java sidesteps it by disallowing multiple class inheritance.
Constructors run base-class first, then derived (the parent must be fully built before the child).
"Inheritance vs composition", "what is the diamond problem?", "constructor call order in inheritance", "types of inheritance", and "is-a vs has-a".
Inheritance = "is-a" reuse/extension; child inherits + overrides parent. Types: single/multilevel/hierarchical/multiple (classes: not in Java). Diamond problem = ambiguous multiple inheritance (C++ virtual inheritance solves it). Constructors run parent→child. Favour composition ("has-a") when there's no true is-a.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.