Skip to content

Instantly share code, notes, and snippets.

@hellokvn
Created February 14, 2022 09:59
Show Gist options
  • Save hellokvn/2f2306075b76d729128a20d417f07b7b to your computer and use it in GitHub Desktop.
Save hellokvn/2f2306075b76d729128a20d417f07b7b to your computer and use it in GitHub Desktop.
Polymorphism - Good
class Animal {
public name: string;
constructor(name: string) {
this.name = name;
}
public makeSound(): void {
process.stdout.write('generic animal sound\n');
}
}
export class Dog extends Animal {
public makeSound(): void {
process.stdout.write('wuff wuff\n');
}
}
class Cat extends Animal {
public makeSound(): void {
process.stdout.write('meow meow\n');
}
}
const pocky: Cat = new Cat('Pocky');
pocky.makeSound(); // -> meow meow
const toshii: Dog = new Dog('Pocky');
toshii.makeSound(); // -> wuff wuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment