Which mechanism provides runtime polymorphism in Java/C++?

Prepare for the 241 Computer Science Certification Exam with comprehensive flashcards and multiple choice questions. Enhance knowledge with explanations and hints to excel in your test journey!

Multiple Choice

Which mechanism provides runtime polymorphism in Java/C++?

Explanation:
Runtime polymorphism happens when the method that runs is determined by the actual object type at runtime, not by the reference type used at compile time. In C++, this is enabled by declaring a function as virtual in a base class and then overriding it in derived classes. When you call that function through a base-class pointer or reference, the runtime uses the object’s real type to pick the correct override, typically via a vtable mechanism. In Java, methods are virtual by default, and interfaces provide a way to treat different implementing classes uniformly; the runtime dispatches to the appropriate method implementation based on the concrete object. This dynamic dispatch is what allows a single piece of code to work correctly with many different subclasses or implementing classes. Other options describe mechanisms that don’t provide runtime dispatch. Operator overloading isn’t about choosing among overridden methods at runtime. Static binding with final classes locks down inheritance and prevents polymorphic overrides. Method overloading resolves calls at compile time based on argument types, not the actual object, so it doesn’t achieve runtime polymorphism.

Runtime polymorphism happens when the method that runs is determined by the actual object type at runtime, not by the reference type used at compile time. In C++, this is enabled by declaring a function as virtual in a base class and then overriding it in derived classes. When you call that function through a base-class pointer or reference, the runtime uses the object’s real type to pick the correct override, typically via a vtable mechanism. In Java, methods are virtual by default, and interfaces provide a way to treat different implementing classes uniformly; the runtime dispatches to the appropriate method implementation based on the concrete object. This dynamic dispatch is what allows a single piece of code to work correctly with many different subclasses or implementing classes.

Other options describe mechanisms that don’t provide runtime dispatch. Operator overloading isn’t about choosing among overridden methods at runtime. Static binding with final classes locks down inheritance and prevents polymorphic overrides. Method overloading resolves calls at compile time based on argument types, not the actual object, so it doesn’t achieve runtime polymorphism.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy