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.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 5
Polymorphism in TypeScript
Stryg for at vise menuen
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.
Tak for dine kommentarer!