Run-Time Polymorphism in C#
Quick Answer
Run-time polymorphism in C# allows methods to be overridden in derived classes and resolved dynamically during program execution. It enables flexible and extensible code by using virtual methods and method overriding, supporting dynamic behavior based on object types at runtime.
Learning Objectives
- Explain the purpose of Run-Time Polymorphism in a practical learning context.
- Identify the main ideas, terms, and decisions involved in Run-Time Polymorphism.
- Apply Run-Time Polymorphism in a simple real-world scenario or practice task.
Introduction to Run-Time Polymorphism
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.
Run-time polymorphism, also known as dynamic polymorphism, occurs when a call to an overridden method is resolved at runtime rather than compile time.
This tutorial explains how run-time polymorphism works in C#, why it is useful, and how to implement it using virtual and override keywords.
Polymorphism allows the same interface to be used for different underlying forms (data types).
Understanding Run-Time Polymorphism
Run-time polymorphism enables a program to decide at runtime which method implementation to invoke based on the actual object type.
This is achieved in C# through method overriding, where a derived class provides a specific implementation of a method declared in its base class.
- Base class declares a method as virtual.
- Derived class overrides the virtual method using the override keyword.
- Method calls on base class references invoke the derived class's method if the object is of the derived type.
Virtual and Override Keywords
The virtual keyword in the base class marks a method as overridable.
The override keyword in the derived class provides a new implementation for the virtual method.
- virtual: Allows a method to be overridden in derived classes.
- override: Provides the new implementation in the derived class.
How Dynamic Binding Works
When a virtual method is called through a base class reference, the runtime determines the actual object type and invokes the appropriate method implementation.
This mechanism is called dynamic binding or late binding.
Example of Run-Time Polymorphism in C#
Let's look at a simple example demonstrating run-time polymorphism using virtual and override methods.
Practical Example
In this example, the Speak method is declared virtual in the Animal base class and overridden in Dog and Cat derived classes. The method call on the Animal reference resolves at runtime to the appropriate derived class implementation.
Examples
using System;
class Animal
{
public virtual void Speak()
{
Console.WriteLine("The animal makes a sound.");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("The dog barks.");
}
}
class Cat : Animal
{
public override void Speak()
{
Console.WriteLine("The cat meows.");
}
}
class Program
{
static void Main()
{
Animal myAnimal = new Dog();
myAnimal.Speak(); // Output: The dog barks.
myAnimal = new Cat();
myAnimal.Speak(); // Output: The cat meows.
}
}In this example, the Speak method is declared virtual in the Animal base class and overridden in Dog and Cat derived classes. The method call on the Animal reference resolves at runtime to the appropriate derived class implementation.
Best Practices
- Always declare base class methods as virtual if they are intended to be overridden.
- Use the override keyword in derived classes to provide specific implementations.
- Avoid calling virtual methods from constructors to prevent unexpected behavior.
- Keep overridden methods consistent with the base method's contract.
- Use polymorphism to write flexible and maintainable code.
Common Mistakes
- Forgetting to mark the base class method as virtual.
- Using new keyword instead of override, which hides the base method instead of overriding.
- Calling overridden methods from base class constructors.
- Not understanding that method calls are resolved at runtime, leading to unexpected results.
Hands-on Exercise
Implement Run-Time Polymorphism
Create a base class Shape with a virtual method Draw. Derive classes Circle and Rectangle that override Draw with their specific implementations. Instantiate objects of each and call Draw through a base class reference.
Expected output: Calling Draw on a Shape reference should invoke the correct derived class method, printing shape-specific messages.
Hint: Use virtual in the base class and override in derived classes.
Interview Questions
What is run-time polymorphism in C#?
InterviewRun-time polymorphism in C# is the ability of a method call to resolve to different method implementations at runtime based on the actual object type, achieved through virtual methods and method overriding.
How do you implement run-time polymorphism in C#?
InterviewBy declaring a method as virtual in the base class and overriding it in derived classes using the override keyword.
What is the difference between method overriding and method overloading?
InterviewMethod overriding replaces a base class method in a derived class with the same signature and is resolved at runtime, while method overloading defines multiple methods with the same name but different parameters and is resolved at compile time.
MCQ Quiz
1. What is the best first step when learning Run-Time Polymorphism?
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 Run-Time Polymorphism?
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. Run-time polymorphism in C# allows methods to be overridden in derived classes and resolved dynamically during program execution.
B. Run-Time Polymorphism never needs examples
C. Run-Time Polymorphism is unrelated to practical work
D. Run-Time Polymorphism should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- Run-time polymorphism in C# allows methods to be overridden in derived classes and resolved dynamically during program execution.
- It enables flexible and extensible code by using virtual methods and method overriding, supporting dynamic behavior based on object types 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.
- Run-time polymorphism, also known as dynamic polymorphism, occurs when a call to an overridden method is resolved at runtime rather than compile time.
- This tutorial explains how run-time polymorphism works in C#, why it is useful, and how to implement it using virtual and override keywords.
Summary
Run-time polymorphism in C# allows methods to be dynamically bound to their implementations during program execution.
It is implemented using virtual methods in base classes and overriding them in derived classes.
This feature enables flexible, extensible, and maintainable object-oriented code.
Frequently Asked Questions
Can a non-virtual method be overridden in C#?
No, only methods marked as virtual, abstract, or override can be overridden in derived classes.
What happens if you use the new keyword instead of override?
Using new hides the base class method instead of overriding it, which means method calls are resolved based on the reference type, not the object type.
Is run-time polymorphism the same as method overloading?
No, run-time polymorphism is method overriding resolved at runtime, while method overloading is compile-time polymorphism with multiple methods having the same name but different signatures.
What is Run-Time Polymorphism?
Run-time polymorphism in C# allows methods to be overridden in derived classes and resolved dynamically during program execution.
Why is Run-Time Polymorphism important?
It enables flexible and extensible code by using virtual methods and method overriding, supporting dynamic behavior based on object types at runtime.
How should I practice Run-Time Polymorphism?
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.

