Polymorphism in TypeScript (OOP)

In today’s article, I want to talk about Polymorphism in TypeScript, which is one of four pillars of object-orientated programming.

Photo by Arnold Francisca on Unsplash

What is Polymorphism?

Polymorphism is the ability to create a class that has more than one form. Or in other words, classes have the same methods but different implementations.

Example

So, before explaining how polymorphism works, I better show you, how it does not work by an example.

Imagine we have two animal classes, one class is called Dog, the other class is called Cat. Both classes should have the same properties and methods.

https://medium.com/media/c81ffd90a60cabf68268cb2283d2cfda/href

And that works fine, but actually, that is not the way to go. As I said, both classes have the exact same properties and methods, so it makes sense to inherit this from a parent class.

Let’s have a look at a better way:

https://medium.com/media/f76a4524f7966c62799820e9a0c07d8d/href

The results of the makeSound methods are exactly the same, but the approach is a little bit different.

GitHub – hellokvn/medium-polymorphism-in-typescript

Thanks for reading my article about Polypmorphism in TypeScript. I hope, I could refresh your knowledge with this quick example.

Cheers

Use any component in all your projects

Up until now, you used to build features hidden inside larger projects.

But what if you were to develop independent features first, and then easily compose and manage them in many applications? Your development will become faster, more consistent, and more scalable every day. Create a component once, and truly use it anywhere to build anything.

OSS Tools like Bit offer a powerful developer experience for building independent components and composing them to applications. You can start small with a nice project, some shared components, or even trying out Micro Frontends. Give it a try →

An independent product component: watch the auto-generated dependency graph

Learn more


Polymorphism in TypeScript (OOP) was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Kevin Vogel

In today's article, I want to talk about Polymorphism in TypeScript, which is one of four pillars of object-orientated programming.

Photo by Arnold Francisca on Unsplash

What is Polymorphism?

Polymorphism is the ability to create a class that has more than one form. Or in other words, classes have the same methods but different implementations.

Example

So, before explaining how polymorphism works, I better show you, how it does not work by an example.

Imagine we have two animal classes, one class is called Dog, the other class is called Cat. Both classes should have the same properties and methods.

And that works fine, but actually, that is not the way to go. As I said, both classes have the exact same properties and methods, so it makes sense to inherit this from a parent class.

Let’s have a look at a better way:

The results of the makeSound methods are exactly the same, but the approach is a little bit different.

GitHub - hellokvn/medium-polymorphism-in-typescript

Thanks for reading my article about Polypmorphism in TypeScript. I hope, I could refresh your knowledge with this quick example.

Cheers

Use any component in all your projects

Up until now, you used to build features hidden inside larger projects.

But what if you were to develop independent features first, and then easily compose and manage them in many applications? Your development will become faster, more consistent, and more scalable every day. Create a component once, and truly use it anywhere to build anything.

OSS Tools like Bit offer a powerful developer experience for building independent components and composing them to applications. You can start small with a nice project, some shared components, or even trying out Micro Frontends. Give it a try →

An independent product component: watch the auto-generated dependency graph

Learn more


Polymorphism in TypeScript (OOP) was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Kevin Vogel


Print Share Comment Cite Upload Translate Updates
APA

Kevin Vogel | Sciencx (2022-02-18T08:23:56+00:00) Polymorphism in TypeScript (OOP). Retrieved from https://www.scien.cx/2022/02/18/polymorphism-in-typescript-oop/

MLA
" » Polymorphism in TypeScript (OOP)." Kevin Vogel | Sciencx - Friday February 18, 2022, https://www.scien.cx/2022/02/18/polymorphism-in-typescript-oop/
HARVARD
Kevin Vogel | Sciencx Friday February 18, 2022 » Polymorphism in TypeScript (OOP)., viewed ,<https://www.scien.cx/2022/02/18/polymorphism-in-typescript-oop/>
VANCOUVER
Kevin Vogel | Sciencx - » Polymorphism in TypeScript (OOP). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/18/polymorphism-in-typescript-oop/
CHICAGO
" » Polymorphism in TypeScript (OOP)." Kevin Vogel | Sciencx - Accessed . https://www.scien.cx/2022/02/18/polymorphism-in-typescript-oop/
IEEE
" » Polymorphism in TypeScript (OOP)." Kevin Vogel | Sciencx [Online]. Available: https://www.scien.cx/2022/02/18/polymorphism-in-typescript-oop/. [Accessed: ]
rf:citation
» Polymorphism in TypeScript (OOP) | Kevin Vogel | Sciencx | https://www.scien.cx/2022/02/18/polymorphism-in-typescript-oop/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.