Open In App

Polymorphism in JavaScript

Improve
Improve
Like Article
Like
Save
Share
Report

What is Polymorphism?

Polymorphism is one of the core concepts of object-oriented programming languages where poly means many and morphism means transforming one form into another. Polymorphism means the same function with different signatures is called many times. In real life, for example, a boy at the same time may be a student, a class monitor, etc. So a boy can perform different operations at the same time. This is called polymorphism. 

Features of Polymorphism:

  • Programmers can use the same method name repeatedly.
  • Polymorphism has the effect of reducing the number of functionalities that can be paired together.

Inheritance Polymorphism in JavaScript: In this example, we will create three functions with the same name and different operations. This program shows JavaScript Inheritance polymorphism.

Example: This example perform javascript inheritance polymorphism.

Javascript




class firstClass {
    add() {
        console.log("First Method")
    }
}
class secondClass extends firstClass {
    add() {
        console.log(30 + 40);
    }
}
class thirdClass extends secondClass {
    add() {
        console.log("Last Method")
    }
}
let ob = new firstClass();
let ob2 = new secondClass();
let ob3 = new thirdClass();
ob.add();
ob2.add();
ob3.add();


Output:

First Method
70
Last Method

The Code above shows how to implement inheritance polymorphism in JavaScript. In this code, we have a class and in this class, we have the “add” method, and we inherit this class in the Second Class. We create different classes with the same method name and different definitions of methods. This example shows us the same method will perform different operations depending on the object upon which it is called.

Polymorphism with Functions and Objects: It is also possible in JavaScript that we can make functions and objects with polymorphism. In the next example, we will make two functions with the same name ‘area’.  We define the area function in class A. In this function, we have two parameters – x and y. Class B is created by extending class A. The area function in class B invoked the area method in class A through super keyword – passing parameters a and b.  To make the area method behave differently in class B, we are going to console log the name of the class inside the method.  This way, it will become clear that the area method will behave differently depending on the object upon which it is called. 

Example: In this example, we will implement Polymorphism with functions and objects.

Javascript




class A {
    area(x, y) {
        console.log(x * y);
    }
}
class B extends A {
    area(a, b) {
        super.area(a, b);
        console.log('Class B')
    }
}
let ob = new B();
let output = ob.area(100, 200);


Output:

20000
Class B


Last Updated : 19 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads