inheritance
a child class can inherit all the methods and properties from another class.
a child class can inherit all the methods and properties from another class.
Rabbit, Fish and Bird classes all inherit properties from their parent class which is Animal
class Animal {
alive = true;
eat() {
console.log(`This ${this.name} is eating`);
}
sleep() {
console.log(`This ${this.name} is sleeping`);
}
}
class Rabbit extends Animal {
name = "rabbit";
run() {
console.log(`This ${this.name} is running`);
}
}
class Fish extends Animal {
name = "fish";
swim() {
console.log(`This ${this.name} is swimming`);
}
}
class Bird extends Animal {
name = "bird";
fly() {
console.log(`This ${this.name} is flying`);
}
}