class Animal {
    constructor(public name: string) {}

    speak(): void {
        console.log(`${this.name} makes a noise.`);
    }
}

class Dog extends Animal {
    speak(): void {
        console.log(`${this.name} barks.`);
    }
}

const dog = new Dog("Buddy");
dog.speak(); // Output: Buddy barks.
Bash

Explanation:

  • This program defines a base class Animal with a constructor and a method speak.
  • The Dog class extends Animal and overrides the speak method to provide specific behavior.
  • An instance of Dog is created, and calling the speak method outputs “Buddy barks.”
Resize text
Scroll to Top