Skip to content

Instantly share code, notes, and snippets.

@suhailgupta03
Created August 9, 2023 15:13
Show Gist options
  • Save suhailgupta03/951da9f5fb16888d7384a6c5bb464356 to your computer and use it in GitHub Desktop.
Save suhailgupta03/951da9f5fb16888d7384a6c5bb464356 to your computer and use it in GitHub Desktop.
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
@suhailgupta03
Copy link
Author

class Animal {
constructor() {
this.eyes = 2
}

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
console.log(rabbit.eyes) // eyes is inherited from Animal class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment