Lesson 15 — Method Overriding in C# (Runtime Polymorphism Explained)

Method Overriding is a core part of runtime polymorphism in C#.
It allows a child class to replace or modify the behavior of a parent class method.

In simple words:

Method Overriding = Same method name + same parameters, but different behavior in child class.

This enables flexible and dynamic behavior in object-oriented programming.


🌟 What is Method Overriding?

Overriding occurs when:

  • A base class has a method marked with virtual
  • A derived class provides its own version using override

The method that gets executed depends on the object type at runtime, not the reference type.

This is why it’s called runtime polymorphism.


🧱 Example — Method Overriding in Action

Base Class:

public class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("Animal makes a sound");
    }
}

Derived Class:

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Dog barks");
    }
}

Usage:

Animal a = new Dog();
a.MakeSound();  // Output: Dog barks

✔ Same method name
✔ Same signature
✔ Different behavior
✔ Decided at runtime


🟣 Why Use Method Overriding?

✔ To change inherited behavior
✔ To provide specific implementation in child class
✔ To support runtime polymorphism
✔ To use shared interfaces with custom logic
✔ To extend base class functionality

🔑 Important Keywords in Overriding


✔ virtual

Marks a method in the base class as overridable.

public virtual void Print() { }


✔ override

Used in derived class to replace the base class method.

public override void Print() { }


✔ base

Used to call the parent method inside the overridden method.

public override void Print()
{
    base.Print();
    Console.WriteLine("Child printing");
}


🔄 Basic Overriding Flow

Base Class Method → marked with virtual
Derived Class Method → marked with override
Call made using base class reference → child class version runs


🧠 Real-World Analogy

Action: MakeSound()

Different animals → different sounds:

AnimalBehavior
DogBark
CatMeow
CowMoo

The base class defines the action.
Child classes define their version.


🧱 Example — Shape Drawing (Common Interview Example)

Base Class:

public class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a shape...");
    }
}

Derived Class:

public class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a circle...");
    }
}

Usage:

Shape shape = new Circle();
shape.Draw();


🌈 Overriding + base Keyword

You can extend the base class method rather than completely replacing it.

public override void Draw()
{
    base.Draw();  
    Console.WriteLine("Now drawing details of circle...");
}


🟠 Method Hiding (new Keyword) — NOT Overriding

If you use new, it hides the base version, it does not override it.

public class Animal
{
    public void Eat() { Console.WriteLine("Animal eats"); }
}

public class Dog : Animal
{
    public new void Eat() { Console.WriteLine("Dog eats"); }
}

Difference:

  • override → polymorphism
  • new → hiding

🟡 Rules of Method Overriding

✔ Must use virtual in base class
✔ Must use override in derived class
✔ Method signature must be same
✔ Cannot override static methods
✔ Access modifier must be same or more accessible
✔ Overridden method must have same return type


🔄 Overloading vs Overriding (Quick Comparison)

FeatureOverloadingOverriding
TypeCompile-time polymorphismRuntime polymorphism
ParametersMust differMust match
Same class?YesNo (needs inheritance)
KeywordsNonevirtual, override
PurposeSame action, different inputsSame action, different implementation

🧰 Practical Example — Payment System

Base Class:

public class Payment
{
    public virtual void Pay(double amount)
    {
        Console.WriteLine($"Processing payment of {amount}");
    }
}

Derived Class:

public class UpiPayment : Payment
{
    public override void Pay(double amount)
    {
        Console.WriteLine($"Paid {amount} via UPI");
    }
}

Usage:

Payment p = new UpiPayment();
p.Pay(500);


📝 Mini Exercise

Create:

1️⃣ Base class → Vehicle with virtual method Start()
2️⃣ Derived classes → Car, Bike
3️⃣ Override Start() in each
4️⃣ Call using:

Vehicle v = new Car();
v.Start();

Predict output.


🔍 FAQs

Q1: Can constructors be overridden?

No. Constructors cannot be overridden.

Q2: Can static methods be overridden?

No. Static methods belong to the class, not objects.

Q3: Is overriding necessary for polymorphism?

Yes. Runtime polymorphism requires virtual and override.

Q4: Can we override private methods?

No. Private methods are not inherited.


🎉 Conclusion

You now understand:

✔ What method overriding is
✔ Why it is used
✔ How runtime polymorphism works
✔ virtual, override, and base keywords
✔ Difference between overriding & hiding
✔ Real-world examples

Method overriding is a powerful OOP feature that enables flexible and dynamic behavior in your C# applications.


Next lesson: