Polymorphism - Introduction
- Compile-time polymorphism
- Run time polymorphism
- Ad-hoc polymorphism
- Coercion polymorphism
The above four polymorphisms also have different names. Run time polymorphism is also called as Subtype polymorphism, Compile-time polymorphism is also called as the Parametric polymorphism, Ad-hoc polymorphism is also known as overloading, and Coercion polymorphism is also known as the (implicit or explicit) casting.
All this various polymorphisms will make sense after reading some examples which I have shown below.
Compile-time polymorphism (Parametric polymorphism)
Parametric polymorphism is all about executing the same code for any type. Templates are very good example for the parametric polymorphism. One of the simplest example using templates is shown below:
max() function is polymorphic on the type
T. Note that if we specialize the
max() function then it won’t be a parametric polymorphism anymore. It will be an example of Ad-hoc polymorphism or Function overloading.
Parametric polymorphism is also known as compile-time polymorphism because the types are applied to the template at the compile time and many forms of the template is created.
Runtime polymorphism (Subtype polymorphism)
Runtime polymorphism as the name defines is the ability to use the derived classes through the base class pointers and references. Never ever forget the
virtual keyword when it comes to the runtime polymorphism. Overiding the base class function in the derived classes is all what triggers runtime polymorphism.
The resolution of polymorphic function calls happens at runtime through an indirection via the virtual table. Compiler does not locate the address of the function to be called at the compile-time, instead the function is called by dereferencing the right pointer in the virtual table.
So this is also called as inclusion polymorphism (or) runtime polymorphism (or) subtype polymorphism.
Ad-hoc polymorphism (Overloading)
Ad-hoc polymorphism allows functions with the same name to act differently for each type. For example, two
int variables and the
+ operator, it adds them together. Given two
std::string variables it concatenates them together. This is called overloading. Wait, not just overloading. Function overloading. We also have operator overloading which is to overload operators. Ad-hoc polymorphism appears in C++ if we specialize a template, overload a function or if we overload an operator.
Here is an example of Ad-hoc polymorphism that implements
Ad-hoc polymorphism also appears when we specialize the templates as shown below
Now we just call
::max(“aaa”,“bbb”) to find the maximum of strings “aaa” and “bbb”.
Coercion polymorphism (Casting)
Coercion happens when the object or a primitive is cast into another object type or primitive type. For example,
Explicit casting happens when you use C’s type casting expressions or C++ casting expressions like
Coercion also happens if the constructor if the constructor is not explicit, for example,
If you've liked this blog post, consider donating or otherwise supporting me.