Q1. What is polymorphism?
Polymorphism is an Object-Oriented Programming concept where the same method or function can behave differently based on the object or input.
The word Polymorphism means “many forms.” It allows developers to use a single interface for different data types or objects. In JavaScript, polymorphism is mainly achieved through method overriding and method overloading (simulated).
The word Polymorphism means “many forms.” It allows developers to use a single interface for different data types or objects. In JavaScript, polymorphism is mainly achieved through method overriding and method overloading (simulated).
Q2. Why is Polymorphism important in JavaScript?
Polymorphism improves code flexibility, readability, and reusability.
It allows developers to write generic and scalable code. The same method can work with different types of objects, reducing code duplication.
It allows developers to write generic and scalable code. The same method can work with different types of objects, reducing code duplication.
Q3. What is Method Overriding in Polymorphism?
Method overriding occurs when a child class provides its own implementation of a method that already exists in the parent class.
The child class replaces the parent class method with its own version.
Example
The child class replaces the parent class method with its own version.
Example
class Animal { speak() { return "Animal makes a sound"; }}
class Dog extends Animal { speak() { return "Dog barks"; }}
const dog = new Dog();console.log(dog.speak());Q4. How does Polymorphism improve code reusability?
Polymorphism allows one method to be used for different objects or data types.
Instead of writing separate methods for each object, a single method can perform different tasks depending on the object calling it.
Instead of writing separate methods for each object, a single method can perform different tasks depending on the object calling it.
Q5. What is Runtime Polymorphism?
Runtime polymorphism occurs when method overriding happens during program execution.
The method that gets executed depends on the object type at runtime rather than compile time.
The method that gets executed depends on the object type at runtime rather than compile time.
