Types of Polymorphisms - Ad-hoc, Inclusion, Parametric & Coercion

Polymorphism is a fundamental concept in programming that allows entities to take multiple forms. There are four main types of polymorphism −

  • Ad-Hoc Polymorphism (Function Overloading)
  • Inclusion Polymorphism (Subtyping)
  • Parametric Polymorphism (Generics/Templates)
  • Coercion Polymorphism (Type Casting)
Note: This article explains polymorphism concepts using C syntax for illustration. Pure C doesn't support object-oriented polymorphism natively, but these concepts can be demonstrated through function pointers and other techniques.

Ad-Hoc Polymorphism (Function Overloading)

Ad-hoc polymorphism allows multiple functions with the same name to operate on different data types. In C, this is achieved through different function names or generic programming −

#include <stdio.h>
#include <string.h>

int addInt(int a, int b) {
    return a + b;
}

double addDouble(double a, double b) {
    return a + b;
}

int main() {
    printf("Addition of integers: %d<br>", addInt(5, 3));
    printf("Addition of doubles: %.2f<br>", addDouble(3.14, 2.86));
    return 0;
}
Addition of integers: 8
Addition of doubles: 6.00

Inclusion Polymorphism (Runtime Polymorphism)

Inclusion polymorphism is achieved in C using function pointers, allowing different implementations to be called through the same interface −

#include <stdio.h>

typedef struct {
    void (*print)(void);
} Shape;

void printCircle() {
    printf("This is a circle.<br>");
}

void printSquare() {
    printf("This is a square.<br>");
}

int main() {
    Shape shape1 = {printCircle};
    Shape shape2 = {printSquare};
    
    shape1.print();  // Calls printCircle
    shape2.print();  // Calls printSquare
    return 0;
}
This is a circle.
This is a square.

Coercion Polymorphism (Type Casting)

Coercion polymorphism occurs when data types are automatically or explicitly converted from one type to another −

#include <stdio.h>

void displayValue(int x) {
    printf("Integer value: %d<br>", x);
}

int main() {
    int intVal = 42;
    float floatVal = 3.14f;
    char charVal = 'A';
    
    displayValue(intVal);           // Direct integer
    displayValue((int)floatVal);    // Explicit cast
    displayValue(charVal);          // Implicit cast (char to int)
    
    return 0;
}
Integer value: 42
Integer value: 3
Integer value: 65

Parametric Polymorphism (Generic Programming)

Parametric polymorphism in C is achieved using generic macros or void pointers. Here's an example using macros −

#include <stdio.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main() {
    int x = 10, y = 20;
    double p = 3.5, q = 2.1;
    char c1 = 'A', c2 = 'Z';
    
    printf("Max of integers (%d, %d): %d<br>", x, y, MAX(x, y));
    printf("Max of doubles (%.1f, %.1f): %.1f<br>", p, q, MAX(p, q));
    printf("Max of chars ('%c', '%c'): %c<br>", c1, c2, MAX(c1, c2));
    
    return 0;
}
Max of integers (10, 20): 20
Max of doubles (3.5, 2.1): 3.5
Max of chars ('A', 'Z'): Z
Updated on: 2026-03-15T11:39:42+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements