DEV Community

Cover image for 11-JS/TS OOP: Polymorphism In OOP
Hasan Zohdy
Hasan Zohdy

Posted on

11-JS/TS OOP: Polymorphism In OOP

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().

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");
  }
}
Enter fullscreen mode Exit fullscreen mode

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().

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;
  }
}
Enter fullscreen mode Exit fullscreen mode

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, it will help me to keep going and keep creating more content.

😍 Join our community

Join our community on Discord 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

Packages & Libraries

React Js Packages

Courses (Articles)

Top comments (0)