Quiz

Explain the difference between classical inheritance and prototypal inheritance

Topics
JavaScriptOOP

TL;DR

Classical inheritance is a model where classes inherit from other classes, typically seen in languages like Java and C++. Prototypal inheritance, used in JavaScript, involves objects inheriting directly from other objects. In classical inheritance, you define a class and create instances from it. In prototypal inheritance, you create an object and use it as a prototype for other objects.


Two inheritance models

Classical inheritance is usually described through classes and instances, while prototypal inheritance delegates directly between objects.

Classical and prototypal inheritance models

JavaScript class syntax presents a class-oriented API, but method lookup still uses the prototype-oriented mechanism on the right.

Difference between classical inheritance and prototypal inheritance

Classical inheritance

Classical inheritance is a pattern used in many object-oriented programming languages like Java, C++, and Python. It involves creating a class hierarchy where classes inherit properties and methods from other classes.

  • Class definition: You define a class with properties and methods.
  • Instantiation: You create instances (objects) of the class.
  • Inheritance: A class can inherit from another class, forming a parent-child relationship.

Example in Java:

class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark(); // Own method
}
}

Prototypal inheritance

Prototypal inheritance is a feature of JavaScript where objects inherit directly from other objects. There are no classes; instead, objects serve as prototypes for other objects.

  • Object creation: You create an object directly.
  • Prototype chain: Objects can inherit properties and methods from other objects through the prototype chain.
  • Flexibility: You can dynamically add or modify properties and methods.

Example in JavaScript:

const animal = {
eat() {
console.log('This animal eats food.');
},
};
const dog = Object.create(animal);
dog.bark = function () {
console.log('The dog barks.');
};
dog.eat(); // Inherited method (Output: This animal eats food.)
dog.bark(); // Own method (Output: The dog barks.)

Key differences

  • Class-based vs. prototype-based: Classical inheritance uses classes, while prototypal inheritance uses objects.
  • Inheritance model: Classical inheritance forms a class hierarchy, whereas prototypal inheritance forms a prototype chain.
  • Flexibility: Prototypal inheritance is more flexible and dynamic, allowing for changes at runtime.

Further reading

Exercises

Check your understanding
Beta
Check your understanding Exercise
Check your understanding Exercise

Which statements correctly compare classical and JavaScript prototypal inheritance? Select all that apply.