JS Tutorials
JS Objects
JS Functions
JS Classes
JS Async
To create a class inheritance, use the extends
keyword.
A class created with a class inheritance inherits all the methods from another class:
Create a class named "Model" which will inherit the methods from the "Car" class:
The super()
method refers to the parent
class.
By calling the super()
method in the
constructor method, we call the parent's constructor method and gets access to
the parent's properties and methods.
Inheritance is useful for code reusability: reuse properties and methods of an existing class when you create a new class.
Classes also allows you to use getters and setters.
It can be smart to use getters and setters for your properties, especially if you want to do something special with the value before returning them, or before you set them.
To add getters and setters in the class, use the
get
and set
keywords.
Create a getter and a setter for the "carname" property: