Question
Which of the following is the primary reason why
polymorphism is useful in Object-Oriented Programming (OOP)?Solution
Polymorphism in OOP is primarily beneficial because it allows objects of different classes to be treated as objects of a common superclass. This enables objects to be used interchangeably, which is useful when designing flexible and reusable code. In OOP, polymorphism is achieved through method overriding (in inheritance) or interfaces, allowing for the same method name to behave differently depending on the object type invoking it. This leads to more generic code, easier maintenance, and greater extensibility. For instance, a superclass Shape could define a method draw() . Subclasses like Circle and Square would each override the draw() method, but polymorphism allows a reference of type Shape to invoke the draw() method of either subclass without needing to know which specific subclass it is dealing with. Why Other Options Are Incorrect:
- A) While encapsulation is a core OOP principle, it is not the primary reason polymorphism is useful. Encapsulation is concerned with hiding object details and is not directly related to the dynamic behavior of polymorphism.
- B) Inheritance is indeed a key OOP feature, but it is not polymorphism. Inheritance enables classes to inherit behavior, while polymorphism allows objects of different types to be treated uniformly.
- D) Constructors are used for object creation, and polymorphism is about dynamic method invocation, not constructor elimination.
- E) Object comparison (using methods like equals() ) is a different concept related to object equality rather than polymorphism.
√323.89 × (3.20) ÷ 9.02 = ?
In 2017, Sunny invested Rs 3500 at S.I and rest at C.I. If the total amount received after respective time is Rs 8960, then find the time for which the ...
In 2015, the difference between compound interest and simple interest for the given time is Rs 20. If the sum is invested for 3 years, what will be the ...
What is the percentage profit (approximate) on selling dates if its selling price is 120% of the selling price of Raisins?
If number of staff members in Patna is 10% less than number of staff members in Chandigarh,in which city the total staff members who work in Credit depa...
If the percentage of profit on selling Almonds is 25%, then what is its cost of packaging?
In 2014, if rate of interest is increased by 3%, what will be the amount received after given time?
If the total number of persons who attended the workshop in 2016 is 324, then what is the total number of Professors who attended the workshop in all t...
In 2016, the interest was semi-annually, the interest would have been Rs 1030.402 for given time. Find the interest when compounded annually.
In 2013, the amount becomes Rs 28,700 after 8 years, find the rate of interest.