Compare the Difference Between Similar Terms

Difference Between Overriding and Overloading in C#

Key Difference – Overriding vs Overloading in C#
 

The key difference between overriding and overloading in C# is that the binding of overridden method call to its definition happens at runtime while the binding of overloaded method call to its definition happens at compile time.

C# is a general-purpose programming language developed by Microsoft. The main advantage of C# is that it supports Object Oriented Programming (OOP). One pillar of OOP is Polymorphism. It gives an object to have multiple behaviours. There are two types in Polymorphism known as overriding and overloading. This article discusses the difference between method overriding and overloading in C#.

CONTENTS

1. Overview and Key Difference
2. What is Overriding in C#
3. What is Overloading in C#
4. Similarities Between Overriding and Overloading in C#
5. Side by Side Comparison – Overriding vs Overloading in C# in Tabular Form
6. Summary

What is Overriding in C#?

There is one another important concept in OOP is inheritance. It is to use attributes and methods of the already existing class. It improves code reusability. The already existing class is the base class, and the new class is known as the derived class. In overriding of polymorphism, there should be a base class and a derived class. The binding of the overridden method call to the definition happens at runtime. An example is as follows.

Figure 01: C# Program with Overriding

According to the above program, the class Shape is the base class, and it contains the display method. Class Rectangle and Triangle are derived classes. These derived classes also have the same method display with their own implementations.

First, the reference variable obj is created. It points to the Shape object. So, the display method of Shape class will execute. Then, the reference variable points to the Rectangle object. So, the display method of Rectangle class will execute. Finally, the reference variable points to the Triangle object. So, the display method of Triangle class will execute. The base class display method is overridden by the display methods of the derived classes.

The method to run is decided at runtime. The Shape class is written with ‘virtual’ keyword. The Rectangle and Triangle classes are written with ‘override’ keyword. If these keywords are not used, the output will print the content of the display method of Shape class for all.

What is Overloading in C#?

In overloading, multiple methods have the same name but with different parameters. The parameters can be of different types. The methods can also have a different number of parameters. Method overloading occurs in the same class. The binding of overloaded methods to the definition happens at compile time.  Refer the below C# program.

Figure 02: C# Program with Overloading

According to the above program, class A has two methods with the same name called sum. They have different types of parameters. In the main program, an object of A is created. The sum (2,3)  will invoke the sum method with integers. The sum (5.1, 7.94) will invoke the sum method with double values. Both methods have the same name and a same number of parameters. But the parameter types are different. The required method is called accordingly. Overloading can also occur if the method names and parameter types are same but the number of parameters is different.

What is the Similarity Between Overriding and Overloading in C#?

What is the Difference Between Overriding and Overloading in C#?

Overriding vs Overloading in C#

Overriding in C# is to provide a specific implementation in a derived class method for a method already existing in the base class. Overloading in C# is to create multiple methods with the same name with different implementations.
 Parameters
In C# Overriding, the methods have the same name, same parameter types and a same number of parameters. In C# Overloading, the methods have the same name but a different number of parameters or a different type of parameters.
Occurrence
In C#, overriding occurs within the base class and the derived class. In C#, overloading occurs within the same class.
 Binding Time
The binding of the overridden method call to its definition happens at runtime. The binding of the overloaded method call to its definition happens at compile time.
Synonyms
Overriding is called as runtime polymorphism, dynamic polymorphism or late binding. Overloading is called as compile time polymorphism, static polymorphism or early binding.

Summary – Overriding vs Overloading in C#

Overriding and Overloading are two types of polymorphism. The difference between overriding and overloading in C# is that the binding of the overridden method call to its definition happens at runtime while the binding of the overloaded method call to its definition happens at compile time.

Reference:

1.Kumar, Mukesh. C# Corner. Available here
2.tutorialspoint.com. “C# Polymorphism.” TutorialsPointAvailable here
3.tutorialspoint.com. “C# Data Types.” TutorialsPoint , Available here