OOP (Object-Oriented Programming) — Inheritance
Inheritance allows one class to use the properties and methods of another class. The class that gives its properties is called the parent (or base) class, and the class that receives them is called the child (or derived) class. Inheritance promotes reusability and avoids writing the same code multiple times. For example, if you have a parent class 'Animal' with methods like eat() and sleep(), you can create child classes like 'Dog' and 'Cat' that automatically have those methods. You can also add extra features to child classes without affecting the parent class.
class Animal {
eat() {
console.log('This animal is eating');
}
}
class Dog extends Animal {
bark() {
console.log('Dog is barking');
}
}
let myDog = new Dog();
myDog.eat();
myDog.bark();