Skip to content
easyjunior5 min read #javascript #prototypes

When should you prefer composition over inheritance? ​

Answer ​

Prefer composition when behavior can be assembled from independent capabilities or when inheritance would create a fragile hierarchy. Inheritance is appropriate only for a stable “is-a” relationship with shared invariants and substitutable subclasses.

Why this matters ​

The important idea behind When should you prefer composition over inheritance is not the terminology alone; it is the engineering decision the concept enables. Objects delegate property lookup through a prototype chain. Classes provide friendlier syntax, but inheritance, property ownership, and this still follow the underlying prototype model. In a real system, the useful question is where the behaviour lives, what assumptions it relies on, and what becomes observable when those assumptions fail. Connecting the definition to those boundaries makes the answer useful for both an interview and day-to-day development.

Explain the underlying mechanism before discussing benefits. That distinction matters because memorized definitions often fail on follow-up questions about edge cases, lifecycle, performance, or production behaviour. For a easy-level question, it is also worth naming one limitation. Doing so shows that you understand when the idea applies instead of treating it as a universal rule.

How to reason about it ​

Start from the guarantee expressed in the direct answer: Prefer composition when behavior can be assembled from independent capabilities or when inheritance would create a fragile hierarchy. Inheritance is appropriate only for a stable “is-a” relationship with shared invariants and substitutable subclasses. Then separate that guarantee from implementation details. Ask what initiates the behaviour, which state or resource it reads, who owns cleanup or recovery, and whether the result is synchronous, asynchronous, persistent, or temporary. These questions expose the edge cases that interviewers usually explore next.

Next, define success in observable terms. A correct solution should produce the intended result for the normal path, remain understandable when input is empty or invalid, and fail without corrupting state. If concurrency, caching, networking, rendering, or persistence is involved, discuss stale data, repeated work, ordering, and partial failure explicitly. The exact concerns vary, but they should follow from When should you prefer composition over inheritance, not from a memorized checklist.

Worked example ​

Consider a team adding a new capability to a multi-tenant SaaS dashboard without disrupting existing customers. In this JavaScript example, the team needs to make a decision specifically about When should you prefer composition over inheritance. They begin with the rule above—Prefer composition when behavior can be assembled from independent capabilities or when inheritance would create a fragile hierarchy. Inheritance is appropriate only for a stable “is-a” relationship with shared invariants and substitutable subclasses. Rather than applying it blindly, they write down the expected input, output, and failure behaviour. They then implement the smallest version that demonstrates the rule and exercise both the successful path and one realistic failure path.

During review, the team asks whether the example would still be correct with repeated requests, missing data, a slow dependency, or a larger workload. Only the cases relevant to this question are kept. Finally, they verify the result at the boundary a user or another system can observe. That may be a focused unit test, an integration test, a browser profile, a log or metric, or a rollback exercise. This turns the concept into evidence rather than an assertion.

Common mistakes and trade-offs ​

A weak answer repeats a definition but never explains consequences. Another common mistake is choosing a tool or pattern before clarifying the requirement. Avoid claiming that one option is always faster, safer, or cleaner; describe the workload and constraints that make the claim true. Also distinguish correctness from optimization: first make the behaviour correct and testable, then use measurements to justify additional complexity.

In production, simpler implementations are generally easier to operate, but simplicity does not mean ignoring error handling, security, accessibility, cleanup, or observability. Add those controls at the boundary where the risk exists. If the solution introduces caching, retries, shared state, abstraction, or background work, explain the invalidation, ownership, or recovery rule as part of the design.

Interview-ready summary ​

Lead with this sentence: Prefer composition when behavior can be assembled from independent capabilities or when inheritance would create a fragile hierarchy. Inheritance is appropriate only for a stable “is-a” relationship with shared invariants and substitutable subclasses. Follow it with the mechanism, one concrete decision from the worked example, and one limitation or trade-off. That structure gives the interviewer a direct answer first while leaving clear openings for deeper follow-up questions.

Copyable example ​

js
// When should you prefer composition over inheritance?
function ConceptExample(value) {
  this.value = value
}

ConceptExample.prototype.explain = function () {
  return this.value
}

const example = new ConceptExample("Prefer composition when behavior can be assembled from independent capabilities or when inheritance would create a fragile hierarchy. Inheritance is appropriate only for a stable “is-a” relationship with shared invariants and substitutable subclasses.")
console.log(example.explain())

Run this JavaScript example in a browser console or Node.js and change the input to explore the rule from this question.