Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Runtime polymorphism execution in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Runtime polymorphism execution
Create base class reference
Assign derived class object
Call overridden method
Runtime decides which method to run
Derived class method runs if overridden
Output result
End
At runtime, the program decides which method to call based on the actual object type, enabling dynamic behavior.
Execution Sample
C Sharp (C#)
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 a = new Dog();
    a.Speak();
  }
}
This code shows a base class Animal and a derived class Dog overriding Speak(). The call a.Speak() runs Dog's Speak() because a refers to a Dog object.
Execution Table
StepActionObject TypeMethod CalledOutput
1Create Animal reference 'a' and assign new Dog()DogN/AN/A
2Call a.Speak()DogDog.Speak()Dog barks
3End of executionDogN/AN/A
💡 Execution stops after calling the overridden method on the Dog object.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
anullDog object referenceDog object referenceDog object reference
Key Moments - 2 Insights
Why does calling a.Speak() run Dog's Speak() and not Animal's?
Because 'a' refers to a Dog object at runtime, the overridden Dog.Speak() method is called, as shown in execution_table step 2.
What if Dog did not override Speak()?
Then calling a.Speak() would run Animal's Speak() method, since no override exists. This is implied by the runtime method selection in the flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the type of object 'a' after step 1?
AAnimal
Bnull
CDog
DObject
💡 Hint
Check the 'Object Type' column in execution_table row 1.
At which step is the overridden method Dog.Speak() called?
AStep 2
BStep 1
CStep 3
DNever
💡 Hint
Look at the 'Method Called' column in execution_table.
If 'a' was assigned new Animal() instead of new Dog(), what method would be called at step 2?
ADog.Speak()
BAnimal.Speak()
CNo method
DCompile error
💡 Hint
Runtime method depends on actual object type, see concept_flow and key_moments.
Concept Snapshot
Runtime polymorphism means the program decides which method to run based on the actual object type at runtime.
Use 'virtual' in base class and 'override' in derived class.
A base class reference can point to a derived class object.
Calling an overridden method runs the derived class version.
This enables flexible and dynamic behavior.
Full Transcript
This example shows runtime polymorphism in C#. We have a base class Animal with a virtual method Speak. The derived class Dog overrides Speak. We create a base class reference 'a' and assign it a new Dog object. When we call a.Speak(), the program checks the actual object type at runtime, which is Dog, and runs Dog's Speak method. The execution table shows the steps: creating the reference, calling the method, and ending. The variable tracker shows 'a' holds a Dog object after assignment. Key moments clarify why the derived method runs and what happens if no override exists. The quiz tests understanding of object type, method call step, and behavior if the object type changes. Runtime polymorphism allows flexible code that adapts behavior based on actual objects during execution.

Practice

(1/5)
1. What does runtime polymorphism in C# allow you to do?
easy
A. Create multiple instances of the same class
B. Change variable types at runtime
C. Call derived class methods through a base class reference
D. Use static methods without creating objects

Solution

  1. Step 1: Understand runtime polymorphism concept

    Runtime polymorphism allows a base class reference to call methods overridden in derived classes.
  2. Step 2: Identify correct behavior

    This means the actual method called depends on the object's real type, not the reference type.
  3. Final Answer:

    Call derived class methods through a base class reference -> Option C
  4. Quick Check:

    Runtime polymorphism = base ref calls derived method [OK]
Hint: Think: base class ref calls derived method at runtime [OK]
Common Mistakes:
  • Confusing polymorphism with changing variable types
  • Thinking static methods are polymorphic
  • Believing polymorphism creates multiple instances
2. Which keyword is used in C# to allow a method to be overridden in a derived class?
easy
A. virtual
B. override
C. new
D. abstract

Solution

  1. Step 1: Identify keyword to enable overriding

    The base class method must be marked with virtual to allow overriding.
  2. Step 2: Understand roles of keywords

    override is used in derived classes, virtual in base classes.
  3. Final Answer:

    virtual -> Option A
  4. Quick Check:

    Base method uses virtual to allow override [OK]
Hint: Base method uses virtual; derived uses override [OK]
Common Mistakes:
  • Using override in base class instead of virtual
  • Confusing new keyword with override
  • Thinking abstract is required for all overrides
3. What is the output of this C# code?
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());
medium
A. Animal sound
B. Bark
C. Meow
D. Compile error

Solution

  1. Step 1: Identify object type and method called

    Variable a is of type Animal but references a Dog object.
  2. Step 2: Apply runtime polymorphism

    Since Speak is virtual and overridden in Dog, the Dog version runs, printing "Bark".
  3. Final Answer:

    Bark -> Option B
  4. Quick Check:

    Base ref calls Dog's Speak() = Bark [OK]
Hint: Base ref calls derived override method at runtime [OK]
Common Mistakes:
  • Expecting base class method output
  • Confusing object type with reference type
  • Thinking compile error due to override
4. Identify the error in this C# code related to runtime polymorphism:
class Base {
  public override void Show() {
    Console.WriteLine("Base Show");
  }
}
class Derived : Base {
  public override void Show() {
    Console.WriteLine("Derived Show");
  }
}
medium
A. Base class method must be virtual, not override
B. Derived class method cannot override base method
C. Missing semicolon after method declaration
D. No error, code is correct

Solution

  1. Step 1: Check base class method declaration

    Base class method incorrectly uses override instead of virtual.
  2. Step 2: Understand override rules

    Only derived classes use override; base class must use virtual to allow overriding.
  3. Final Answer:

    Base class method must be virtual, not override -> Option A
  4. Quick Check:

    Base method needs virtual keyword [OK]
Hint: Base method uses virtual, not override [OK]
Common Mistakes:
  • Using override in base class method
  • Thinking override is allowed without virtual
  • Ignoring method signature correctness
5. Given these classes:
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?
hard
A. Vehicle\nCar\nSportsCar
B. Car\nCar\nCar
C. Vehicle\nVehicle\nVehicle
D. SportsCar\nSportsCar\nSportsCar

Solution

  1. Step 1: Identify actual object type for all references

    All variables v, c, and s reference a SportsCar object.
  2. Step 2: Apply runtime polymorphism for Describe()

    Since Describe is overridden in SportsCar, all calls print "SportsCar" regardless of reference type.
  3. Final Answer:

    SportsCar\nSportsCar\nSportsCar -> Option D
  4. Quick Check:

    All calls use SportsCar override [OK]
Hint: Actual object type decides method, not reference type [OK]
Common Mistakes:
  • Assuming base class method runs for base type variable
  • Confusing reference type with object type
  • Ignoring override in most derived class