Question:

What is polymorphism? Write short notes about the types of polymorphism.

Show Hint

Polymorphism enables objects of different classes to be treated as objects of a common superclass, making the code more flexible and maintainable.
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Define polymorphism.
Polymorphism is the ability of a single function, method, or operator to behave differently depending on the type or number of arguments. It is a core concept in object-oriented programming (OOP) that allows methods or functions to be used in multiple forms.
Step 2: Types of polymorphism.
(i) Compile-time polymorphism (Static polymorphism):
This type of polymorphism occurs when the method to be called is determined at compile time. Method overloading and operator overloading are examples of compile-time polymorphism. (ii) Runtime polymorphism (Dynamic polymorphism):
This type of polymorphism occurs when the method to be called is determined at runtime. Method overriding is a common example of runtime polymorphism, where a method in the base class is overridden by a method in the derived class.
Step 3: Conclusion.
Polymorphism enhances flexibility and the reusability of code by allowing the same method to work in different ways based on the input or object type.
Was this answer helpful?
0
0