DEV Community

Cover image for Polymorphism in JavaScript
Manikandan K
Manikandan K

Posted on • Edited on

Polymorphism in JavaScript

Explanation:

  • Polymorphism: Polymorphism is a fundamental concept in object-oriented programming where objects of different types can be treated as instances of a common superclass or interface. In this example, polymorphism is demonstrated by the sound() method, which behaves differently for different types of animals (Animal, Lion, Dog) despite being called using the same interface.

Let Breakdown the term

  • The term "polymorphism" indeed breaks down to "poly," meaning "many," and "morph," meaning "form."
  • In the context of programming, polymorphism refers to the ability of objects to take on different forms or behave differently based on their specific types or classes.
// Definition of the Animal class
class Animal {
    // Constructor method for initializing an animal with a name
    constructor(_animalName) {
        this.name = _animalName;
    }

    // Method to make a sound, default implementation for all animals
    sound() {
        console.log(`${this.name} Definitely makes a sound`);
    }
}

// Definition of the Lion class, inheriting from Animal
class Lion extends Animal {
    // Constructor method for initializing a Lion with a name
    constructor(_name) {
        super(_name); // Calls the constructor of the parent class
    }

    // Method to make a sound, specific to Lions
    sound() {
        console.log(`${this.name} sounds like roaring...`);
    }
}

// Definition of the Dog class, inheriting from Animal
class Dog extends Animal {
    // Constructor method for initializing a Dog with a name
    constructor(_name) {
        super(_name); // Calls the constructor of the parent class
    }

    // Method to make a sound, specific to Dogs
    sound() {
        console.log(`${this.name} sounds like barking...`);
    }
}

// Creating an instance of Animal
const tiger = new Animal('Stark');
console.log(tiger); // Output: Animal { name: 'Stark' }
tiger.sound(); // Output: Stark Definitely makes a sound

// Creating an instance of Lion
const leo = new Lion('Leo');
console.log(leo); // Output: Lion { name: 'Leo' }
leo.sound(); // Output: Leo sounds like roaring...

// Creating an instance of Dog
const tommy = new Dog('Tommy');
console.log(tommy); // Output: Dog { name: 'Tommy' }
tommy.sound(); // Output: Tommy sounds like barking...

Enter fullscreen mode Exit fullscreen mode
  • Class Definitions: Three classes are defined: Animal, Lion, and Dog. Each class represents a type of animal, with Lion and Dog inheriting from Animal.

  • Constructor Methods:
    Each class has a constructor method that initializes the object with a name. The super() method is used in the constructor of the derived classes (Lion and Dog) to call the constructor of the base class (Animal) and set the name.

  • sound() Method:
    The sound() method is defined in the Animal class as the default behavior for making a sound. However, this method is overridden in the Lion and Dog classes to provide specific sound implementations for Lions and Dogs.

  • Instance Creation and Method Invocation:
    Instances of Animal, Lion, and Dog are created (tiger, leo, tommy), and the sound() method is called on each instance. Despite being called using the same method name, the actual behavior varies depending on the type of animal due to polymorphism.

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy šŸŽ–ļø

If you're going to use generative AI to do your assignments, you might want to remove text like this:

Certainly! Here's your provided code with line-by-line explanations and a brief definition of polymorphism

Collapse
 
jonrandy profile image
Jon Randy šŸŽ–ļø

Also AI generated content should be marked as such, according to the dev.to guidelines

Collapse
 
nameismani profile image
Manikandan K

Sure, Thanks