Difference Between Overriding and Overloading Methods in C#

Introduction

Overriding and overloading methods are two ways to improve the readability and flexibility of code in object-oriented programming. They are utilized in various circumstances and for various reasons, though. Here in this article, we are going to explain to you the difference between Overriding and Overloading methods. So, let's dive in.

Method Overriding in C#

In C#, a derived class can give a particular implementation for a method that is already defined in its base class by using a feature called method overriding. In other words, the implementation of a method that a derived class inherits from its base class is replaced or expanded upon by the derived class.

using System;

public class Shape
{
    // Base class method marked as virtual
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a shape");
    }
}

public class Circle : Shape
{
    // Derived class method using the override keyword
    public override void Draw()
    {
        Console.WriteLine("Drawing a circle");
    }
}

class Program
{
    static void Main()
    {
        // Creating an instance of the derived class
        Circle circle = new Circle();

        // Calling the overridden method
        circle.Draw();  // Output: Drawing a circle

        // Using polymorphism with a base class reference
        Shape shape = new Circle();
        shape.Draw();  // Output: Drawing a circle
    }
}

A base class Shape with a virtualized method called Draw is shown in this example. Drawing a circle is done by overriding the Draw method with an implementation of its own by the Circle class, which is derived from Shape.

In the Main method, we instantiate the Circle class and invoke its Draw function. The output "Drawing a circle" indicates that the Circle class's overridden method has been used.

This example demonstrates how method overriding builds code extensibility and flexibility by enabling a derived class to offer a particular implementation for a method defined in its base class.

Method Overloading in C#

In C#, the ability to declare numerous methods with the same name but a distinct list of parameters within the same class is known as method overloading. The quantity or kind of parameters used in these techniques can vary. The method signature, which consists of the method name, type, and number of parameters, is how the compiler makes the distinction between them.

public class Calculator
{
    // Method with two integer parameters
    public int Add(int x, int y)
    {
        return x + y;
    }

    // Method with three integer parameters
    public int Add(int x, int y, int z)
    {
        return x + y + z;
    }

    // Method with two double parameters
    public double Add(double x, double y)
    {
        return x + y;
    }
}

In this illustration, the Calculator class contains several Add methods, each with a unique list of parameters. Two integers are required for the first Add method, three integers are required for the second, and two doubles are required for the third. Based on the quantity and kinds of parameters supplied during the method invocation, the compiler chooses which method to call.

Important details regarding C# method overloading

  • Each method needs to belong to the same class.
  • The method names need to match.
  • There must be variations in the quantity and kind of parameters amongst the parameter lists.

Conclusion

To sum up, both method overloading and overriding are crucial ideas in object-oriented programming, and they each have a specific function. In a class hierarchy, method overriding allows for polymorphism and specialization, while method overloading improves code readability and flexibility within a class. Writing well-organized, maintainable code is facilitated by knowing when and how to use each technique.

Feel free to drop a comment in the comment section if you have any queries.

Thanks

Mukesh Nailwal


Similar Articles