Introduction To Method Overloading and Method Overriding in C#

In this post, I would introduce the definition of method overloading and method overriding in C# (be similar in most programming languages such as Java and C++). Also, I would discuss the differences between these methods for you to find out which one is the most suitable in a specific situation. Sometimes, while you are building your programs, you inadvertently use them to save many lines of code. Therefore, I am here to give you a quick definition and help you identify them clearly.

Before I jump into their definition and examples, I have one thing to let you know that method overloading and method overriding are two types of polymorphism. Polymorphism is a magical ability of objects to transform into multiple appearances, which allows the derived classes to have their own implementation (or their own playground in which you must conform to their rules). Let’s get started!

1. Method Overloading

Method Overloading is a special programming technique that gives you permission to define one or more methods arming the same names with different types or numbers of arguments. In the first place, beginners think that it is impossible to create two methods, for example, having the same name, and it would return a bunch of errors. Yes. Some errors occur if you do not follow the rules of defining a method overloading. But no worries, I guarantee that it is easy and simple to use when it comes to seeing some below examples

There are two ways of overloading a method:

a. Define different numbers of parameters:

Oops. Just a quick definition here. You may see that sometimes I use “parameter” or “argument”, so what is the difference between them? “Parameter” is a hole defined in a method while “Argument” is an actual value passed to the method to make the method usable at runtime. Get back to examples to understand this way of overloading a method.

class Student
{
   void RetrieveStudentInfo(int studentID)
   {
      Console.WriteLine(“First Method”);
   }

   // Or
   void RetrieveStudentInfo(int studentID, int age)
   {
      Console.WriteLine(“Second Method”);
   } 
}

The two methods above are a valid case of method overloading (they are not perfect methods but a good example). As can be seen from the above lines of code, the “RetrieveStudentInfo” method has two definitions, but they have different numbers of parameters. Therefore, when you pass the “RetrieveStudentInfo” method one argument (studentID), you are using the first method. And when you pass the method two arguments (studentID and age), you are taking the second method. It’s awesome, isn’t it?

class Student
{
   static void Main(string[] args)
   {
      Student student = new Student();
      student.RetrieveStudentInfo(98247); 
      // Print “First Method”

      student.RetrieveStudentInfo(98247, 21); 
      // Print “Second Method”
   } 
}

b. Define different types of parameters:

class Student
{
   void RetrieveStudentInfo(String name, int age)
   {
      Console.WriteLine(“First Method”);
   }

   // Or
   void RetrieveStudentInfo(int studentID, int age)
   {
      Console.WriteLine(“Second Method”);
   } 
}

The two methods above are also a typical usage of method overloading (again, they are not perfect methods but a good example). The first method takes String as the first argument type and int as the second one. But, the below method accepts two arguments as int.

class Student
{
   static void Main(string[] args)
   {
      Student student = new Student();
      student.RetrieveStudentInfo(“Nam Nguyen”, 21); 
      // Print “First Method”

      student.RetrieveStudentInfo(98247, 21); 
      // Print “Second Method”

   } 
}

2. Method Overriding

Method overriding is a special technique in object-oriented programming that allows you to override child-class methods defined in the parent class earlier. Before overriding, the child classes have to inherit the parent class. A quick definition, inheritance is a “copying” ability of children classes that they can “steal” all public properties and methods of the parent class. Please look at the below example to get an understanding.

// Parent class
class Fighter
{
    // Overridden method
   void virtual AttackDamage()
   {
      Console.WriteLine(“The enemy loses 5 points”);
   }
}

// Child class
class Soldier : Fighter
{
    // Overriding method
   void override AttackDamage()
   {
      Console.WriteLine(“The enemy loses 10 points”);
   }
}

The “AttackDamage” method in “Soldier” class rewriting the one in “Fighter” is called overriding method, and the one in the parent class is the overridden method. Therefore, when you instantiate a “Soldier” object and call “AttackDamage” method, the console prints out “The enemy loses 10 points”

class Program
{
   static void Main(string[] args)
   {
      Fighter soldier = new Soldier();
      soldier.AttackDamage(); // The enemy loses 10 points
   } 
}

3. Differences Between Method Overloading And Method Overriding

Here is a list of main differences for beginners:

  1. Method overriding requires forcefully rewriting the method with the same parameters in terms of size and type. In meanwhile, method overloading is more flexible by allowing multiple types and different sizes which are its speciality and outstanding ability.

  2. Method overloading cannot perform outside the class that the first method is defined in, which means all overloading should be defined within the same class. Method overriding has a totally different story. Because method overriding is a representation of the relationship between classes, specifically parent and child classes, overriding methods must be re-defined in other classes.

  3. Method overloading executes at compile-time where all overloaded methods are formed their argument types, size and definition early before executing the program. Method overriding is as stubborn as a mule, and it prefers to define itself in runtime, which means during execution, it shows “information” as well as actual “characteristics”.

4. Conclusion

This post is a quick introduction to Method Overloading and Method Overriding in C# as well as object-oriented programming. Also, this is the first time I’ve written a tech post. I want to force myself to review my own knowledge about programming concepts and to see how solid I know about OOp as well as how well I could explain it to my reader in my own words.

Therefore, if you find any mistake in my post or give me feedback, please comment below. I am welcome to your responses.

Reference: Difference between method Overloading and Overriding in java