What is Method Overriding in C#?
π‘ Concept: Method Overriding
Method overriding allows a derived class to provide its specific implementation for a method that is already defined in its base class using the virtual
and override
keywords.
π Quick Intro
Method overriding in C# supports runtime polymorphism. It lets a child class override the behavior of a base class method marked with virtual
using the override
keyword.
π§ Analogy
Think of a base class method like a blueprint. A derived class can customize the blueprint to build something different while still following the original structure. Thatβs overriding in actionβchanging how something behaves without changing the definition.
π§ Technical Explanation
- Only
virtual
methods can be overridden usingoverride
in derived classes. - The base method must be marked as
virtual
,abstract
, oroverride
. - Overriding enables dynamic dispatch or runtime polymorphism.
- Signatures must match between base and overridden method.
- Optional:
base.MethodName()
can be used to call the base implementation.
π― Use Cases
- Customizing behavior in derived classes while preserving a consistent interface.
- Implementing polymorphic behavior in frameworks or APIs.
- Allowing extensibility in large-scale object-oriented systems.
- Using
ToString()
,Equals()
, or other virtual base class methods.
π» Code Example
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 pet = new Dog();
pet.Speak(); // Output: Dog barks

β Interview Q&A
Q1: What is method overriding?
A: It allows a derived class to redefine a base class method marked as virtual.
Q2: What keyword is used to override a method?
A: The override
keyword.
Q3: Can we override a non-virtual method?
A: No, only virtual or abstract methods can be overridden.
Q4: What is runtime polymorphism?
A: The method call is resolved at runtime based on the objectβs type.
Q5: Can you override static methods?
A: No, static methods cannot be overridden.
Q6: Can constructors be overridden?
A: No, constructors are not inherited and cannot be overridden.
Q7: Can access modifiers differ between base and override methods?
A: Yes, but the overriding method cannot have a more restrictive access level.
Q8: Can a method be both overloaded and overridden?
A: Yes, as long as their signatures differ.
Q9: What is the use of base.Method()?
A: To call the base class implementation from the derived class.
Q10: Can override be used with abstract methods?
A: Yes, abstract methods must be overridden in derived classes.
π MCQs
Q1. Which keyword is used to override a method?
- virtual
- new
- override
- base
Q2. What enables method overriding?
- Static method
- Virtual method in base class
- Private method
- Constructor
Q3. Can you override static methods?
- Yes
- No
- Only if sealed
- If protected
Q4. What is polymorphism?
- Inheritance
- Encapsulation
- Polymorphism
- Abstraction
Q5. Is overriding decided at runtime?
- No
- Yes
- Compile-time
- Depends on CLR
Q6. What happens if you forget override keyword?
- Compiles as override
- Runtime error
- Hides base method (not overrides)
- Calls constructor
Q7. Which access modifier allows overriding?
- Private
- Internal
- Protected internal
- Public or protected
Q8. Can you override a sealed method?
- Yes
- No
- Only in base
- Only in derived
Q9. Can you override an abstract method?
- No
- Yes
- Only if sealed
- Only if internal
Q10. How do you call the base method?
- super()
- base.MethodName()
- this.Method()
- None
π‘ Bonus Insight
Method overriding brings flexibility and reusability to object-oriented design. Proper use of polymorphism allows extending applications without rewriting existing logic, making your codebase more scalable.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!