Both classical and prototypal inheritance are object-oriented programming paradigms. Objects in object-oriented programming are abstractions that encapsulate the properties of an entity. This is known as abstraction.
When dealing with multiple levels of abstraction, each level is more general or more specific. The more general abstraction of a more specific abstraction is called a generalization.
As mentioned previously, objects are abstraction of entities. We use either classes (classical inheritance) or prototypes (prototypal inheritance) to create generalizations of these objects. Generalizations are created by inheritance.
Consider an example:
max
and
claire
respectively.
Dog
, which encapsulates their common
characteristics. We can use inheritance to pass characteristics from
Dog
to max
.
Cat
. Similarly, claire
will inherit
characteristics from Cat
.
Animal
, to encapsulate those
characteristics. Dog
and Cat
inherit these
common characteristics from Animal
.
In classical object-oriented programming, there are two types of abstractions: objects and classes. An object is an abstractions of an entity, while a class is either an abstraction of an object or another class.
If we were to model the previous example using classical inheritance, it would look something like this:
class Animal { }
class Dog extends Animal { }
class Cat extends Animal { }
const max = new Dog();
.name = 'Max';
max
const claire = new Cat();
.name = 'Claire'; claire
In prototypal object-oriented programming, there’s only one type of abstraction: objects. Objects are either abstractions of entities or other objects, in which case they’re called prototypes. Hence a prototype is a generalization.
Objects can be created out of nothing or from another object, which in turn becomes the prototype of the newly created object.
If we were to model the previous example using prototypal inheritance, it would look something like this:
const animal = {};
const dog = Object.create(animal);
const cat = Object.create(animal);
const max = Object.create(dog);
.name = 'Max';
max
const claire = Object.create(cat);
.name = 'Claire'; claire