JavaScript Polymorphism

In this chapter, we will learn about JavaScript polymorphism. Polymorphism is one of the core concepts of Object-Oriented Programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. We will cover:

  • What is Polymorphism?
  • Why Use Polymorphism?
  • Method Overriding
  • Polymorphism in Action
  • Simple Programs using Polymorphism

What is Polymorphism?

Polymorphism, derived from the Greek words "poly" (many) and "morph" (form), is the ability of different objects to respond to the same method call in their own unique way. In JavaScript, polymorphism is achieved through method overriding and the use of a common interface.

Why Use Polymorphism?

Polymorphism provides several benefits:

  • Code Reusability: Allows different classes to share a common interface, promoting code reuse.
  • Flexibility: Enables you to write more flexible and maintainable code by decoupling code dependencies.
  • Extensibility: Makes it easier to extend and modify existing code without affecting other parts of the program.

Method Overriding

Method overriding occurs when a child class provides a specific implementation of a method that is already defined in its parent class. This allows the child class to provide its own behavior while retaining the method name and signature.

Example

class Animal { makeSound() { return "Some generic sound"; } } class Dog extends Animal { makeSound() { return "Bark"; } } class Cat extends Animal { makeSound() { return "Meow"; } } let animals = [new Dog(), new Cat()]; for (let animal of animals) { console.log(animal.makeSound()); } 

Output:

Bark Meow 

Polymorphism in Action

Polymorphism allows you to treat objects of different classes in a uniform manner. You can write code that works with objects of different classes through a common interface.

Example

class Shape { getArea() { return 0; } } class Circle extends Shape { constructor(radius) { super(); this.radius = radius; } getArea() { return Math.PI * Math.pow(this.radius, 2); } } class Rectangle extends Shape { constructor(width, height) { super(); this.width = width; this.height = height; } getArea() { return this.width * this.height; } } let shapes = [new Circle(5), new Rectangle(4, 6)]; for (let shape of shapes) { console.log(shape.getArea()); } 

Output:

78.53981633974483 24 

Simple Programs using Polymorphism

Program 1: Employee Management with Polymorphism

class Employee { constructor(name) { this.name = name; } getDetails() { return `Employee: ${this.name}`; } } class Developer extends Employee { constructor(name, programmingLanguage) { super(name); this.programmingLanguage = programmingLanguage; } getDetails() { return `Developer: ${this.name}, Programming Language: ${this.programmingLanguage}`; } } class Manager extends Employee { constructor(name, department) { super(name); this.department = department; } getDetails() { return `Manager: ${this.name}, Department: ${this.department}`; } } let employees = [ new Developer("Ramesh", "JavaScript"), new Manager("Neha", "HR") ]; for (let employee of employees) { console.log(employee.getDetails()); } 

Output:

Developer: Ramesh, Programming Language: JavaScript Manager: Neha, Department: HR 

Program 2: Payment System with Polymorphism

class Payment { processPayment() { return "Processing generic payment"; } } class CreditCardPayment extends Payment { processPayment() { return "Processing credit card payment"; } } class PayPalPayment extends Payment { processPayment() { return "Processing PayPal payment"; } } let payments = [new CreditCardPayment(), new PayPalPayment()]; for (let payment of payments) { console.log(payment.processPayment()); } 

Output:

Processing credit card payment Processing PayPal payment 

Program 3: Transportation System with Polymorphism

class Vehicle { move() { return "Moving"; } } class Car extends Vehicle { move() { return "Driving a car"; } } class Bike extends Vehicle { move() { return "Riding a bike"; } } let vehicles = [new Car(), new Bike()]; for (let vehicle of vehicles) { console.log(vehicle.move()); } 

Output:

Driving a car Riding a bike 

Program 4: Appliance Control with Polymorphism

class Appliance { turnOn() { return "Turning on appliance"; } } class WashingMachine extends Appliance { turnOn() { return "Turning on washing machine"; } } class Refrigerator extends Appliance { turnOn() { return "Turning on refrigerator"; } } let appliances = [new WashingMachine(), new Refrigerator()]; for (let appliance of appliances) { console.log(appliance.turnOn()); } 

Output:

Turning on washing machine Turning on refrigerator 

Program 5: Document Handling with Polymorphism

class Document { print() { return "Printing document"; } } class PDFDocument extends Document { print() { return "Printing PDF document"; } } class WordDocument extends Document { print() { return "Printing Word document"; } } let documents = [new PDFDocument(), new WordDocument()]; for (let document of documents) { console.log(document.print()); } 

Output:

Printing PDF document Printing Word document 

Conclusion

In this chapter, you learned about JavaScript polymorphism, including its definition, benefits, method overriding, and polymorphism in action. We also provided various use cases with simple programs to demonstrate the usage of polymorphism. Polymorphism is a powerful feature in OOP that promotes code reuse and flexibility.

Leave a Comment

Scroll to Top