1 min read

Class Inheritance

Inheritance is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
Class Inheritance

To create a class inheritance, use the extends keyword.A class created with a class inheritance inherits all the methods from another class.

The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.

syntax:

class Child extends Parent{ }

Example

class Car {
 constructor(brand) {
this.carname = brand;
}
 present() {
return'I have a ' + this.carname;
}
}

class Model extends Car {
constructor(brand, mod) {
super(brand);
this.model = mod;
}
 show() {
return this.present() + ', it is a ' + this.model;
}
}

let myCar = new Model("Ford", "Mustang");
document.getElementById("demo").innerHTML= myCar.show();

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.

https://static.javatpoint.com/images/core/inheritance.jpg
JavaScript Class Inheritance
https://www.w3schools.com/js/js_class_inheritance.asp

#JavaScript #Inharitance #OOPS #extends #Probyto #Probyto AI

Subscribe and follow us for latest news in Data Science and Machine learning and stay updated!

Facebook: https://facebook.com/probyto
Twitter: https://twitter.com/probyto
LinkedIn: https://linkedin.com/company/probyto
Instagram: https://instagram.com/probyto