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

Method overriding with virtual and override 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 - 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.

Practice

(1/5)
1. What keyword in C# allows a method in a base class to be changed by a derived class?
easy
A. new
B. override
C. virtual
D. abstract

Solution

  1. Step 1: Understand base class method flexibility

    The virtual keyword marks a method in the base class as changeable by derived classes.
  2. Step 2: Differentiate from other keywords

    override is used in derived classes, new hides methods, and abstract requires implementation.
  3. Final Answer:

    virtual -> Option C
  4. Quick Check:

    Base method change = virtual [OK]
Hint: Base class method change uses virtual keyword [OK]
Common Mistakes:
  • Confusing override with virtual
  • Using new instead of virtual for overriding
  • Thinking abstract allows method change without implementation
2. Which of the following is the correct syntax to override a virtual method named Display in a derived class?
easy
A. public void Display() { }
B. public virtual void Display() { }
C. public new void Display() { }
D. public override void Display() { }

Solution

  1. Step 1: Identify override syntax

    To change a virtual method in a derived class, use override before the method signature.
  2. Step 2: Eliminate other options

    The plain public void Display() { } lacks the override keyword, public virtual void Display() { } incorrectly uses virtual in the derived class, and public new void Display() { } hides the base method but doesn't override it for polymorphism.
  3. Final Answer:

    public override void Display() { } -> Option D
  4. Quick Check:

    Override method uses override keyword [OK]
Hint: Override methods must use override keyword [OK]
Common Mistakes:
  • Omitting override keyword in derived class
  • Using virtual instead of override in derived class
  • Using new keyword instead of override
3. What will be the output of the following code?
class Animal {
  public virtual string Speak() { return "Animal sound"; }
}
class Dog : Animal {
  public override string Speak() { return "Bark"; }
}
class Cat : Animal {
  public override string Speak() { return "Meow"; }
}

Animal a = new Dog();
Animal b = new Cat();
Console.WriteLine(a.Speak());
Console.WriteLine(b.Speak());
medium
A. Bark Meow
B. Animal sound Animal sound
C. Bark Animal sound
D. Animal sound Meow

Solution

  1. Step 1: Understand virtual and override behavior

    Because Speak is virtual and overridden, the derived class method runs even when referenced as base type.
  2. Step 2: Trace the output calls

    a is a Dog instance, so Speak() returns "Bark"; b is a Cat instance, so it returns "Meow".
  3. Final Answer:

    Bark Meow -> Option A
  4. Quick Check:

    Override method output = Bark, Meow [OK]
Hint: Virtual method calls use derived override at runtime [OK]
Common Mistakes:
  • Expecting base class method output
  • Ignoring override effect on base class reference
  • Confusing new keyword behavior with override
4. Identify the error in this code snippet:
class Base {
  public virtual void Show() { Console.WriteLine("Base"); }
}
class Derived : Base {
  public void Show() { Console.WriteLine("Derived"); }
}

Base obj = new Derived();
obj.Show();
medium
A. No error; output is Base
B. No error; output is Derived
C. Compile-time error: missing override keyword
D. Runtime error: method not found

Solution

  1. Step 1: Check method overriding rules

    The derived class method Show does not use override, so it hides the base method instead of overriding.
  2. Step 2: Determine method called by base reference

    Because Show is virtual in base but not overridden, calling obj.Show() calls base class method, outputting "Base".
  3. Final Answer:

    No error; output is Base -> Option A
  4. Quick Check:

    Missing override means base method runs [OK]
Hint: Override keyword needed to replace virtual method [OK]
Common Mistakes:
  • Assuming method hides override automatically
  • Expecting Derived output without override
  • Thinking missing override causes compile error
5. Given the classes below, what will be the output?
class Vehicle {
  public virtual string Describe() => "Vehicle";
}
class Car : Vehicle {
  public override string Describe() => "Car";
}
class SportsCar : Car {
  public new 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());
hard
A. Vehicle Car SportsCar
B. Car Car SportsCar
C. Vehicle Vehicle Vehicle
D. SportsCar SportsCar SportsCar

Solution

  1. Step 1: Understand new vs override

    SportsCar uses new to hide Describe, not override it. So base class virtual dispatch applies only up to Car.
  2. Step 2: Trace each call

    v.Describe() calls Vehicle reference to SportsCar instance, but virtual dispatch stops at Car override, so returns "Car".
    c.Describe() calls Car reference to SportsCar, same as above, returns "Car".
    s.Describe() calls SportsCar reference, so calls hidden method returning "SportsCar".
  3. Final Answer:

    Car Car SportsCar -> Option B
  4. Quick Check:

    new hides method, override dispatches virtual [OK]
Hint: new hides method; override participates in virtual dispatch [OK]
Common Mistakes:
  • Expecting new method to override virtual dispatch
  • Confusing new with override behavior
  • Assuming base reference calls hidden method