Explain the concept of inheritance in ES2015 classes
TL;DR
JavaScript class inheritance uses extends to link constructor and prototype chains. A derived constructor must call super() before accessing this, and super.method() invokes parent behavior. Use inheritance when the child is genuinely substitutable for the parent and the contract is stable. Prefer composition for optional capabilities or when subclasses would need to override many unrelated methods.
The two chains created by extends
ES2015 class inheritance links both instance methods and static members through separate prototype relationships.
Instance method lookup uses the lower prototype chain, while inherited static methods use the constructor chain.
Inheritance in ES2015 classes
Basic concept
Inheritance in ES2015 classes allows a class (child class) to inherit properties and methods from another class (parent class). This promotes code reuse and a hierarchical class structure.
Using the extends keyword
The extends keyword is used to create a class that is a child of another class. The child class inherits all the properties and methods of the parent class.
Using the super keyword
The super keyword is used to call the constructor of the parent class and to access its methods. This is necessary when you want to initialize the parent class properties in the child class.
Method overriding
Child classes can override methods from the parent class. This allows the child class to provide a specific implementation of a method that is already defined in the parent class.