Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1) Create a class Animal with properties name and a method makeSound()
- class Animal {
- constructor(name) {
- this.name = name;
- }
- makeSound() {
- console.log("The animal makes a sound");
- }
- }
- // 2) Create a class Dog that inherits from Animal and overrides the makeSound() method
- class Dog extends Animal {
- constructor(name) {
- super(name);
- }
- makeSound() {
- console.log(`${this.name} barks`);
- }
- }
- // 3) Instantiate a Dog object and call its makeSound() method
- const myDog = new Dog("Buddy");
- myDog.makeSound(); // Output: Buddy barks
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement