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.
GNP exceeds NNP by:
Country A can produce 10 units of cloth or 20 units of wheat per day. Country B can produce 15 units of cloth or 15 units of wheat per day. Which of the...
Under a fixed exchange rate system (A)_________ would be an exogenous monetary policy instrument, whereas under a flexible exchange rate system (B) ____...
Hirschman takes divergent series of investment as a project that :Â
A firm sells 40 units of commodity X when its price is 10. At what price it will sell 60 units of the commodity if its price elasticity of supply is 0....
In a given year, the nominal GDP of a country was $2,500 billion and the real GDP was $2,000 billion. What is the GDP deflator for that year?
What is sum product of the error an independent variable in OLS estimation
Which among the following are the recommendations of the Urjit Patel Committee report on monetary policy?
I. Curtailment of the fiscal deficit.
Which of the following is correct regarding long run cost
The rate of interest paid when one bank borrows reserves from another bank is called the: