Method overloading, Method overriding in C#

In this article, we will discuss Method overloading and Method overriding in C#, These two are widely used and reusable object-oriented programming capabilities of the C# language which will be covered in this article. Method overloading is a technique for defining several methods under a single name for the class. Therefore, we can declare many methods with the same name in a class. However, you must keep in mind that the parameters for each of those techniques should differ (different in terms of the number, type, and order of the parameters).

Whereas, Method overriding and function overriding are both used equally. A method for implementing polymorphism (also known as run-time polymorphism or dynamic polymorphism) in C# is method overriding. In case of Method overriding If the Super Class or Parent Class method logic is not fulfilling the Sub Class or Child Class business requirements, then the Sub Class or Child Class needs to override the superclass method with the required business logic. Usually, in most real-time applications, the Parent Class methods are implemented with generic logic which is common for all the next-level sub-classes.


Method overloading in C#:


The most popular method for creating polymorphism is method overloading (also known as compile-time polymorphism or static polymorphism). It is the capacity to reinterpret a function in many contexts. Function overloading can be used by defining two or more functions in a class that has the same name. With different method signatures, C# may distinguish between different methods. Within the same class, several methods may have the same name but different parameter lists (i.e., the number, order, and data types of the parameters). In simple words, we can say that the Method Overloading in C# allows a class to have multiple methods with the same name but with a different signature. The functions or methods can be overloaded based on the number, type (int, float, etc), order, and kind (Value, Ref, or Out) of parameters.

Method overloading has the benefit of making code easier to read and maintain. Although it is possible to have methods with the same name that carry out completely distinct tasks, it is recommended that overloaded methods share common performance characteristics.

Given below is an example of method overloading to understand the concepts.

Example:


MethodOverload.cs


 using System;

 using System.Text;

 namespace PracticeConsoleApp1

 {

  /*  internal class MethodOverLoad

    {

                            //-- use of normal method class object--

           public int Add(int a, int b)

           {

               return a + b;

           }

           public int Add(int a, int b, int c)

           {

               return a + b + c;

           }*/

        //-- use of static class object--

    static class MethodOverload {

        public static int Add(int a, int b)

        {

            return a + b;

        }

        public static int Add(int a, int b, int c)

        {

            return a + b + c;

        }

    }

 }



Below is another .cs file example For all the implementations, here is the main .cs file.

Program.cs

 using System;

 using System.Text;

 namespace PracticeConsoleApp1

 {

    public delegate int MyDelegate(int num);

    internal class Program

    {

        static void Main(string[] args)

        {

     


 Console.WriteLine("addition of two number is : " + MethodOverload.Add(15, 20));


 Console.WriteLine("addition of three number is : " + MethodOverload.Add(20, 30, 15));

            Console.ReadLine();

        }

   }

 }




Output:


 addition
of two number is : 35


 addition
of three number is : 65



Method overriding in C#:


Method overriding and function overriding are both used equally. A method for implementing polymorphism (also known as run-time polymorphism or dynamic polymorphism) in C# is method overriding. With the use of the method overriding technique, the derived class can call methods from the base class of another class. Method overriding is the process of adding a method with the same signature to a derived class as a base class method. To perform the method overriding in C#, you need to use virtual keyword with the base class method and the override keyword with the derived class method.

Overriding, to put it simply, is a feature that enables a subclass or child class to offer a particular implementation of a method that is already supplied by one of its super-classes or parent classes. The term "override" refers to a method in a subclass that replaces a method in a superclass when both methods share the same name, same parameters, same signature, and same return type (or sub-type). The method which is overridden by the override declaration is called the overridden base method. The virtual, overridden, or abstract base method can all be overridden. This base class's override method is inherited from a new implementation that is invoked.

Given Below is an example of method overriding to get the concepts.

Example:


MethodOverride.cs

 

 using System;

 using System.Text;

 namespace PracticeConsoleApp1

 {                                // --- Mehtod OverRide ---

    public class MethodOverRide

    {

        public virtual void eat()  // --- use of virtual keyword ---

        {

            Console.WriteLine("eating");

        }

    }

    public class MethodOverRideSon : MethodOverRide

    {

        public override void eat()  // --- use of override keyword ---

        {

            Console.WriteLine("eating bread");

        }

    }

 }



Below is another .cs file example For all the implementations, here is the main .cs file.

Program.cs

 using System;

 using System.Text;

 namespace PracticeConsoleApp1

 {

    internal class Program

    {

        static void Main(string[] args)

        {

     

  MethodOverRide methodOver = new MethodOverRideSon();

             methodOver.eat();

             Console.ReadLine();

        }

   }

 }



Output:


 eating
bread





Difference between Overloading and Overriding:



  • Overloading is called compile time polymorphism, static polymorphism, or early binding. Whereas Overriding is called as runtime polymorphism, dynamic polymorphism, or late binding.


  • Method Overloading has no separate keywords that are used to implement function overloading. Whereas Method Overriding uses the virtual keyword for the base class function and override keyword in the derived class function to implement function overriding.


  • Overloading in C# is to create multiple methods with the same name with different implementations. Whereas in C#, overriding refers to giving an existing method in the base class a particular implementation in a derived class method.


  • In C# Overloading, the methods have the same name but a different number of parameters or different types of parameters. Whereas In C# Overriding, the methods have the same name, the same parameter types, and the same number of parameters.


  • In C#, overloading occurs within the same class. Whereas Overriding takes place in both the base class and the derived class in C#.


  • The binding of the overloaded method call to its definition happens at compile time. Whereas The binding of the overridden method call to its definition happens at runtime.


  • Private and final methods can be overloaded. Whereas Private and final methods can’t be overridden.


  • The argument list should be different while doing method overloading. Whereas the Argument list should be the same in method overriding.


  • Method Overloading has Poor Performance due to compile time polymorphism. Whereas Method Overriding gives better performance. The reason behind this is that the binding of overridden methods is being done at runtime.


  • Method Overloading is an approach to defining multiple methods with the same name but with a different signature means by changing the number, type, and order of parameters. Whereas Method Overriding is an approach to defining multiple methods with the same name and with the same signature means the same number, type, and order of parameters.




Conclusion:


In this article, we discussed the concept of method overriding, the necessity for method overriding, practical applications of the overriding principle, how to implement it, the use of the keywords "virtual" and "override," and an example of method overriding using code. In summary, overriding is a valuable property of polymorphism that allows us to modify the behavior of a base class method in a derived class.

I hope you found this post to be interesting and helpful. Please add your opinions and suggestions to the discussion below.


Share This Post

Linkedin
Fb Share
Twitter Share
Reddit Share

Support Me

Buy Me A Coffee