Polymorphism in TypeScript
Polymorphism is a foundational concept in object-oriented programming that allows you to write code that works with objects of different classes through a shared interface or base class. This means you can treat instances of different subclasses as if they are instances of their common parent, enabling your code to be both flexible and extensible. By relying on polymorphism, you can design systems where new types can be introduced with minimal changes to the existing codebase, as long as they adhere to the expected interface or contract.
12345678910111213141516171819202122232425abstract class Animal { abstract speak(): void; } class Dog extends Animal { speak(): void { console.log("Woof!"); } } class Cat extends Animal { speak(): void { console.log("Meow!"); } } function makeAnimalSpeak(animal: Animal): void { animal.speak(); } const myDog: Animal = new Dog(); const myCat: Animal = new Cat(); makeAnimalSpeak(myDog); // Output: Woof! makeAnimalSpeak(myCat); // Output: Meow!
This example illustrates how polymorphism works: the makeAnimalSpeak function takes an Animal reference, but you can pass it any object that is derived from Animal. Each subclass provides its own implementation of the speak method, so the correct behavior is invoked at runtime depending on the actual type of the object. This approach leads to code that is both flexible and maintainable, as you can add new animal types without changing the code that operates on the Animal base class.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Can you explain more real-world scenarios where polymorphism is useful?
How does polymorphism differ from inheritance?
Can you show how to add another animal type using this pattern?
Awesome!
Completion rate improved to 5
Polymorphism in TypeScript
Sveip for å vise menyen
Polymorphism is a foundational concept in object-oriented programming that allows you to write code that works with objects of different classes through a shared interface or base class. This means you can treat instances of different subclasses as if they are instances of their common parent, enabling your code to be both flexible and extensible. By relying on polymorphism, you can design systems where new types can be introduced with minimal changes to the existing codebase, as long as they adhere to the expected interface or contract.
12345678910111213141516171819202122232425abstract class Animal { abstract speak(): void; } class Dog extends Animal { speak(): void { console.log("Woof!"); } } class Cat extends Animal { speak(): void { console.log("Meow!"); } } function makeAnimalSpeak(animal: Animal): void { animal.speak(); } const myDog: Animal = new Dog(); const myCat: Animal = new Cat(); makeAnimalSpeak(myDog); // Output: Woof! makeAnimalSpeak(myCat); // Output: Meow!
This example illustrates how polymorphism works: the makeAnimalSpeak function takes an Animal reference, but you can pass it any object that is derived from Animal. Each subclass provides its own implementation of the speak method, so the correct behavior is invoked at runtime depending on the actual type of the object. This approach leads to code that is both flexible and maintainable, as you can add new animal types without changing the code that operates on the Animal base class.
Takk for tilbakemeldingene dine!