C# Interview Question #18
What is the difference between method overloading and method overriding?
OOP & Core C# Concepts Junior Beginner
Quick Interview Answer
Method overloading means defining multiple methods with the same name but different parameter lists. The compiler selects the appropriate overload based on the call.
Detailed Explanation
Method overloading means defining multiple methods with the same name but different parameter lists. The compiler selects the appropriate overload based on the call.
Method overriding occurs when a derived class replaces the implementation of an inherited virtual or abstract member. Runtime dispatch determines which overridden implementation is executed.
In short:
Overloading = same method name, different parameter signatures.
Overriding = derived class provides different behavior for an inherited virtual/abstract member.
Code Example
public int Add(int a, int b) => a + b;
public int Add(int a, int b, int c) => a + b + c;
// Overriding:
public class Animal
{
public virtual void Speak() => Console.WriteLine("Animal");
}
public class Cat : Animal
{
public override void Speak() => Console.WriteLine("Cat");
}