In .NET, Function Overloading and Function Overriding are both forms of Polymorphism, but they work very differently.
🔹 Function Overloading vs Function Overriding
| Feature | Function Overloading | Function Overriding |
|---|---|---|
| Definition | Same method name with different parameters | Child class redefines parent class method |
| Occurs in | Same class | Base class + Derived class |
| Parameters | Must be different | Must be same |
| Return type | Can be different (but not only difference) | Must be same (or covariant) |
| Keywords | No special keyword needed | virtual, override, abstract |
| Polymorphism Type | Compile-time polymorphism | Runtime polymorphism |
| Inheritance Required | ❌ No | ✅ Yes |
🔹 Function Overloading Example
Same method name, different parameters.
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
public double Add(double a, double b)
{
return a + b;
}
}
Usage
Calculator calc = new Calculator();
calc.Add(2,3); // Calls first method
calc.Add(2,3,4); // Calls second method
calc.Add(2.5,3.5); // Calls third method
👉 Same method name Add, different parameters
👉 This is Function Overloading
🔹 Function Overriding Example
Parent class method overridden in child class.
public class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal speaks");
}
}
public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Dog barks");
}
}
Usage
Animal animal = new Dog();
animal.Speak();
Output
Dog barks
👉 Parent method replaced by child method
👉 This is Function Overriding
🔹 Keywords Used in Overriding
| Keyword | Purpose |
|---|---|
virtual |
Allows method to be overridden |
override |
Overrides base method |
abstract |
Must be overridden |
sealed |
Prevents further overriding |
🔹 Abstract Example
public abstract class Animal
{
public abstract void Speak();
}
public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Dog barks");
}
}
🔹 Interview One-Line Answer
Function Overloading → Same method name, different parameters (Compile time)
Function Overriding → Child class modifies parent class method (Runtime)
🔹 Real-World Example
Overloading
Login(username, password)
Login(googleToken)
Login(mobileOtp)
Overriding
Employee.CalculateSalary()
Manager.CalculateSalary() // Different logic
Developer.CalculateSalary() // Different logic
🔹 Interview Trick Question ⚡
Can we override static method?
❌ No — Static methods cannot be overridden
🔹 Another Trick Question
Can we overload static method?
✅ Yes — Static methods can be overloaded
