start learning
Image 1
440000979747

Polymorphism JavaScript Object-Oriented Programming (OOP)


Polymorphism allows objects of different types to be treated as instances of a common superclass or interface, enabling code flexibility, extensibility, and modularity.


Method Overriding


class Animal {
  speak() {
    console.log('The animal makes a sound.');
  }
}

class Cat extends Animal {
  speak() {
    console.log('Meow!');
  }
}

const animal = new Animal();
const cat = new Cat();

animal.speak(); // Output: The animal makes a sound.
cat.speak(); // Output: Meow!

Explanation: In this example, the Animal class has a speak method that prints a generic sound. The Cat class extends Animal and overrides the speak method with its own implementation. When speak is called on an instance of Animal, it uses the method from the Animal class, while calling it on an instance of Cat uses the method overridden in the Cat class. This demonstrates polymorphism through method overriding.


Polymorphic Function Parameter


class Shape {
  getArea() {
    return 0;
  }
}

class Rectangle extends Shape {
  constructor(width, height) {
    super();
    this.width = width;
    this.height = height;
  }

  getArea() {
    return this.width * this.height;
  }
}

class Circle extends Shape {
  constructor(radius) {
    super();
    this.radius = radius;
  }

  getArea() {
    return Math.PI * Math.pow(this.radius, 2);
  }
}

function printArea(shape) {
  console.log(`Area: ${shape.getArea()}`);
}

const rectangle = new Rectangle(4, 5);
const circle = new Circle(3);

printArea(rectangle); // Output: Area: 20
printArea(circle); // Output: Area: 28.274333882308138

In this example, the printArea function takes a parameter shape, which can be an instance of any class that extends the Shape class. The getArea method is polymorphic, as its implementation differs depending on the specific class (e.g., Rectangle or Circle). When printArea is called with an instance of Rectangle or Circle, it invokes the appropriate getArea method based on the object's type.


Interface-based Polymorphism


class Animal {
  constructor(name) {
    this.name = name;
  }

  makeSound() {
    // Abstract method
  }
}

class Dog extends Animal {
  makeSound() {
    console.log('Woof!');
  }
}

class Cat extends Animal {
  makeSound() {
    console.log('Meow!');
  }
}

function playSound(animal) {
  animal.makeSound();
}

const dog = new Dog('Buddy');
const cat = new Cat('Whiskers');

playSound(dog); // Output: Woof!
playSound(cat); // Output: Meow!

Explanation: In this example, the Animal class has an abstract method makeSound that is overridden by its subclasses Dog and Cat. The playSound function accepts an animal parameter, which can be an instance of any class that extends Animal. The function invokes the makeSound method on the provided animal, without knowing the specific subclass. This demonstrates polymorphism through interface-based programming, where different objects conform to a common interface (in this case, the makeSound method).


Feel free to copy the code and experiment with our javascript compiler


Output: