Q: What benefit does polymorphism provide in OOP?
-
A
It allows objects to be of different types at runtime.
-
B
It restricts objects to a single type.
-
C
It eliminates the need for interfaces.
-
D
It reduces the flexibility of code.
A
Answer:
A
Explanation:
Polymorphism is a powerful feature of object-oriented programming that allows objects to be treated as instances of their parent class rather than their actual class. This means that a single function or method can work with different types of objects, enabling code to be more flexible and reusable. Polymorphism is achieved through method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass. It allows for dynamic method binding, which means that the method that gets executed is determined at runtime based on the object's actual type. This capability enhances the flexibility and scalability of the code, as it can handle new, unforeseen types of objects without modification.
Related Topic:
Share Above MCQ