Introducing Polymorphism C#
The term polymorphism was derived from the Greek words ‘poly’ and ‘morphos’, which mean ‘many’ and ‘forms’, respectively. In Object-Oriented Programming (OOPs), polymorphism is often expressed by the phrase “one interface, multiple functions”. This expression means that polymorphism allows one interface to be used for multiple functions. You can apply polymorphism for reducing the complexity within the functions of a class of your program.
Polymorphism can either be static dynamic. In static polymorphism the response to a function is decided at compile time. In dynamic polymorphism the response to the function is decided at run time.
Static polymorphism refers to an entity, which exists in various forms simultaneously. The concept of static polymorphism is analogous to the role of a woman who can be a wife, a mother, a daughter, a sister, and an executive at the same time.
The mechanism of linking a function with an object during compile time is called early binding. It is also known a static binding.
C# uses two approaches to implement static polymorphism. These are:
· Function Overloading
· Operator Overloading
This approach allows using the same name for two or more functions. Each redefinition of a function must use different types of parameters, sequence of parameters, or a number of parameters. The type, sequence, or number of parameters for a function is called function signature. When you have multiple functions with the same name, the compiler identifies the function based on the parameters to the function.
Consider an example, to understand the benefit of function overloading where you need a function that converts distance in kilometers to miles, and kilometers can either be an integer or a float. One approach can be to have two functions of different name, as shown in the following code:
int ConvertInteger(int km);
int ConverFloat(float km);
Another approach can be using function overloading. You can have two functions with the same name but with different parameters, as shown in the following code:
int Convert(int km);
float Conver(float km);
This approach allows user-defined types such as structures and classes, to use overloaded operators for easy manipulation of their objects. Operator overloading can be achieved by defining the static member functions using the operator keyword. For example, you can redefine the + operator for a user-defined class, Hour to add the hour value of two class objects. You can then add two Hour objects by using the + operator.
The following code is an example showing the usage of operator (+):
h3 = h1+h2;
In dynamic polymorphism the decision about function execution is made at run time. Dynamic polymorphism is more useful as compared to static polymorphism because dynamic polymorphism gives much more flexibility for manipulating objects.
The mechanism of linking a function with an object at run time is called dynamic or late binding. Dynamic binding supports virtual functions.
C# uses two approaches to implement dynamic polymorphism. These are:
· Abstract classes: Are the special type of base classes that consist of abstract class members. All the class members that are derived directly from abstract classes must implement all the abstract functions and properties.
Virtual functions: Are the functions that do not really exist, however, appear to be present in some parts of the program.