Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте 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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 5

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 3
some-alt