Distinguish static (compile-time) polymorphism from dynamic (runtime) polymorphism with examples.

Prepare for the Object-Oriented Programming Test with flashcards and multiple choice questions. Each question comes with hints and explanations to enhance understanding. Master OOP concepts and succeed in your exam!

Multiple Choice

Distinguish static (compile-time) polymorphism from dynamic (runtime) polymorphism with examples.

Explanation:
The key idea is that static polymorphism is decided during compilation, while dynamic polymorphism is decided at runtime based on the actual object’s type. Static polymorphism happens when you have multiple methods with the same name but different signatures, like overloaded methods. The compiler chooses the correct one at compile time by looking at the types of the arguments. For example, if you have print(int) and print(string), a call with an int argument binds to the int version before the program runs. Templates in languages like C++ can also be a form of static polymorphism because decisions are made at compile time based on the types used with the template. Dynamic polymorphism relies on virtual or override mechanisms. The call to a method is resolved at runtime using the actual object’s type, not just the reference or pointer type. For instance, with a base class Animal that defines a virtual speak method and derived classes Dog and Cat that override speak, calling a->speak() through a base class pointer will invoke Dog’s version if the pointer actually refers to a Dog object. The dispatch is done via a runtime mechanism such as a vtable. That’s why the correct description is that static polymorphism resolves overloads at compile time, while dynamic polymorphism uses virtual/override to resolve the call at runtime based on the real object. The other statements either flip the timing, misstate the requirements (inheritance vs. interfaces), or claim there is no difference.

The key idea is that static polymorphism is decided during compilation, while dynamic polymorphism is decided at runtime based on the actual object’s type.

Static polymorphism happens when you have multiple methods with the same name but different signatures, like overloaded methods. The compiler chooses the correct one at compile time by looking at the types of the arguments. For example, if you have print(int) and print(string), a call with an int argument binds to the int version before the program runs. Templates in languages like C++ can also be a form of static polymorphism because decisions are made at compile time based on the types used with the template.

Dynamic polymorphism relies on virtual or override mechanisms. The call to a method is resolved at runtime using the actual object’s type, not just the reference or pointer type. For instance, with a base class Animal that defines a virtual speak method and derived classes Dog and Cat that override speak, calling a->speak() through a base class pointer will invoke Dog’s version if the pointer actually refers to a Dog object. The dispatch is done via a runtime mechanism such as a vtable.

That’s why the correct description is that static polymorphism resolves overloads at compile time, while dynamic polymorphism uses virtual/override to resolve the call at runtime based on the real object. The other statements either flip the timing, misstate the requirements (inheritance vs. interfaces), or claim there is no difference.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy