Inheritance in JavaScript
Inheritance is a feature of OOP that allows you to create new classes based on existing classes. In JavaScript, you can use the extends keyword to create a subclass that inherits from a superclass.
Here's an example of how to create a subclass in JavaScript:
class Student extends Person {
constructor(name, age, grade) {
super(name, age);
this.grade = grade;
}
sayHello() {
console.log(`Hello, my name is ${this.name} and I'm a student in grade ${this.grade}.`);
}
}
const student = new Student('Jane', 17, 11);
student.sayHello(); // prints "Hello, my name is Jane and I'm a student in grade 11."
No comments:
Post a Comment