88.classInheritance

Write 3 classes named Animal, Dog and GoldenRetriever that inherit from each other.

Animal should

  • receive a name in it's constructor and store it in a private property
  • have a name getter that will return the name of the animal
  • have a sleep method that will return {name} is sleeping...
  • have a eat method that will return {name} is eating. Nom nom!

Dog should inherit from Animal and

  • have an additional prop for it's constructor named breed which will be stored in a private property
  • have a breed getter that will return the breed of the dog
  • have a bark method that will return {name}: Woof! Woof!

GoldenRetriever should inherit from Dog and

  • always have the breed "Golden Retriever"
  • have a guardHouse method that returns: {name} is a Golden Retriever thus too friendly to guard the house.

Example 1

const myCat = new Animal("Susie");

console.log(myCat.name); // Susie
console.log(myCat.sleep()); // "Susie is sleeping..."
console.log(myCat.eat()); // "Susie is eating. Nom nom!"

const dog = new Dog("Pixel", "German Shepherd");
console.log(dog.name); // Pixel
console.log(dog.sleep()); // "Pixel is sleeping..."
console.log(dog.eat()); // "Pixel is eating. Nom nom!"
console.log(dog.bark()); // "Pixel: Woof! Woof!"

const goldenBoy = new GoldenRetriever("Goldie");
console.log(goldenBoy.name); // Goldie
console.log(goldenBoy.sleep()); // "Goldie is sleeping..."
console.log(goldenBoy.eat()); // "Goldie is eating. Nom nom!"
console.log(goldenBoy.bark()); // "Goldie: Woof! Woof!"
console.log(goldenBoy.guardHouse()); // "Goldie is a Golden Retriever thus too friendly to guard the house."