What is polymorphism: OOP, ad hoc, universal

What is polymorphism: OOP, ad hoc, universal

What is polymorphism in object oriented programming ? The word polymorphism comes from Greek and means having several different forms. This is one of the essential concepts of object-oriented programming. Where inheritance is related to classes and (their hierarchy), polymorphism is related to object methods.

What are the different types of polymorphism?

In general, there are three types of polymorphism: Overloading polymorphism; Parametric polymorphism; and Inclusion polymorphism.

The different types of polymorphism

What is overloading polymorphism?

Overloading polymorphism is where functions of the same name exist, with similar functionality, in classes that are entirely independent of each other (these do not have to be children of the object class). For example, the complex, image, and link classes may each have the function "display." This means that we do not need to worry about what type of object we are dealing with if all we want to do is display it on the screen.

Therefore, overloading polymorphism allows us to define operators whose behavior will vary depending on their applied parameters. Therefore it is possible, for example, to add the + operator and make it behave differently according to whether it refers to an operation between two integers (addition) or between two character strings (concatenation).

What is parametric polymorphism?

Parametric polymorphism is also called template polymorphism. This is the ability to define several functions using the same name but using different parameters (name and/or type). Parametric polymorphism automatically selects the correct method to be adopted according to the data type passed in the parameter.

We can therefore define several addition () methods using the same name, which calculates a sum of values.

  • The int addition(int, int) method would return the sum of two integers.
  • The float addition(float, float) would return the sum of two floats.
  • The char addition(char, char) would result in the sum of two characters as defined by the author.
  • etc.

A signature is a name and type (static) given to the arguments of a function. Therefore it is a method's signature that determines what is called on.

What is an inclusion polymorphism?

The ability to redefine a method in classes inherited from a base class is called specialization. One can therefore call on an object's method without having to know its intrinsic type: this is inclusion polymorphism (also called redefinition or overriding). This makes it possible to disregard the specialized class details of an object family by masking them with a common interface (this being the basic class).

Imagine a game of chess, with the objects king, queen, bishop, knight, rook and pawn, each inheriting the piece object. The movement method could using inclusion polymorphism, make the corresponding move according to the object class that is called on. Therefore, this allows the program to perform piece.movement without having to be concerned with each piece's class.