Q: What is one advantage of using polymorphism in OOP?
-
A
It restricts the use of interfaces.
-
B
It allows methods to operate on objects of different classes through a common interface.
-
C
It increases the complexity of method invocation.
-
D
It limits code flexibility.
B
Answer:
B
Explanation:
Polymorphism in object-oriented programming allows methods to operate on objects of different classes through a common interface, providing significant flexibility and reusability in the code. This capability is achieved through method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass, and through method overloading, where multiple methods with the same name but different parameters are defined within a class. Polymorphism enables dynamic method binding, where the method that gets executed is determined at runtime based on the actual type of the object. This allows for the creation of more generic and flexible code, as methods can be written to work with objects of various types, reducing the need for extensive conditionals and type checking. As a result, polymorphism enhances the extensibility and maintainability of the software, making it easier to adapt to new requirements and changes.
Related Topic:
Share Above MCQ