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.
A seller marked an item above its cost price and allows a discount of 25% on it. On the discounted price, the seller charges a packaging price which is ...
The item "Butter" is marked up by Rs. 490 above the cost price. Subsequently, a discount is applied, resulting in a 12% profit for the entire transactio...
- A shopkeeper offered two back-to-back discounts of x% and sold an article for Rs. 1800. The marked price was Rs. 3200. What was the percentage of each disc...
If by selling an article for Rs. 366 a shopkeeper gains 22%, find its cost.
When a lamp and a table are sold at 10% profit and 25% profit, respectively, the seller earns Rs. 30 more in comparison when they are sold by interchang...
A shopkeeper loses 15% by selling article A for Rs 212.50 and gains 35% by selling article B for Rs 1012.50. What is his overall gain percentage in the ...
Akash bought a sofa for Rs. 12500. After one year he sold it to Deepak at 12% less of his cost price. Deepak spends extra Rs. 500 for its repair. And of...
A shopkeeper bought an article and marked it at Rs. 600. By selling the article at a discount of 4%, he earns a profit of 20%. Find the cost price of th...
- Varun sold an item to Ritu at a 12% profit. Ritu then sold it to Nikhil at a 28% profit. If Nikhil purchased the item for Rs. 798.72, what was the original...
A fruit seller sells mangoes at the rate of Rs.9 per kg and thereby loses 20%. At what price per kg, he should have sold them to make a profit o...