12 - Polymorphism in C#

12.1 Introduction to Polymorphism

The term polymorphism was derived from the Greek word ‘poly’ means ‘many’ and ‘morphos’ means ‘forms’. It is the ability to allow a function to exist in different forms.

It allows user to invoke derive class methods through the base class during run time. A person can react differently in various situations. Hence, different methods work differently in their specific situations.

There are two types of polymorphism as follows:

1) Static Polymorphism: In static polymorphism, the response to a function is decided at compile time

2) Dynamic Polymorphism: In dynamic polymorphism, the response to the function is decided at run time.

12.2 Static Polymorphism

Static polymorphism refers to an entity that exists in different forms simultaneously. The concept of static polymorphism is similar to role of a person in specific situation.

C# uses the following approaches to implement polymorphism. They are as mentioned below:

1) Function overloading for implementing static polymorphism

2) Operator overloading for implementing dynamic polymorphism

1) Function overloading for implementing static polymorphism

Function overloading allows the user to use the same name for two or more functions. Each of these functions have the same name must use different function signature. The signature of a function is defined as follows:

a) The number of parameters. Consider the following code

void AddNumber (int);
void AddNumber(int, float);

In the above code, the two functions are different because of their number of parameters.

b) The data type of parameters. Consider the following code snippet.

void Display(int);
void Display(float);

In the above code, the two functions are different because of the data type of the parameters.

c) The sequence of the parameters. Consider the following code snippet.

void Display(int, char);
void Display(char, int);

In the above code, the order of the parameters is different in the declarations.

2) Operator overloading for implementing dynamic polymorphism

Operator overloading allows different operators to be applied to different user defined data types. User can redefine the + operator for a user defined class, Hour. Two Hour objects can be added by using the + operator.

The following code snippet shows the use of overloading a + operator.

Hour h1;
Hour h2;
Hour h3;
h3 = h1+h2;

 

In the above code, the + operator will add the value of h1 and h2 and assign them to h3.

In dynamic polymorphism, the decision about the function execution is made at the run time. It provides flexibility for manipulating objects.

12.3 Dynamic Polymorphism

C# uses two approaches to implement the concept of dynamic polymorphism. They are as follows:

a) Abstract classes

b) Virtual functions

a) Abstract classes: These are special type of base classes that consists of abstract class members. All the class members that are derived directly from the abstract classes must implement all the abstract functions and properties.

b) Virtual Functions: They are the functions that do not exist. They appear to be present in some parts of the program.

Implement Function overloading

It is the most common way to implement polymorphism. User can implement function overloading by defining two or more functions in a class having the same name. The function signature is different for every function.

The code example for function overloading is as shown below:

class CalculateMax
{
    public int Max(int no1, int no2)
    {
        if(no1>no2)
        {
            return no1;
        }
        else
        {
            return no2;
        }
    }
    public float Max(float no1, float no2)
    {
        if(no1>no2)
        {
            return no1;
        }
        else
        {
            return no2;
        }
    }
class MaxCalc
{
    static void Main ( string[ ] args )
    {
        CalculateMax c = new CalculateMax();
        Console.WriteLine(“The int value is {0}”, c.Max(3,4));
        Console.WriteLibe(“The float value is {0}”, c.Max(6.4F, 4.4F ) );
        Console.Read();
    }
  }
}

 

The output for the code is as shown below:

In the above code, the Max() method displays the maximum of two numbers, no1 and no2. The Max() method is overloaded by passing integer and float values.

12.4 Constructor Overloading

Constructors can be parameterized and therefore they can be overloaded. They are commonly used in C# to provide flexibility while creating an object.

The following code shows the use of constructor overloading in C#.

class CalculateNumber
{
    int no1, no2, total;
    public CalculateNumber()
    {
        no1 = no2 = total = 0;
    }
    public CalculateNumber(int num1, int num2)
    {
        no1 = num1;
        no2 = num2;
        total = 0;
    }
    public void AddNumber()
    {
        total = no1+no2;
    }
    public void display()
    {
        Console.WriteLine(“The sum of two numbers is {0}”, total);
    }
}
class CalNum
{
    static void Main ( string[ ] args)
    {
        CalculateNumber c1 = new CalculateNumber();
        CalculateNumber c2 = new CalculateNumber(4,5);
        c1.AddNumber();
        c1.display();
        c2.AddNumer();
        c2.display();
        Console.Read();
    }
}
}

 

The output for the code is as shown below:

Like us on Facebook