Facebook Pixel

Prototypal vs Classical Inheritance

How JavaScript's prototypal inheritance differs from Java/C++ classical inheritance.

Prototypal vs Classical Inheritance

Classical Inheritance (Java, C++)

  • Classes: blueprints for objects. Objects are instances of classes.
  • Hierarchy: classes inherit from other classes.
  • Strict: defined at compile time. Cannot change at runtime.
  • Multiple inheritance: some languages support it (C++), some do not (Java).

Prototypal Inheritance (JavaScript)

  • Objects: objects inherit directly from other objects. No classes (ES6 classes are sugar).
  • Chain: objects link to other objects via the prototype chain.
  • Dynamic: prototypes can be changed at runtime.
  • Single inheritance: each object has one prototype (but can compose).

Key Differences

FeatureClassicalPrototypal
UnitClassObject
InheritanceClass to classObject to object
FlexibilityStaticDynamic
InstanceCreated from classCreated from object
ES6 classesN/ASyntactic sugar

Composition Over Inheritance

In JavaScript, composition (mixing objects) is often preferred over deep inheritance chains:

const canFly = { fly: () => "flying" }; const canSwim = { swim: () => "swimming" }; Object.assign(bird, canFly); Object.assign(fish, canSwim);

The Takeaway

Classical: classes inherit from classes (static, strict). Prototypal: objects inherit from objects (dynamic, flexible). JS uses prototypal. ES6 classes are syntactic sugar. Prefer composition over deep inheritance chains.

Classical: classes inherit from classes (static, strict, compile-time). Prototypal: objects inherit from objects (dynamic, flexible, runtime). JS uses prototypal. ES6 classes are syntactic sugar over prototypes.

Yes. Use Object.setPrototypeOf(obj, newProto). This is possible because JS uses prototypal inheritance (dynamic). In classical inheritance, the hierarchy is fixed at compile time.

Composition is often preferred. Instead of deep inheritance chains, mix objects: Object.assign(bird, canFly, canSwim). This is more flexible and avoids the problems of deep inheritance (fragile base class, tight coupling).

Prototypal. ES6 classes are syntactic sugar over the prototype system. Under the hood, it is the same: methods go on the prototype, extends sets up the prototype chain. The class syntax looks classical but the mechanism is prototypal.

Not directly (each object has one prototype). But you can simulate it with composition: Object.assign(obj, mixin1, mixin2). This copies properties from multiple sources. Mixins are the JS way to achieve multiple inheritance-like behavior.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.