These questions have always bothered me. What is the difference between prototypal and classical inheritance? Is prototypal inheritance better than classical? If yes, then why?
Let’s understand those differences and reasons which makes prototypal inheritance better than classical.
First things first.
What is Prototypal and Classical inheritance?
To learn more about Prototypal inheritance please read my previous post on here.
An example for each of them will make it more clear.
A simple example of classical inheritance in Java:
Moving ahead. Let’s dive into the real differences and reasons that make prototypal inheritance so much cooler than its counterpart.
Compile time vs Runtime inheritance:
In class-based languages classes are created at compile time. So, it’s during compile time that the members of class are accumulated and we know that
user1.getName() will call
getName method on its parent class.
user1 object. If not, it follows its prototype link which points to its parent and checks there if the
getName method is present and return its value.
On the other hand, in classical inheritance, once a class is created you cannot modify it during runtime.
Talking about classical inheritance. Although the above-mentioned example was pretty simple, there are much more complications involved when you start dealing with large-scale applications. While dealing with complex code, terms like Interfaces, Abstract classes, Virtual Classes start coming into the picture. And from my experience, it just adds to the complexity - of writing the code and understanding it as well.
Let’s see a simple example of how to mimic multiple inheritance in ES6:
Above code is mostly self-explanatory. We are creating separate classes
Employee. Then we create an empty class
User and we decide that
User should have two methods-
sleep. We could have easily done it by inheriting the
User class from the
Person class. But, we don’t want to inherit the
sleep method of
Therefore, we just extend our independent
User class by handpicking methods from the classes we want. For example, we took
walk method from
Person class and
sleep method from
Employee class (because employees know how to sleep better, eh!).
And that’s how we mimicked multiple inheritance.
Although, I did not come across any solid drawbacks of prototypal inheritance over classical ones in terms of performance or memory consumption- I know from my experience, that coming from the classical background it is a bit confusing to get started with prototypal inheritance for beginners.
I have also seen people mimicking classical inheritance in a prototypal style. Which defeats the whole purpose of using prototype-based languages.
I would just like to conclude by saying that prototypal inheritance is much powerful than you think. Try and make use of its dynamic nature to your benefit.
Also, do drop a note if you find any conceptual mistake or if you think the post can be improved in any way. Because, at the end of the day, I am a learner myself.