Method overriding with virtual and override in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use method overriding with virtual and override in C#, it changes how methods are called at runtime.
We want to understand how this affects the time it takes to run the program as it grows.
Analyze the time complexity of the following code snippet.
using System;
class Animal {
public virtual void Speak() {
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal {
public override void Speak() {
Console.WriteLine("Dog barks");
}
}
// Usage
Animal myPet = new Dog();
myPet.Speak();
This code shows a base class with a virtual method and a derived class overriding it. The method call is decided at runtime.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single method call using virtual dispatch.
- How many times: Once per call; no loops or recursion here.
Since there is no loop or repeated calls, the time to execute the method call stays the same no matter how many objects or classes exist.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 method call |
| 100 | 1 method call |
| 1000 | 1 method call |
Pattern observation: The time does not grow with input size because each call is independent.
Time Complexity: O(1)
This means each method call takes a constant amount of time regardless of program size.
[X] Wrong: "Overriding methods makes the program slower as the number of classes grows."
[OK] Correct: The method call uses a fixed lookup mechanism that does not slow down with more classes or objects.
Understanding how virtual and override affect method calls helps you explain object-oriented design and performance clearly in interviews.
"What if the method call was inside a loop running n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand base class method flexibility
Thevirtualkeyword marks a method in the base class as changeable by derived classes.Step 2: Differentiate from other keywords
overrideis used in derived classes,newhides methods, andabstractrequires implementation.Final Answer:
virtual -> Option CQuick Check:
Base method change = virtual [OK]
- Confusing override with virtual
- Using new instead of virtual for overriding
- Thinking abstract allows method change without implementation
Display in a derived class?Solution
Step 1: Identify override syntax
To change a virtual method in a derived class, useoverridebefore the method signature.Step 2: Eliminate other options
The plainpublic void Display() { }lacks theoverridekeyword,public virtual void Display() { }incorrectly usesvirtualin the derived class, andpublic new void Display() { }hides the base method but doesn't override it for polymorphism.Final Answer:
public override void Display() { } -> Option DQuick Check:
Override method uses override keyword [OK]
- Omitting override keyword in derived class
- Using virtual instead of override in derived class
- Using new keyword instead of override
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());Solution
Step 1: Understand virtual and override behavior
BecauseSpeakis virtual and overridden, the derived class method runs even when referenced as base type.Step 2: Trace the output calls
ais aDoginstance, soSpeak()returns "Bark";bis aCatinstance, so it returns "Meow".Final Answer:
Bark Meow -> Option AQuick Check:
Override method output = Bark, Meow [OK]
- Expecting base class method output
- Ignoring override effect on base class reference
- Confusing new keyword behavior with override
class Base {
public virtual void Show() { Console.WriteLine("Base"); }
}
class Derived : Base {
public void Show() { Console.WriteLine("Derived"); }
}
Base obj = new Derived();
obj.Show();Solution
Step 1: Check method overriding rules
The derived class methodShowdoes not useoverride, so it hides the base method instead of overriding.Step 2: Determine method called by base reference
BecauseShowis virtual in base but not overridden, callingobj.Show()calls base class method, outputting "Base".Final Answer:
No error; output is Base -> Option AQuick Check:
Missing override means base method runs [OK]
- Assuming method hides override automatically
- Expecting Derived output without override
- Thinking missing override causes compile error
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());Solution
Step 1: Understand new vs override
SportsCarusesnewto hideDescribe, not override it. So base class virtual dispatch applies only up toCar.Step 2: Trace each call
v.Describe()callsVehiclereference toSportsCarinstance, but virtual dispatch stops atCaroverride, so returns "Car".c.Describe()callsCarreference toSportsCar, same as above, returns "Car".s.Describe()callsSportsCarreference, so calls hidden method returning "SportsCar".Final Answer:
Car Car SportsCar -> Option BQuick Check:
new hides method, override dispatches virtual [OK]
- Expecting new method to override virtual dispatch
- Confusing new with override behavior
- Assuming base reference calls hidden method
