Dynamic binding Flashcards
What is static binding?
the compiler uses the type of variables to do the binding to methods.
compile time feature
What is dynamic binding?
the decision is made at runtime based on the type of the actual values
run-time feature
exists in both static and dynamic languages.
What type of binding used in Java, C#, C++ by default?
Java - dynamic binding (encourages OOP)
C# - static binding (strict inheritance by default)
C++ - static binding (because of zero overhead principle).
Java: can final methods be invoked via dynamic binding?
yes. if the method overrides a non-final method and the static type is of the inherited class (the “parent”).
What’s replacement overriding? what are the downsides?
The new implementation of an operation replaces the implementation of the operation in the base class
Downsides: disallows code reuse, use case doesn’t justify language limitation.
What’s Refinement overriding?
The new implementation of an operation refines the implementation of the operation in the base class
Overridden method can be called even though an overriding method exists.
רק מוסיפים לפונקציה המקורית, לכן אין צורך לשכפל קוד וניתן פשוט לקרוא למתודה של ההורה.
What are the two refinement strategies?
Alpha refinement – child’s method called first, internally calls parent’s method if it wants
Beta refinement – Parent’s method called first, internally calls child’s method if it wants
What is const_cast in c++?
Purpose: cast away const
Targets: pointers and references
Run-time overhead: none – merely instructs the compiler to allow mutation of the target
What is reinterpret_cast in c++?
Purpose: instruct the compiler to interpret a bunch of bits differently
Targets: mostly pointer and integral types
Run-time overhead: none
What is static_cast in c++?
Purpose: cast related types and numeric types
Targets: mostly pointers and numeric types
Run-time overhead: low (no RTTI)
Shortcomings compared to dynamic_cast:
- Can’t down-cast from virtual base class
- No indication whether cast was legal
What is dynamic_cast in c++?
Purpose: cast to a different sub-object of a polymorphic type
- Non-polymorphic types will yield a compiler error
- Sub-object type usually derived from target’s type, but may be anywhere in the dynamic type’s inheritance
graph
Targets: pointers, references
Run-time overhead: RTTI lookup, possibly noticeable
What is C-style cast?
does the first C++ cast that is legal within a well defined series of attempts