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.
By interchanging the given two signs and numbers which of the following equation will be correct?
× and ÷, 7 and 9
The ratio of the income and savings of John is 5:2, respectively. If his expenditure increases by an amount which is equal to half of his initial saving...
- A merchant made a profit of Rs. 600 on selling a gadget. Had the cost price been Rs. 400 lower and the selling price Rs. 900 more, the profit would have be...
Which of the following city G goes?
Four of the following five are alike in a certain way and hence form a group. Find the one who doesn’t belong to that group.
- ‘P’, ‘Q’ and ‘R’ divide a certain amount among themselves. The average of their shares is Rs. 4860. Share of ‘P’ is 12(1/2)% more than shar...
If all the digits of the number 789615432 are arranged in decreasing order, then sum of 5 th and 6 th digits (from the left end) of the number thus form...
In a certain code language, ‘accounts receipt expenditure’ is coded as ‘ef ws bn’, ‘rules receipt framed’ is coded as ‘pl ef yu’, and �...
- What is the area of the sector of a circle of radius 10 cm and formed by an arc of length 15 cm?
If all the prime digits of the number 6493858573 are written first (in increasing order) followed by even digits(in increasing order) and then odd digit...