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.
Types of polymorphism
In general, there are three types of polymorphism: Overloading polymorphism; Parametric polymorphism; and Inclusion polymorphism.
Overloading polymorphism is where functions of the same name exist, with similar functionality, in classes which are completely independent of each other (these do not have to be children of the object class). For example, the complex class, the image class and the link class 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 the parameters that are applied to them. 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).
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 type of data 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 as the sum of two characters as defined by the author.
A signature is the name and type (static) given to the arguments of a function. Therefore it is a method's signature that determines what is called on.
The ability to redefine a method in classes that are 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.
- Types of polymorphism in oops
- Types of polymorphism in oop - Best answers
- 3 types of polymorphism - Best answers
- Typing master pro download - Download
- How to type @ on keyboard - Guide
- Which is not a hard disk partition type - Articles
- How to type e with accent - Guide
- Typing tutor for kids free download - Download