Polymorphism in JavaScript
Tags: webdev, beginners, javascript, tutorial
Published: 2024-03-22 11:42:17

## 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.

```javascript
// 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...

```

- **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.