Polymorphism in C# with Method Overloading And Method Overriding

Dogan Ogut
5 min readMay 4, 2022

--

Polymorphism means “Many Forms”. In Polymorphism, poly means “Many” and morph means “Forms.” Polymorphism is one of the main pillars in Object Oriented Programming. It allows you to create multiple methods with the same name but different signatures in the same class. The same name methods can also be in derived classes.

There are two types of Polymorphism,

  • Method Overloading
  • Method Overriding

These are often confused because of their similar sounding names.

In this article, we will cover explain method overloading and method overriding concept in C#. I will try to demonstrate step by step differences between these.

Method Overloading

Method Overloading is known as compile-time (or static) polymorphism because each of the different overloaded methods is resolved when the application is compiled. It has several names like “Compile Time Polymorphism” or “Static Polymorphism” and sometimes it is called “Early Binding”.

Method Overloading means creating multiple methods in a class with same names but different signatures (ie. parameters). We can achieve overloading by varying the number of parameters, the order of parameters, and data types of parameters.

It permits a class, struct, or interface to declare multiple methods with the same name with unique signatures.

Compiler automatically calls required method to check number of parameters and their type which are passed into that method.

using System;  
namespace DemoCsharp
{
class Program
{
public int Add(int num1, int num2)
{
return (num1 + num2);
}
public int Add(int num1, int num2, int num3)
{
return (num1 + num2 + num3);
}
public float Add(float num1, float num2)
{
return (num1 + num2);
}
public string Add(string value1, string value2)
{
return (value1 + " " + value2);
}
static void Main(string[] args)
{
Program objProgram = new Program();
Console.WriteLine("Add with two int parameter :" + objProgram.Add(3, 2));
Console.WriteLine("Add with three int parameter :" + objProgram.Add(3, 2, 8));
Console.WriteLine("Add with two float parameter :" + objProgram.Add(3 f, 22 f));
Console.WriteLine("Add with two string parameter :" + objProgram.Add("hello", "world"));
Console.ReadLine();
}
}
}

Output:

Add with two int parameter :5
Add with three int parameter :13
Add with two float parameter :25
Add with two string parameter :hello world

In the above example, you can see that there are four methods with same name but type of parameters or number of parameters is different. When you call Add(4,5), complier automatically calls the method which has two integer parameters and when you call Add(“hello”,”world”), complier calls the method which has two string parameters. So basically in method overloading complier checks which method should be called at the time of compilation.

Note: Changing the return type of method does not make the method overloaded. You cannot create method overloaded vary only by return type.

Method Overriding

Overriding, on the other hand, is the ability to redefine the implementation of a method in a class that inherits from a parent class. When a method is overridden, the name and the parameters stay the same, but the implementation that gets called depends on the type of the object that’s calling it.

It has several names like “Run Time Polymorphism” or “Dynamic Polymorphism” and sometime it is called “Late Binding”. It is called runtime (or dynamic) polymorphism because the type of the calling object is not known until runtime, and therefore the method implementation that runs is determined at runtime.

If we want to reiterate; method overriding means having two methods with same name and same signatures (ie. parameter), one should be in the base class and other method should be in a derived class (child class). You can override the functionality of a base class method to create a same name method with same signature in a derived class. You can achieve method overriding using inheritance. Virtual and Override keywords are used to achieve method overriding.

using System;
namespace TestPolymorphism
{

class BaseClass
{
public virtual int Add(int num1, int num2)
{
return (num1 + num2);
}
}
class ChildClass: BaseClass
{
public override int Add(int num1, int num2)
{
if (num1 <= 0 || num2 <= 0)
{
Console.WriteLine("Values could not be less than zero or equals to zero");
Console.WriteLine("Enter First value : ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Second value : ");
num2 = Convert.ToInt32(Console.ReadLine());
}
return (num1 + num2);
}
}
class Program
{
static void Main(string[] args)
{
BaseClass baseClassObj;
baseClassObj = new BaseClass();
Console.WriteLine("Base class method Add :" + baseClassObj.Add(-3, 8));
childClassObj = new ChildClass();
Console.WriteLine("Child class method Add :" + childClassObj.Add(-2, 2));
Console.ReadLine();
}
}
}

Output:

Base class method Add :5
Values could not be less than zero or equals to zero
Enter First value :
5
Enter Second value :
7
Child class method Add :12

In the above example, we have created two same name methods in the BaseClass as well as in the ChildClass.

We use virtual and override keywords are used which means the base class is virtual and child class can implement this class and override means this child class has the same name and same method signature as parent class.

When you call the BaseClass Add method with less than zero value as parameters then it adds successfully. But when you call the ChildClass Add method with less than zero value then it checks for negative value. And the passing values are negative then it asks for new value.

Here the BaseClass still uses it’s own implementation, but the inherited ChildClass has it’s own overridden implementation that it uses. The application checks at runtime what the type of the class is (Base or Child) and calls the method for that particular type.

So, here it is clear that we can modify the base class methods in derived(child) classes.

Remember these restrictions:

  • Method cannot be private.
  • Only abstract or virtual method can be overridden.
  • Which method should be called is decided at run time.
table courtesy of the geeksforgeeks

Conclusion

So, today we learned what Polymorphism is in OOP and we have compared two forms of polymorphism in C#, overloading and overriding. We have seen that:

  • Overloading is determined at compile time and is static. Overriding is determined at runtime and is dynamic.
  • Overloading concerns giving a method with the same name different parameters. Overriding concerns defining a different implementation of the same method in inherited classes.

Note: To run the codes in VS Code : https://docs.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code?pivots=dotnet-6-0

Or try this interactive online IDE :JDoodle Online C# Compiler IDE with the code snippets below.

Method-Overloading:

Method-Overriding:

--

--

Dogan Ogut
Dogan Ogut

Written by Dogan Ogut

A Full-stack developer | Student who is trying to be a human among people

No responses yet