Skip to the solution
HardFrontend7 min read

Explain how prototypal inheritance differs from classical inheritance.

748 views

The problem

Question presented to candidate: "How does the way inheritance works in JavaScript actually differ from classical, class-based inheritance in a language like Java or C++?"

What a strong answer should cover:

  • Classical inheritance is a blueprint-and-instance model: a class is a fixed template, checked and fixed at compile time, and objects are stamped out from it.
  • Prototypal inheritance is an object-to-object delegation model: there are no blueprints -- objects link directly to other live objects, and lookup happens dynamically at runtime.
  • Delegation vs copying: a prototypal object does not receive a private copy of its prototype's methods -- it looks them up live, every time, by following a real link.
  • Dynamic vs static: a JavaScript prototype can be patched or extended after instances already exist, and every existing instance picks up the change immediately -- a compiled Java class cannot be altered at runtime.
  • Even JavaScript's class keyword does not introduce true classical inheritance underneath -- it is syntax sugar over the exact same prototype chain, and a class's methods remain a live, mutable object (Ctor.prototype) that can still be patched after the fact.

Clarifying questions expected:

  • "Should I focus on the conceptual difference, or also cover exactly how the JavaScript prototype mechanism itself works?" (the mechanism has its own dedicated question)

Code / implementation expected: Yes -- a runnable snippet proving a class's prototype is still live and patchable after declaration, unlike a compiled classical class.

  • prototypes
  • inheritance
  • oop
  • classes

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 7 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptA class prototype stays a live, patchable object -- even after existing instances are created (run directly)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 136 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

How would you use the new Iterator helper methods (.map, .filter, .take, .drop) to process a large or infinite iterator lazily?

Continue