Runtime polymorphism lets a program decide which method to run while it is running, not before. This helps make programs flexible and easy to change.
Runtime polymorphism execution in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
class BaseClass { public virtual void Show() { // base method } } class DerivedClass : BaseClass { public override void Show() { // derived method } } BaseClass obj = new DerivedClass(); obj.Show();
virtual keyword marks a method that can be changed in child classes.
override keyword is used in child classes to provide a new version of the method.
Speak method is called on an Animal reference but runs the Dog version because of runtime polymorphism.class Animal { public virtual void Speak() { Console.WriteLine("Animal speaks"); } } class Dog : Animal { public override void Speak() { Console.WriteLine("Dog barks"); } } Animal pet = new Dog(); pet.Speak();
Draw method runs the Circle version even though the variable is of type Shape.class Shape { public virtual void Draw() { Console.WriteLine("Drawing shape"); } } class Circle : Shape { public override void Draw() { Console.WriteLine("Drawing circle"); } } Shape s = new Circle(); s.Draw();
This program shows runtime polymorphism by calling the Start method on a Vehicle reference that points to different types of vehicles. The actual method that runs depends on the object type at runtime.
using System; class Vehicle { public virtual void Start() { Console.WriteLine("Vehicle is starting"); } } class Car : Vehicle { public override void Start() { Console.WriteLine("Car is starting with a roar"); } } class Bike : Vehicle { public override void Start() { Console.WriteLine("Bike is starting quietly"); } } class Program { static void Main() { Vehicle myVehicle; myVehicle = new Car(); myVehicle.Start(); myVehicle = new Bike(); myVehicle.Start(); } }
Runtime polymorphism requires a base method marked with virtual and derived methods marked with override.
The decision about which method to call happens when the program runs, not when it is compiled.
Using runtime polymorphism helps keep code easy to extend and maintain.
Runtime polymorphism lets methods behave differently based on the actual object type at runtime.
Use virtual in base classes and override in derived classes to enable this behavior.
This makes your code flexible and easier to change later.
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
