Benefits of Polymorphism | Java SE 8 Programmer I

Benefits of Polymorphism

Question

Which two are benefits of polymorphism?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D. E.

BD.

https://www.cs.princeton.edu/courses/archive/fall98/cs441/mainus/node5.html

Polymorphism is a key concept in object-oriented programming (OOP) and refers to the ability of an object to take on many forms. It allows objects of different classes to be treated as if they are objects of the same class. The benefits of polymorphism are as follows:

  1. More flexible and reusable code: Polymorphism makes code more flexible and reusable because it allows objects of different classes to be treated as if they are objects of the same class. This means that if you have a method that works with an object of a particular class, you can also use that method with objects of other classes that inherit from that class or implement the same interface. This can save a lot of time and effort because you don't have to write a separate method for each class.

  2. More dynamic code at runtime: Polymorphism makes code more dynamic at runtime because it allows the actual method that gets called to be determined at runtime rather than at compile time. This means that if you have a method that is overridden in a subclass, the version of the method that gets called will depend on the type of the object at runtime. This can lead to more flexible and powerful code that can adapt to different situations.

Option A and B are incorrect because polymorphism does not directly affect the speed or efficiency of code at runtime. Option C is partially correct because polymorphism can make code more dynamic at runtime, but it is not the only factor that affects code dynamism. Option E is incorrect because polymorphism actually makes code more extensible by allowing objects of different classes to be treated as if they are objects of the same class.