What if your program could choose the right action all by itself, without you writing endless checks?
Why Runtime polymorphism execution in C Sharp (C#)? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have different types of animals, and you want each to make its own sound. If you write separate code for each animal type everywhere, your program becomes huge and hard to manage.
Manually checking each animal type and calling its sound method means lots of repeated code and mistakes. Every time you add a new animal, you must change many places, which is slow and error-prone.
Runtime polymorphism lets you write one simple code that calls the sound method, and the right animal sound happens automatically. This keeps your code clean, easy to add new animals, and less buggy.
if (animal is Dog) { ((Dog)animal).Bark(); } else if (animal is Cat) { ((Cat)animal).Meow(); }
animal.MakeSound(); // Calls the right sound at runtime
It enables writing flexible programs where objects decide their behavior during execution, making your code easier to extend and maintain.
Think of a music app playing different instruments. With runtime polymorphism, the app just calls PlaySound() on any instrument, and the correct sound plays without extra checks.
Manual type checks make code complex and fragile.
Runtime polymorphism calls the right method automatically at runtime.
This leads to cleaner, easier-to-maintain, and extendable code.
Practice
Solution
Step 1: Understand runtime polymorphism concept
Runtime polymorphism allows a base class reference to call methods overridden in derived classes.Step 2: Identify correct behavior
This means the actual method called depends on the object's real type, not the reference type.Final Answer:
Call derived class methods through a base class reference -> Option CQuick Check:
Runtime polymorphism = base ref calls derived method [OK]
- Confusing polymorphism with changing variable types
- Thinking static methods are polymorphic
- Believing polymorphism creates multiple instances
Solution
Step 1: Identify keyword to enable overriding
The base class method must be marked withvirtualto allow overriding.Step 2: Understand roles of keywords
overrideis used in derived classes,virtualin base classes.Final Answer:
virtual -> Option AQuick Check:
Base method uses virtual to allow override [OK]
- Using override in base class instead of virtual
- Confusing new keyword with override
- Thinking abstract is required for all overrides
class Animal {
public virtual string Speak() => "Animal sound";
}
class Dog : Animal {
public override string Speak() => "Bark";
}
class Cat : Animal {
public override string Speak() => "Meow";
}
Animal a = new Dog();
Console.WriteLine(a.Speak());Solution
Step 1: Identify object type and method called
Variableais of typeAnimalbut references aDogobject.Step 2: Apply runtime polymorphism
SinceSpeakis virtual and overridden inDog, theDogversion runs, printing "Bark".Final Answer:
Bark -> Option BQuick Check:
Base ref calls Dog's Speak() = Bark [OK]
- Expecting base class method output
- Confusing object type with reference type
- Thinking compile error due to override
class Base {
public override void Show() {
Console.WriteLine("Base Show");
}
}
class Derived : Base {
public override void Show() {
Console.WriteLine("Derived Show");
}
}Solution
Step 1: Check base class method declaration
Base class method incorrectly usesoverrideinstead ofvirtual.Step 2: Understand override rules
Only derived classes useoverride; base class must usevirtualto allow overriding.Final Answer:
Base class method must be virtual, not override -> Option AQuick Check:
Base method needs virtual keyword [OK]
- Using override in base class method
- Thinking override is allowed without virtual
- Ignoring method signature correctness
class Vehicle {
public virtual string Describe() => "Vehicle";
}
class Car : Vehicle {
public override string Describe() => "Car";
}
class SportsCar : Car {
public override string Describe() => "SportsCar";
}
Vehicle v = new SportsCar();
Car c = new SportsCar();
SportsCar s = new SportsCar();
Console.WriteLine(v.Describe());
Console.WriteLine(c.Describe());
Console.WriteLine(s.Describe());
What is the output?Solution
Step 1: Identify actual object type for all references
All variablesv,c, andsreference aSportsCarobject.Step 2: Apply runtime polymorphism for Describe()
SinceDescribeis overridden inSportsCar, all calls print "SportsCar" regardless of reference type.Final Answer:
SportsCar\nSportsCar\nSportsCar -> Option DQuick Check:
All calls use SportsCar override [OK]
- Assuming base class method runs for base type variable
- Confusing reference type with object type
- Ignoring override in most derived class
