11-JS/TS OOP: Polymorphism In OOP
Tags: javascript, oop, typescript
Published: 2022-11-28 09:54:02

---
title: 11-JS/TS OOP: Polymorphism In OOP
published: true
description: 
series: jstsoop
tags: javascript, oop, typescript
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/79kdzq0hxnp7k46fc50e.png
# Use a ratio of 100:42 for best results.
# published_at: 2022-10-25 09:51 +0000
---

## Polymorphism

Polymorphism is a concept that allows us to use the same method name for different classes, and each class will have its own implementation of that method.

This principle is refers to `interfaces` that multiple classes can implement, and each class will have its own implementation of the method regardless how each class is implemented.

> The keyword `Polymorphism` is a greek word that means `many forms`, and this is what we are doing here, we are using the same method name for different classes, and each class will have its own implementation of that method.

## Real world example

Let's take a real world example for `Polymorphism`, let's say we have a `Worker` interface, that interface can be implemented literally by any job (as class), for example a `Teacher` can implement that interface, and a `Doctor` can implement that interface as well, and each class will have its own implementation of the method `work()`.

```ts
interface Worker {
  work(): void;
}

class Teacher implements Worker {
  work(): void {
    console.log("I'm a teacher, i teach students");
  }
}

class Doctor implements Worker {
  work(): void {
    console.log("I'm a doctor, i treat patients");
  }
}
```

So every class has its own way to implement the method `work()`, and this is what we call `Polymorphism`.

Another example that can be said here is `Area` interface, that interface can be implemented by `Circle`, `Square` and `Rectangle` classes, and each class will have its own implementation of the method `area()`.

```ts
interface Area {
  area(): number;
}

class Circle implements Area {
  constructor(private radius: number) {}

  area(): number {
    return Math.PI * this.radius ** 2;
  }
}

class Square implements Area {
  constructor(private side: number) {}

  area(): number {
    return this.side ** 2;
  }
}

class Rectangle implements Area {
  constructor(private width: number, private height: number) {}

  area(): number {
    return this.width * this.height;
  }
}
```

So each class has its own way to implement the method `area()`, and this is what we call `Polymorphism`.

> This can lead us to one of `SOLID` principles, which is `Dependency Inversion Principle`, as we can receive any object that implements the `Area` interface, and we don't care about how that object is implemented, we just care about the method `area()`.

## Use cases for Polymorphism

We can use `Polymorphism` in many cases, for example we can use it in `Factory Design Pattern`, `Strategy Design Pattern`, `Command Design Pattern`.

Also, we can use it in `Dependency Injection` to inject different implementations of the same interface.

## 🎨 Conclusion

As i mentioned before, i'm not going to write a fancy academic articles regarding OOP, because the main purpose of this series is to understand the concepts in easy way, if you want academic definitions, then Google is your best friend.

## ☕♨️ Buy me a Coffee ♨️☕

If you enjoy my articles and see it useful to you, you may [buy me a coffee](https://www.paypal.com/paypalme/hassanzohdy), it will help me to keep going and keep creating more content.

## 😍 Join our community

Join our community on [Discord](https://discord.gg/uBh98KZGGF) to get help and support.

## 📚 Bonus Content 📚

You may have a look at these articles, it will definitely boost your knowledge and productivity.

General Topics

* [Event Driven Architecture: A Practical Guide in Javascript](https://dev.to/hassanzohdy/event-driven-architecture-the-best-paradigm-that-i-love-to-work-with-in-javascript-and-node-js-1gnk)
* [Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript](https://dev.to/hassanzohdy/best-practices-for-case-styles-camel-pascal-snake-and-kebab-case-in-node-and-javascript-55oi)
* [After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
](https://dev.to/hassanzohdy/after-6-years-of-practicing-mongodb-here-are-my-thoughts-on-mongodb-vs-mysql-574b)

Packages & Libraries

* [Collections: Your ultimate Javascript Arrays Manager](https://dev.to/hassanzohdy/collections-your-ultimate-javascript-array-handler-3o15)
* [Supportive Is: an elegant utility to check types of values in JavaScript](https://dev.to/hassanzohdy/supportive-is-an-elegant-utility-to-check-types-of-values-in-javascript-1b3e)
* [Localization: An agnostic i18n package to manage localization in your project](https://dev.to/hassanzohdy/mongez-localization-the-simplest-way-to-translate-your-website-regardless-your-favorite-framework-4gi3)

 React Js Packages

* [useFetcher: easiest way to fetch data in React Js](https://dev.to/hassanzohdy/usefetcher-the-easiest-way-to-fetch-data-in-react-45o9)

Courses (Articles)

* [ES6++: The Ultimate Guide to ES6 and Beyond](https://dev.to/hassanzohdy/es6-1-introduction-h3e)
* [Node Js 2023 Course: Node With Typescript, The Ultimate Guide](https://dev.to/hassanzohdy/nodejs-course-2023-introduction-to-nodejs-3n4e)
* [React Js: Let"s Create File Manager With React Js and Node Js](https://dev.to/hassanzohdy/lets-create-a-file-manager-from-scratch-with-react-and-typescript-chapter-i-a-good-way-to-expand-your-experience-5g4k)