 Open in App
Not now

# Ad-hoc, Inclusion, Parametric & Coercion Polymorphisms

• Difficulty Level : Expert
• Last Updated : 25 Mar, 2023

When we talk about Polymorphism in C++, we come to hear the following four types: Discussing these in details:

1. Ad-hoc Polymorphism, also called as Overloading Ad-hoc Polymorphism allows functions having same name to act differently for different types. For example: The + operator adds two integers and concatenates two strings. Above example could be better illustrated by invoking the function “sum()” in under-mentioned code:

## CPP14

 `#include ``using` `namespace` `std;` `int` `sum(``int` `x, ``int` `y)``{``    ``int` `c = x + y;``    ``return` `c;``}` `string sum(``const` `char``* x, ``const` `char``* y)``{``    ``string summation(x);``    ``summation += y;``    ``return` `summation;``}` `int` `main()``{``    ``cout << sum(50, 20)``         ``<< " :- Integer addition Output\n";``    ``cout << sum("Polymorphism", " achieved")``         ``<< " :- String Concatenation Output\n";``}`

## Java

 `import` `java.util.*;` `public` `class` `Main {``    ``public` `static` `int` `sum(``int` `x, ``int` `y)``    ``{``        ``int` `c = x + y;``        ``return` `c;``    ``}` `    ``public` `static` `String sum(String x, String y)``    ``{``        ``String summation = x.concat(y);``        ``return` `summation;``    ``}` `    ``public` `static` `void` `main(String[] args)``    ``{``        ``System.out.println(sum(``50``, ``20``)``                           ``+ ``" :- Integer addition Output"``);``        ``System.out.println(``            ``sum(``"Polymorphism"``, ``" achieved"``)``            ``+ ``" :- String Concatenation Output"``);``    ``}``}`

## Python3

 `def` `sum``(x, y):``    ``c ``=` `x ``+` `y``    ``return` `c` `def` `sum_str(x, y):``    ``summation ``=` `x ``+` `y``    ``return` `summation` `# Main function``if` `__name__ ``=``=` `'__main__'``:``    ``print``(f``"{sum(50, 20)} :- Integer addition Output"``)``    ``print``(f``"{sum_str('Polymorphism', ' achieved')} :- String Concatenation Output"``)`

Output:

```70 :- Integer addition Output
Polymorphism achieved :- String Concatenation Output```
1. Hence, by calling two different functions(which differ in the type of arguments) having the same names, to execute multiple operations, we have successfully achieved Ad-hoc Polymorphism.
2. Inclusion Polymorphism, also called as Subtyping Inclusion Polymorphism is the ability to use derived classes through base class pointers and references. It is also known as Run-time polymorphism because the address of the function is not located by the Compiler at compile-time, rather, the right pointer from the virtual table is dereferenced to invoke the function at run-time. The concept of Virtual Function, also known as Dynamic Linkage, is employed to achieve Inclusion Polymorphism. The usage of Virtual Function allows the selection of that function which is to be invoked based on the kind of object for which it is called. For example: To implement such a Polymorphism technique, let us take different files under consideration such as .jpg, .gif, .png files. All these files fall under the category of Image Files. So, they can be represented as Classes Derived from Image Base Class and overriding the display() pure virtual function. Above example could be better understood by the following illustration:

## CPP14

 `#include ``using` `namespace` `std;` `class` `Image {` `public``:``    ``Image()``    ``{``    ``}` `    ``virtual` `void` `display() = 0;``};``class` `Jpg : ``public` `Image {``public``:``    ``Jpg()``    ``{``    ``}` `    ``void` `display()``    ``{``        ``cout << "JPG Image File" << endl;``    ``}``};` `class` `Png : ``public` `Image {``public``:``    ``Png()``    ``{``    ``}` `    ``void` `display()``    ``{``        ``cout << "PNG Image File" << endl;``    ``}``};` `// Main function``int` `main()``{``    ``Image* img;``    ``Jpg jg;``    ``Png pg;` `    ``// stores the address of Jpg``    ``img = &jg;` `    ``// invoking display() func of Jpg``    ``img->display();` `    ``// stores the address of Png``    ``img = &pg;` `    ``// invoking display() func of Png``    ``img->display();` `    ``return` `0;``}`

Output:

```JPG Image File
PNG Image File```
1. Hence, in the above code, we have two different Classes with a function having the same name and not differing by Parameters, but with different implementations.
2. Coercion Polymorphism, also called as Casting Coersion Polymorphism occurs when an object or primitive is cast into some other type. It could be either Implicit or Explicit. Implicit casting happens as a responsibility of Compiler itself. For example: float f=100 (integer implicitly gets promoted to float) Explicit casting makes use of some type-casting expressions such as const_cast, dynamic_cast, etc. For example: When a class defines conversion operator for some type, say “int”, then, it could be employed anywhere in the program where integer type data is expected. Illustration Below could make it more easier to understand: This above illustration could be more clarified with help of code below:

## CPP14

 `#include ``using` `namespace` `std;` `class` `IntClass {``    ``int` `num;` `public``:``    ``IntClass(``int` `a)``        ``: num(a)``    ``{``    ``}` `    ``operator ``int``() ``const``    ``{``        ``return` `num;``    ``} ``// conversion from User-defined type to Basic type``};` `void` `show(``int` `x)``{``    ``cout << x << endl;``}` `int` `main()``{``    ``IntClass i = 100;``    ``show(746); ``// outputs 746``    ``show(i); ``// outputs 100``}`

## Java

 `class` `IntClass {``    ``private` `int` `num;` `    ``public` `IntClass(``int` `a) {``        ``num = a;``    ``}` `    ``public` `int` `intValue() {``        ``return` `num;``    ``} ``// conversion from User-defined type to Basic type``}` `class` `Main {``    ``public` `static` `void` `show(``int` `x) {``        ``System.out.println(x);``    ``}` `    ``public` `static` `void` `main(String[] args) {``        ``IntClass i = ``new` `IntClass(``100``);``        ``show(``746``);``        ``show(i.intValue());``    ``}``}`

Output:

```746
100```
1. The IntClass reference is used in place of integer type argument, and hence, the concept of Casting is well understood.
2. Parametric Polymorphism, also called as Early Binding Parametric Polymorphism opens a way to use the same piece of code for different types. It is implemented by the use of Templates. For example: To develop an understanding of this sort of polymorphism, let us execute a program for finding greater of two Integers or two Strings,

## CPP14

 `#include ``#include ``using` `namespace` `std;` `template` `<``class` `temp>``temp greater(temp a, temp b)``{``    ``if` `(a > b)``        ``return` `a;``    ``else``        ``return` `b;``}` `int` `main()``{``    ``cout << ::greater(55, 11) << endl;` `    ``string str1("Early"), str2("Binding");``    ``cout << ::greater(str1, str2) << endl;``}`

Output:

```55
Early```
1. Using Templates, the same function can be parameterized with different types of data, but this needs to be decided at compile-time itself, and hence, this polymorphism is named so. If we wish to achieve such polymorphism for pointers, it turns into Ad-hoc Polymorphism.

My Personal Notes arrow_drop_up