0
0
C Sharp (C#)programming~10 mins

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

Choose your learning style9 modes available
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.