How does the inheritance work in JavaScript? Is there multiple inheritance support in JavaScript? JavaScript inheritance explained in simple terms.
Inheritance is one of the OOP features in the JavaScript. JavaScript utilizes the built-in prototype property to get the inheritance implemented. JavaScript allows us to associate a prototypical object with any constructor function.
Inheritance in JavaScript
Below section demonstrates how the inheritance works in JavaScript. Below example shows how the SubClass inherits a SuperClass.
In the above example, ” SubClass.prototype = new SuperClass;” makes the SubClass inherit all the properties and methods of SuperClass.
No Multiple inheritance in Javascript
Does javaScript support multiple inheritance? The answer is No!
Why does JavaScript not supporting Multiple inheritance?
Inheritance in JavaScript happens at run time by searching the prototype chain of an object. An object has a single associated prototype, JavaScript cannot dynamically inherit from more than one prototype chain!
NOTE: If multiple classes needs to be inherited, they should be arranged in the same hierarchy with parent-to-child relationship.
Though we can have multiple constructor function call within a constructor function, it is different from the multiple inheritance. Using ‘prototype’ property, we cannot add/modify any properties/methods of such inner consturctors.