Polymorphism in C#

Introduction

In this article we will learn about one of the reusable object oriented features of C#, we will learn about polymorphism from the basics because I wrote this article focusing on students and beginners. Before proceeding further please refer to my previous articles for a better understandability.

What is Polymorphism in C#?

Polymorphism means one thing havingmany (poly) forms. Suppose the example shown in the following diagram.

In the preceding example a vehicle is something that has various forms; two-wheeler, three-wheeler and four-wheeler and so on. So how to differentiate each form in the preceding example using wheels (parameters)? Let us see more about polymorphism.

Type of Polymorphism in C#

Compile Time Polymorphism

Compile time polymorphism is done at compile time. The following are examples of compile time polymorphism.

  • Method overloading
  • Operator overloading

What is Method overloading?

Creating multiple methods in a class with the same name but different parameters and types is called method overloading.

Method overloading can be done in any of the following ways.

  • By changing the number of parameters used.
  • By changing the order of parameters.
  • By using different data types for the parameters.

Example

namespace BAL
{
    public class MethodOverloading
    {
        public int Add(int a, int b) // Method1
        {
            return a + b;
        }

        public int Add(int a, int b, int c) // Method2
        {
            return a + b + c;
        }

        public float Add(float a, float b, float c, float d) // Method3
        {
            return a + b + c + d;
        }
    }
}

In the preceding method their are three methods with the same name but different parameter type. This is called method overloading. In my next article we will see the program method of overloading in details.

Run time polymorphism

Run time polymorphism happens at run time, in other words values are ed at runtime to the method. Runtime polymorphism can be done using method overriding.

What is Method overriding?

Creating the method in a derived class with the same name, same parameters and same return type as in the base class is called method overriding.

  • Method overriding is only possible in a derived class, not within the same class where the method is declared.
  • Only those methods overriden in the derived class declared in the base class using the virtual keyword or abstract keyword.

Example

namespace BAL
{
    public class MethodOverriding
    {
        public virtual int Balance()
        {
            return 10;
        }
    }

    public class Amount : MethodOverriding
    {
        public override int Balance()
        {
            return 500;
        }
    }
}

Output

overlosding

In the preceding program we declare the Virtual method that returns 10 and the same method we are using in the class Amount using the override keyword that at runtime returns 500 without changing the values of the method, even the names are the same so the example above shows runtime polymorphism. In my next article we will see in details a program with runtime polymorphism.

Difference between method overloading and method overriding


Method overloading

Creating multiple methods in a class with the same name but different parameters and types is called method overloading.

Method overriding

Creating the method in a derived class with the same name, the same parameters and the same return type as in a base class is called method overriding.

Difference between virtual method and abstract method

FAQ's

Q. Can method overloading have the same number of parameters with different return types?

Ans. No, because a conflict occurs in methods when ing the parameters.

Q. What is operator overloading?

Ans. We can redefine operators like +, - and * with additional functionalities.

Summary

I hope you have learned an overview of polymorphism and its types. In the next article we will learn eachpolymorphism type in detail. If you have any suggestions regarding this article then please contact me.


Similar Articles