architect-handbook

Software Architect Handbook

View on GitHub

Prototypal vs Classical Inheritance

Overview

In class-based languages the classes are defined beforehand and the objects are instantiated based on the classes. If two objects apple and orange are instantiated from the class Fruit, they are inherently fruits and it is guaranteed that you may handle them in the same way.

In prototype-based languages the objects are the primarty entities. No classes even exist. The prototype of an object is just another object to which the object is linked. Every object has one prototype link (and only one). New objects can be created based on already existing objects chosen as their prototype. You may call two different objects apple and orange a fruit, if the object fruit exists, both apple and orange may have fruit as their prototype. The idea of the fruit class doesn’t exist explicitly, but as the equivalence class of the object’s sharing the same prototype. The attributes and methods of the prototype are delegated to all the objects of the equivalence class defined by this prototype. The key difference is that the attributes and methods owned individually by the object may not be shared by others of the same equivalence class, thus it is not guaranteed that two objets linked to the same prototype may be handled the same way.

Only single inheritance can be implemented through the prototype.

Classical Inheritance Drawbacks

Resources:

  • https://www.youtube.com/watch?v=lKCCZTUx0sI&t=1s