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

Method overriding with virtual and override in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Method overriding with virtual and override
Base class defines virtual method
Derived class overrides method with override
Create derived class object
Call method on derived object
Derived method runs, not base
End
The base class marks a method as virtual. The derived class overrides it. When called on a derived object, the overridden method runs.
Execution Sample
C Sharp (C#)
using System;
class Base {
  public virtual void Show() {
    Console.WriteLine("Base Show");
  }
}
class Derived : Base {
  public override void Show() {
    Console.WriteLine("Derived Show");
  }
}
class Program {
  static void Main() {
    var obj = new Derived();
    obj.Show();
  }
}
Defines a base class method as virtual and overrides it in a derived class, then calls the method on a derived object.
Execution Table
StepActionMethod CalledOutput
1Create Derived objectNoneNone
2Call Show() on Derived objectDerived.Show()Derived Show
3End of executionNoneNone
💡 Method call resolved to Derived.Show() because of override keyword
Variable Tracker
VariableStartAfter Step 1After Step 2Final
objnullDerived instanceDerived instanceDerived instance
Key Moments - 3 Insights
Why does calling Show() on the derived object run Derived.Show() and not Base.Show()?
Because the base method is marked virtual and the derived method uses override, the call resolves to the derived version at runtime (see execution_table step 2).
What happens if the base method is not marked virtual but the derived method uses override?
This causes a compile error because override requires the base method to be virtual or abstract.
Can you call the base class method from the derived class override?
Yes, by using base.Show() inside the derived override method to run the base version.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which method runs when obj.Show() is called?
ADerived.Show()
BBase.Show()
CBoth Base.Show() and Derived.Show()
DNo method runs
💡 Hint
Check step 2 in the execution_table where the method called is shown
At which step is the Derived object created?
AStep 3
BStep 1
CStep 2
DNo object is created
💡 Hint
Look at the Action column in execution_table for object creation
If the base method was not virtual, what would happen when compiling this code?
AIt compiles and runs Derived.Show()
BIt compiles and runs Base.Show()
CCompile error due to override keyword
DRuntime error
💡 Hint
Recall key_moments about override requiring virtual base method
Concept Snapshot
Use virtual keyword on base class method to allow overriding.
Use override keyword in derived class to replace base method.
Calling method on derived object runs overridden method.
Override requires base method to be virtual or abstract.
Use base.Method() to call base version inside override.
Full Transcript
In C#, method overriding lets a derived class replace a base class method's behavior. The base method must be marked with the virtual keyword. The derived class uses override to provide a new version. When you create an object of the derived class and call the method, the overridden version runs. This is shown in the example where Base.Show() is virtual and Derived.Show() overrides it. The execution table shows the derived method runs when called. If the base method is not virtual, using override causes a compile error. You can also call the base method from inside the override using base.Show(). This mechanism helps customize behavior in subclasses while keeping a common interface.