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
Recall & Review
beginner
What does the virtual keyword do in C#?
The <code>virtual</code> keyword allows a method in a base class to be overridden in a derived class. It marks the method as extendable or replaceable.
Click to reveal answer
beginner
What is the purpose of the override keyword in C#?
The <code>override</code> keyword tells the compiler that a method in a derived class is replacing a <code>virtual</code> method from its base class.
Click to reveal answer
intermediate
Can a method without <code>virtual</code> in the base class be overridden with <code>override</code> in the derived class?
No. Only methods marked <code>virtual</code> (or <code>abstract</code>) in the base class can be overridden using <code>override</code> in the derived class.
Click to reveal answer
intermediate
What happens if you call a virtual method on a base class reference that points to a derived class object?
The overridden method in the derived class is called. This is called polymorphism and allows behavior to change based on the actual object type.
Click to reveal answer
beginner
Show a simple example of method overriding using virtual and override.
Example:<br><pre>class Animal {
public virtual void Speak() {
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal {
public override void Speak() {
Console.WriteLine("Dog barks");
}
}</pre>
Click to reveal answer
Which keyword marks a method in the base class to allow overriding?
Avirtual
Boverride
Cnew
Dabstract
✗ Incorrect
The virtual keyword marks a method as overridable in the base class.
What keyword do you use in the derived class to replace a base class virtual method?
Avirtual
Bnew
Coverride
Dsealed
✗ Incorrect
The override keyword replaces the base class virtual method in the derived class.
If a base class method is not virtual, what happens if you try to override it?
ACompiler error
BIt overrides successfully
CRuns base method anyway
DMethod is hidden but not overridden
✗ Incorrect
You get a compiler error because only virtual or abstract methods can be overridden.
Calling a virtual method on a base class reference that points to a derived object calls:
ABase class method
BDerived class overridden method
CCompiler error
DRandom method
✗ Incorrect
The derived class overridden method is called due to polymorphism.
Which keyword prevents further overriding of a virtual method?
Avirtual
Boverride
Cabstract
Dsealed
✗ Incorrect
The sealed keyword stops further overriding of a virtual method.
Explain how method overriding works using virtual and override keywords in C#.
Think about how a base class method can be changed in a child class.
You got /4 concepts.
Describe what happens if you call a virtual method on a base class reference that points to a derived class object.
Focus on which method actually runs at runtime.
You got /4 concepts.
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
Step 1: Understand base class method flexibility
The virtual keyword marks a method in the base class as changeable by derived classes.
Step 2: Differentiate from other keywords
override is used in derived classes, new hides methods, and abstract requires implementation.
Final Answer:
virtual -> Option C
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
Step 1: Identify override syntax
To change a virtual method in a derived class, use override before the method signature.
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.
Final Answer:
public override void Display() { } -> Option D
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
Step 1: Understand virtual and override behavior
Because Speak is virtual and overridden, the derived class method runs even when referenced as base type.
Step 2: Trace the output calls
a is a Dog instance, so Speak() returns "Bark"; b is a Cat instance, so it returns "Meow".
Final Answer:
Bark
Meow -> Option A
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
Step 1: Check method overriding rules
The derived class method Show does not use override, so it hides the base method instead of overriding.
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".
Final Answer:
No error; output is Base -> Option A
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
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.
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".
Final Answer:
Car
Car
SportsCar -> Option B
Quick Check:
new hides method, override dispatches virtual [OK]
Hint: new hides method; override participates in virtual dispatch [OK]