Created
August 9, 2023 15:13
-
-
Save suhailgupta03/951da9f5fb16888d7384a6c5bb464356 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal { | |
walk() { | |
console.log("This animal walks") | |
} | |
legs() { | |
console.log("This animal has 4 legs") | |
} | |
} // All the common functionalities | |
// are represented in the class named Animal | |
class Herbivore extends Animal { | |
// Herbivore is a type of Animal | |
// Using extends keyword, we can inherit | |
// all the functionalities of Animal class | |
eat() { | |
console.log("I eat only plants") | |
} | |
} | |
const rabbit = new Herbivore(); | |
rabbit.eat(); | |
rabbit.walk(); // walk() is inherited from Animal class | |
rabbit.legs(); // legs() is inherited from Animal class | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
class Animal {
constructor() {
this.eyes = 2
}
} // All the common functionalities
// are represented in the class named Animal
class Herbivore extends Animal {
// Herbivore is a type of Animal
// Using extends keyword, we can inherit
// all the functionalities of Animal class
eat() {
console.log("I eat only plants")
}
}
const rabbit = new Herbivore();
rabbit.eat();
rabbit.walk(); // walk() is inherited from Animal class
rabbit.legs(); // legs() is inherited from Animal class
console.log(rabbit.eyes) // eyes is inherited from Animal class