Skip to content

Instantly share code, notes, and snippets.

@shian15810
Last active October 2, 2022 16:22
Show Gist options
  • Save shian15810/fa8563348c2dc4e04e6e541352c563fd to your computer and use it in GitHub Desktop.
Save shian15810/fa8563348c2dc4e04e6e541352c563fd to your computer and use it in GitHub Desktop.
Inheritance Breaks Encapsulation: https://www.youtube.com/watch?v=EfxYiAN6YME
class Dog {
protected bark(): void {
// console.log("bark");
this.barkMany(1);
}
protected barkMany(n: number): void {
for (let i = 0; i < n; i++) {
// this.bark();
console.log("bark");
}
}
}
class VerboseDog extends Dog {
public barkMany(n: number): void {
console.log("As a dog, I say: ");
for (let i = 0; i < n; i++) {
this.bark();
}
}
}
new VerboseDog().barkMany(2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment