Inheritance in Classes
Explore how to implement inheritance in JavaScript classes by using the extends keyword to link child and parent classes. Understand how the super() function calls the parent constructor to initialize inherited properties, enabling cleaner and more efficient object-oriented programming.
We'll cover the following...
In this lesson we will implement inheritance in classes.
Inheritance in classes
The underlying concept when utilizing inheritance is the same in both approaches: constructor functions and classes. However, the latter provides a cleaner syntax as well as new keywords. First, take a look at the expected hierarchy in a class-based approach for inheritance in the diagram below.
Use the extends keyword to have all properties available from one class to another.
class childClassName extends parentClassName{
//body containing constructor and methods
}
First, write the child class followed by the extends keyword, then the name of the parent class, which is upon which the child class is based and will ...