Polymorphism

 

The new normal on a packed subway ride
Brooklyn Subway

Is the following still a common question in 2021 ?

Describe and give an example of each of the following types of polymorphism:

    • Ad-hoc polymorphism
    • Parametric polymorphism
    • Subtype polymorphism

Subtype polymorphism

This is the type of polymorphism usually learned when beginning Object Oriented Programming. Object classes that are extended, still share common behaviors and attributes. Functions or methods can be implemented that operate on these common behaviors and attributes.

One naïve example could be the act to registering or licensing a pet which could be common implementation to a pet regardless of the type of pet. As long as the object conforms to the interface imposed by parent class that method can be called using the object.

Parametric polymorphism

Improvements to programming languages introduced Generics in which data structures like Lists could perform operations like add, remove or delete on the specified Class Type. Similarly it is possible to define generic classes that provide a host of functions or methods capable of operating on the specified Class Type.

Ad-Hoc polymorphism

And finally in most languages it is possible to overload a function so that if behaves differently depending on the type of parameter passed into it

Extending our naïve example of pets, it is possible to implement two separate functions both called feed, that implements different actions depending on whether the input is a cat or a dog.