Lesson 24 of 34 · Inheritance & Interfaces
C# Inheritance: virtual, override and base
Warriors, mages and archers are all characters: they have a name and health, they can attack and describe themselves. Writing those parts three times would be wasteful and error-prone. Inheritance lets a class build on another one: the new class gets everything the original has and adds or changes only what is different.
Base classes and derived classes
public class Character
{
public string Name { get; }
public int Health { get; protected set; }
public Character(string name, int health)
{
Name = name;
Health = health;
}
public void TakeDamage(int amount) => Health = Math.Max(0, Health - amount);
}
public class Archer : Character
{
public int Arrows { get; private set; } = 20;
public Archer(string name) : base(name, 90) { }
public void Shoot() => Arrows--;
}
Archer : Character means Archer inherits from Character. Character is the base class, Archer the derived class. An archer has Name, Health and TakeDamage without writing them again, plus its own Arrows and Shoot. The relationship reads as "is a": an archer is a character.
Calling the base constructor
A derived class must make sure the base part of the object is initialised. : base(name, 90) calls the Character constructor before the archer's own constructor body runs. If the base class has no parameterless constructor, forgetting : base(...) is a compile error.
protected
private members are invisible even to derived classes. protected members are visible to the class and its descendants, but not to the outside world. Above, Health has a protected set, so archers may change it but other code may not.
virtual and override
A base class marks methods that derived classes may replace with virtual. A derived class replaces them with override:
public class Character
{
// ...
public virtual int Attack() => 5;
public virtual string Describe() => $"{Name} ({Health} HP)";
}
public class Knight : Character
{
public Knight(string name) : base(name, 150) { }
public override int Attack() => 12;
public override string Describe() => "Sir " + base.Describe(); // reuse the base version
}
base.Describe() calls the version from the base class, so the knight extends the description instead of rewriting it. The magic of virtual is that the override is used even through a base-class variable:
Character c = new Knight("Lan");
Console.WriteLine(c.Attack()); // 12 — the Knight's version
Console.WriteLine(c.Describe()); // Sir Lan (150 HP)
You will explore that idea, polymorphism, in a later lesson.
sealed and the object class
sealed class Dragon : Character prevents anyone from inheriting from Dragon. A class can inherit from only one base class, and every class ultimately inherits from object — which is why every object has ToString(), Equals() and GetHashCode(), and why you could override ToString() in the previous boss.
Common mistakes
- Overriding a non-virtual method:
overrideonly works onvirtual,abstractoroverridemembers. - Forgetting
: base(...)when the base class needs constructor arguments. - Using inheritance for "has a": a backpack has gear; it is not a kind of gear. Use a field or property instead.
Assignment
Character is given. Complete two derived classes:
Warrior(name, strength)starts with 150 health and storesStrength. ItsAttack()returnsBaseDamage + Strength * 2.Mage(name, mana)starts with 80 health and storesMana(which can change). ItsAttack()casts a spell: if the mage has at least 10 mana it spends 10 and deals 30; otherwise it pokes with its staff for 2. ItsDescribe()adds the mana to the base description:"Zed (80 HP) [40 mana]".
Premium lesson
Unlock the full Learn C# course
- All 34 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Inheritance in other languages
- JavaScript Inheritance: extends, super and static · JavaScript
- Python Inheritance and super() · Python
- Go Struct Embedding and Composition · Go
- Java Inheritance: extends and super · Java
Need a quick reminder? The C# cheat sheet has every topic of Learn C# on one page.
Lesson last updated on .