Advertisement
SergioG_0823849

dog

Apr 4th, 2024 (edited)
679
0
305 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 1) Create a class Animal with properties name and a method makeSound()
  2. class Animal {
  3.   constructor(name) {
  4.     this.name = name;
  5.   }
  6.  
  7.   makeSound() {
  8.     console.log("The animal makes a sound");
  9.   }
  10. }
  11.  
  12. // 2) Create a class Dog that inherits from Animal and overrides the makeSound() method
  13. class Dog extends Animal {
  14.   constructor(name) {
  15.     super(name);
  16.   }
  17.  
  18.   makeSound() {
  19.     console.log(`${this.name} barks`);
  20.   }
  21. }
  22.  
  23. // 3) Instantiate a Dog object and call its makeSound() method
  24. const myDog = new Dog("Buddy");
  25. myDog.makeSound(); // Output: Buddy barks
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement