Lesson 29 of 34 · Inheritance & Interfaces
Boss: Morwen the Mirror Witch
Morwen the Mirror Witch blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.
The final hall of the west wing is lined with mirrors, and in every one of them stands Morwen the Mirror Witch. Each reflection fights in a different style — one hides behind steel, one strikes from afar, one mends the others' wounds. Only a commander who understands every kind of fighter through one common language can shatter the glass.
Battle plan
Unit is the abstract base class of every fighter. It is complete — read it carefully:
Healthcan only be changed throughTakeDamage(virtual) andRestore(heals the living, never aboveMaxHealth);Poweris abstract: every kind of unit decides how hard it hits;Attack(target)dealsPowerdamage to the target through the target's ownTakeDamage— so a knight's armour works no matter who attacks it.
Two interfaces describe extra capabilities: IArmored (has an Armor value) and IHealer (can Heal another unit). The Arena helpers must work with any mix of units, using only Unit and the interfaces — never by checking for concrete classes.
Assignment
Knight(120 max health, power 15) isIArmoredwith armor 4. It overridesTakeDamage: every hit is reduced by the armor, but always does at least 1 damage.Archer(70 max health) has power 18 and no special rules.Cleric(80 max health, power 5) is anIHealer:Heal(target)restores 20 health to the target. ItsDescribe()adds" (healer)"to the base description.Arena.TotalPower(units)adds up the power of the living units.Arena.Volley(attackers, target)lets every living attacker attack the target once.Arena.HealParty(party)— every living healer heals the living unit with the lowest health (the first one on a tie). Return how many heals were cast.Arena.Survivors(units)returns the names of the living units, in order.
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.
Keep learning
Need a quick reminder? The C# cheat sheet has every topic of Learn C# on one page.
Lesson last updated on .