Method Overloading and Method Overriding

Method overloading and method overriding are two important concepts in object-oriented programming, particularly in .NET. They both involve defining multiple methods with the same name but with different parameters or behaviors. However, there are significant differences between method overloading and method overriding. Let's examine each concept in detail with examples:

Method Overloading

Method overloading allows you to define multiple methods with the same name but with different parameters within the same class or in a derived class. The compiler differentiates between these methods based on their parameter types, number, or order.

Example | Method overloading:
public class Calculator { public int Add(int a, int b) { return a + b; } public double Add(double a, double b) { return a + b; } }
Method Overloading versus Overriding C# VB.Net Interview questions and answers

In the above example, the Calculator class has two Add methods: one that accepts two integers and another that accepts two doubles. The methods have the same name but different parameter types (integers vs. doubles). This allows the user to call the appropriate method based on the types of arguments they provide.

Calculator calculator = new Calculator(); int sum1 = calculator.Add(2, 3); // Calls the Add(int, int) method double sum2 = calculator.Add(2.5, 3.5); // Calls the Add(double, double) method

Method overloading provides flexibility and allows you to write code that can handle different types of data while using the same method name.

Method Overriding

Method overriding is a concept that occurs in inheritance, where a derived class provides a different implementation for a method that is already defined in its base class. The method in the derived class must have the same name, return type, and parameters as the method in the base class.

Example | Method overriding:
public class Shape { public virtual void Draw() { Console.WriteLine("Drawing a shape."); } } public class Circle : Shape { public override void Draw() { Console.WriteLine("Drawing a circle."); } }
What are method overloading and method overriding? C# VB.Net

In the above example, the Shape class has a virtual method called Draw(). The Circle class inherits from Shape and overrides the Draw() method with its own implementation. The override keyword is used to indicate that the method is intended to override a base class method.

Shape shape = new Circle(); shape.Draw(); // Calls the Draw() method in the Circle class

In this case, when the Draw() method is called on a Shape object that references a Circle instance, the overridden Draw() method in the Circle class is executed. This is known as runtime polymorphism or dynamic method dispatch.

Method overriding allows you to provide specialized behavior in derived classes while using the common interface defined in the base class.

Conclusion

Method overloading involves defining multiple methods with the same name but different parameters within the same class or in a derived class. Method overriding occurs in inheritance and allows a derived class to provide a different implementation for a method that is already defined in its base class.