ES6 Classes part 3
Inheritance in ES6
The extends keyword is used in class declarations or class expressions to create a child of a class. The child inherits the properties and methods of the parent.
For example:
In the code above, the Dog class is a child of the Animal class, inheriting its properties and methods.
If there is a constructor present in the subclass, it needs to first call super() before using this. Also, the super keyword is used to call parent's methods.
For example, we can modify the program above to the following:
In the code above, the parent's speak() method is called using the super keyword.
Comments
Post a Comment