C# Polymorphism: Method Overriding Tutorial
Quick Answer
Method overriding in C# allows a derived class to provide a specific implementation of a method already defined in its base class. It enables runtime polymorphism, letting the program decide which method to invoke based on the object type at runtime.
Learning Objectives
- Explain the purpose of Method Overriding in a practical learning context.
- Identify the main ideas, terms, and decisions involved in Method Overriding.
- Apply Method Overriding in a simple real-world scenario or practice task.
Introduction to Method Overriding in C#
Polymorphism is a core concept in object-oriented programming that allows objects to be treated as instances of their parent class rather than their actual class.
Method overriding is a key technique to achieve polymorphism in C#. It lets a derived class change the behavior of a method defined in its base class.
This tutorial explains method overriding with simple examples and practical guidance.
Polymorphism allows methods to do different things based on the object calling them.
Understanding Method Overriding
Method overriding occurs when a derived class provides a new implementation for a method declared in its base class.
The base class method must be marked with the 'virtual' keyword to allow overriding.
The derived class method uses the 'override' keyword to indicate it replaces the base class method.
- Enables runtime polymorphism.
- Supports dynamic method dispatch.
- Allows extending or modifying base class behavior.
Virtual and Override Keywords
The 'virtual' keyword in the base class signals that a method can be overridden.
The 'override' keyword in the derived class indicates the method replaces the base implementation.
- Base class method: virtual void Display() { ... }
- Derived class method: override void Display() { ... }
Rules for Method Overriding
The overriding method must have the same signature as the base method.
Access modifiers must be compatible (usually public or protected).
The base method cannot be static or private.
- Signature includes method name, return type, and parameters.
Example of Method Overriding in C#
Here is a simple example demonstrating method overriding in C#.
Practical Example
The base class Animal defines a virtual method Speak. The derived class Dog overrides Speak to provide its own implementation. At runtime, the correct method is called depending on the object type.
Examples
using System;
class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Dog barks");
}
}
class Program
{
static void Main()
{
Animal myAnimal = new Animal();
Animal myDog = new Dog();
myAnimal.Speak(); // Output: Animal speaks
myDog.Speak(); // Output: Dog barks
}
}The base class Animal defines a virtual method Speak. The derived class Dog overrides Speak to provide its own implementation. At runtime, the correct method is called depending on the object type.
Best Practices
- Always mark base class methods you expect to override with 'virtual'.
- Use 'override' keyword explicitly in derived classes to avoid accidental hiding.
- Call base method using 'base.MethodName()' if you want to extend base behavior.
- Keep method signatures consistent between base and derived classes.
- Avoid overriding methods unnecessarily to keep code maintainable.
Common Mistakes
- Forgetting to mark the base method as 'virtual'.
- Using 'new' keyword instead of 'override', which hides rather than overrides.
- Changing method signature in derived class causing hiding instead of overriding.
- Trying to override static or private methods.
- Not calling base method when needed, leading to incomplete behavior.
Hands-on Exercise
Implement Method Overriding
Create a base class Shape with a virtual method Draw. Derive classes Circle and Rectangle that override Draw with their own implementations. Instantiate each and call Draw to see polymorphism in action.
Expected output: Circle and Rectangle print their specific draw messages when Draw is called.
Hint: Use 'virtual' in Shape and 'override' in derived classes.
Interview Questions
What is method overriding in C#?
InterviewMethod overriding allows a derived class to provide a specific implementation of a method already defined in its base class, enabling runtime polymorphism.
What keywords are used for method overriding in C#?
InterviewThe base class method is marked with 'virtual', and the derived class method uses 'override' to provide a new implementation.
Can you override a static method in C#?
InterviewNo, static methods cannot be overridden because they belong to the class, not an instance.
MCQ Quiz
1. What is the best first step when learning Method Overriding?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce Method Overriding?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. Method overriding in C# allows a derived class to provide a specific implementation of a method already defined in its base class.
B. Method Overriding never needs examples
C. Method Overriding is unrelated to practical work
D. Method Overriding should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- Method overriding in C# allows a derived class to provide a specific implementation of a method already defined in its base class.
- It enables runtime polymorphism, letting the program decide which method to invoke based on the object type at runtime.
- Polymorphism is a core concept in object-oriented programming that allows objects to be treated as instances of their parent class rather than their actual class.
- Method overriding is a key technique to achieve polymorphism in C#.
- It lets a derived class change the behavior of a method defined in its base class.
Summary
Method overriding is a fundamental feature in C# that enables polymorphism by allowing derived classes to customize base class behavior.
It requires the base method to be virtual and the derived method to use override.
Proper use of method overriding leads to flexible and maintainable object-oriented designs.
Frequently Asked Questions
What is the difference between method overriding and method hiding?
Method overriding replaces the base class method at runtime using 'virtual' and 'override'. Method hiding uses the 'new' keyword and hides the base method but does not support polymorphism.
Can constructors be overridden in C#?
No, constructors cannot be overridden because they are not inherited by derived classes.
What happens if you forget to use 'override' in the derived class?
If you omit 'override' but define a method with the same signature, it hides the base method instead of overriding it, which can cause unexpected behavior.
What is Method Overriding?
Method overriding in C# allows a derived class to provide a specific implementation of a method already defined in its base class.
Why is Method Overriding important?
It enables runtime polymorphism, letting the program decide which method to invoke based on the object type at runtime.
How should I practice Method Overriding?
Polymorphism is a core concept in object-oriented programming that allows objects to be treated as instances of their parent class rather than their actual class.

