Inheritance
OOP · 10 interview questions
Inheritance lets a class take on the fields and behaviour of another, so a subclass starts as a copy of its parent and changes only what it needs. It models an is-a relationship: a Manager is an Employee, so Manager can extend Employee.
The reason interviews spend so long here is that inheritance is the most over-used tool in object-oriented programming. It creates the tightest coupling any two classes can have — a subclass depends on its parent's implementation, not just its interface, so a change in the parent can silently break every descendant.
The useful test is substitutability, not vocabulary. If code written against the parent would be surprised by the subclass, the is-a phrasing was a trick of language rather than a fact about behaviour.
Inheritance interview questions
- What is inheritance?
- A mechanism where one class acquires the fields and methods of another, modelling an is-a relationship. The subclass reuses the parent's behaviour and can extend or override it.
- Which languages support multiple inheritance of classes, and which don't?
- C++ and Python allow a class to inherit from several classes. Java, C# and most newer languages do not — they allow only one parent class, but any number of interfaces. The restriction exists to avoid the ambiguity of the diamond problem.
- What is the diamond problem?
- Class D inherits from B and C, which both inherit from A. If B and C each override the same method from A, it is ambiguous which one D should get. C++ resolves it with virtual inheritance; Python uses a defined method resolution order; Java sidesteps it by banning multiple class inheritance.
- Why they ask: Almost always the follow-up to the multiple-inheritance question.
- How does Python decide which parent method to call under multiple inheritance?
- By the method resolution order, computed with the C3 linearisation. It produces a deterministic ordering that respects each parent's own order and guarantees a class appears before its own parents.
- What's the difference between an is-a and a has-a relationship?
- Is-a means a subtype: a Car is a Vehicle, so inheritance fits. Has-a means containment: a Car has an Engine, so composition fits. Choosing inheritance for a has-a relationship is the most common OOP design error.
- In what order do constructors run in an inheritance chain?
- Base class first, then derived. The parent must be fully constructed before the child's constructor body runs, since the child may depend on the parent's state.
- Why is calling an overridable method from a constructor dangerous?
- The subclass's override runs before the subclass's own fields are initialised, so it can observe them as null or zero. In C++ the base constructor calls the base version instead, which surprises people the other way.
- Why they ask: A strong senior-level question — it catches people who know the rules but not the consequences.
- Why would you deliberately prevent a class from being inherited?
- To keep behaviour guaranteed. If a class has invariants that a subclass could break, or is designed to be immutable, sealing it removes that risk. Java's final, C#'s sealed and Kotlin's default-final all exist for this.
- What is the fragile base class problem?
- A seemingly safe change to a base class breaks subclasses, because they depended on its internal behaviour rather than its contract — for example, a parent method that starts calling another overridable method internally.
- What can an override legally change about a method's signature?
- It cannot narrow visibility, and it cannot add new checked exceptions in languages that have them. Many languages allow a covariant return type — returning a more specific type than the parent declared.
You'll forget most of this by next week
That's not a discipline problem, it's how memory works. In the app these come back on an expanding schedule — right before you'd lose them.
Start free for 7 days