Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Polymorphism in TypeScript | Inheritance and Class Hierarchies
TypeScript Classes and OOP

bookPolymorphism 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.

12345678910111213141516171819202122232425
abstract 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!
copy

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.

question mark

What is polymorphism in the context of object-oriented programming?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5

bookPolymorphism in TypeScript

Swipe to show menu

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.

12345678910111213141516171819202122232425
abstract 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!
copy

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.

question mark

What is polymorphism in the context of object-oriented programming?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3
some-alt