What if your program could automatically pick the right action without you telling it every time?
Why Method overriding with virtual and override in C Sharp (C#)? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a base class with a method, and several child classes that need to change how that method works. Without special tools, you would have to write separate methods with different names or copy-paste code everywhere.
This manual way is slow and confusing. You might forget to call the right method, or accidentally call the base method when you want the child one. It's easy to make mistakes and hard to keep track of which method runs.
Using virtual in the base class and override in child classes lets you replace methods cleanly. The program automatically picks the right method to run depending on the object type, so you don't have to worry about calling the correct one.
class Animal { public void Speak() { Console.WriteLine("Animal sound"); } } class Dog : Animal { public void Speak() { Console.WriteLine("Bark"); } }
class Animal { public virtual void Speak() { Console.WriteLine("Animal sound"); } } class Dog : Animal { public override void Speak() { Console.WriteLine("Bark"); } }
This lets you write flexible programs where child classes can change behavior easily, making your code cleaner and easier to maintain.
Think of a video game where different characters have a Move() method. Using overriding, each character can move differently but you can call Move() on any character without extra checks.
Manual method changes are confusing and error-prone.
virtual and override let child classes replace base methods safely.
This makes your code easier to read, maintain, and extend.
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
