Method overriding lets a child class change how a method works from its parent class. This helps make programs flexible and easy to update.
Method overriding with virtual and override in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
class BaseClass { public virtual void MethodName() { // base method code } } class ChildClass : BaseClass { public override void MethodName() { // new method code } }
virtual keyword marks a method in the base class that can be changed in child classes.
override keyword tells the compiler that the child class is changing the base class method.
Animal has a virtual method Speak. The Dog class changes it to say "Dog barks".using System; class Animal { public virtual void Speak() { Console.WriteLine("Animal speaks"); } } class Dog : Animal { public override void Speak() { Console.WriteLine("Dog barks"); } }
Car class overrides the Start method to show a more specific message.using System; class Vehicle { public virtual void Start() { Console.WriteLine("Vehicle starting"); } } class Car : Vehicle { public override void Start() { Console.WriteLine("Car starting with key"); } }
This program shows how the Student class changes the Greet method from Person. Even when a Student is used as a Person type, the overridden method runs.
using System; class Person { public virtual void Greet() { Console.WriteLine("Hello from Person"); } } class Student : Person { public override void Greet() { Console.WriteLine("Hello from Student"); } } class Program { static void Main() { Person p = new Person(); p.Greet(); Student s = new Student(); s.Greet(); Person ps = new Student(); ps.Greet(); } }
If you forget virtual in the base class, you cannot override the method in child classes.
Use override to make sure you are actually changing a method from the base class.
Calling the method on a base class reference that points to a child object runs the child's version if overridden.
Use virtual in the base class to allow method changes.
Use override in the child class to change the method.
This helps customize behavior while keeping a shared structure.
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
