Skip to content

Instantly share code, notes, and snippets.

@hellokvn
Created February 14, 2022 09:59
Show Gist options
  • Save hellokvn/0ede7977630ed64eff31d6e31f5f78e6 to your computer and use it in GitHub Desktop.
Save hellokvn/0ede7977630ed64eff31d6e31f5f78e6 to your computer and use it in GitHub Desktop.
Polymorphism - Bad
class Dog {
public name: string;
constructor(name: string) {
this.name = name;
}
public makeSound(): void {
process.stdout.write('wuff wuff\n');
}
}
class Cat {
public name: string;
constructor(name: string) {
this.name = name;
}
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