Runtime polymorphism
Appearance
in computing, runtime polymorphism is polymorphism resolved dynamically based on runtime information. This contrasts with compile time polymorphism.
Approaches
Different systems exist for implementing runtime polymorphism with various tradeoffs in performance and flexibility:-
Single dispatch
- in the C++ programming language, vtables embedded in each object instance are used to select behaviour. Compiled code needs to know a base class vtable format and data layout in order to use any derived type. Multiple inheritance is also possible, implemented via internal double indirection to access different base class members.
- in Go and Rust, vtable pointers are carried with object references as 'fat pointers' ('interfaces' or 'trait objects'). This decouples the supported interfaces from the underlying data structures; each compiled library needn't know the full range of interfaces supported in order to correctly use a type, just their required vtable layouts.
Late binding
- in Objective C and swift, method calls are resolved by searching a dictionary binding a message ID to a function.