Inheritance Review

A quick look back at inheritance in C#. A derived class names its base class after a colon, inherits its members, adds its own, and overrides the base class's virtual methods.

Apprentice · C# · OOP · Updated

By

After this lesson you can

  • Read a base class and a derived class, and say what the derived class inherits, adds and overrides

Key takeaways

  • In class Archer : Character, Archer is the derived class and inherits Character's Name, Health and Level without writing them.
  • A derived class can add members of its own, such as Arrows, which only archers have.
  • In the archer's constructor, : base(name) passes the name on to Character's constructor.
  • A derived class may override a virtual method, and base.LevelUp() runs the base class's version inside the override.

Abstract classes are built on inheritance, so here's a quick reminder of how it works in C#. Inheritance is an is-a relationship: an archer is a character, so Archer inherits everything Character has.

The base class and a derived class

Archer robin = new Archer("Robin");
robin.LevelUp();

Console.WriteLine(quot;{robin.Name}: level {robin.Level}, arrows {robin.Arrows}, {robin.SpecialAbility()}");

public class Character
{
    public string Name { get; }
    public int Health { get; protected set; } = 100;
    public int Level { get; protected set; } = 1;

    public Character(string name)
    {
        Name = name;
    }

    public virtual void LevelUp()
    {
        Level++;
        Health += 10;
    }

    public virtual string SpecialAbility()
    {
        return "None";
    }
}

public class Archer : Character
{
    public int Arrows { get; private set; }

    public Archer(string name) : base(name)
    {
    }

    public override void LevelUp()
    {
        base.LevelUp();
        Arrows++;
    }

    public override string SpecialAbility()
    {
        return "Rain of arrows";
    }
}
Robin: level 2, arrows 1, Rain of arrows

What each part does

In the code What it means
class Archer : Character Archer is the derived class, Character its base class.
Name, Health, Level Inherited: Archer never wrote them, but every archer has them.
Arrows Added: only archers have it.
: base(name) The archer's constructor passes the name on to Character's constructor.
virtual in Character Derived classes may change this method.
override in Archer Archer replaces the method with its own version.
base.LevelUp() The override runs Character's version first, then adds the arrow.

Notice what Character can do

Character is a complete class. new Character("Villager") makes a perfectly good villager, whose special ability is None.

That works for characters. But some base classes describe an idea so general that an object of just that class makes no sense. You can open a checking account or a savings account, but not an account of no kind at all. The next lesson shows how C# handles those.

Do it: finish the wizard

The game needs a second kind of character: a wizard. A wizard is a character, so it gets a Name, Health and Level from Character. It also has something only wizards have: mana, the magic it spends on spells.

Character is given, the same class as above. The Wizard class is started: it derives from Character, passes the name on with : base(name), and has a Mana property that starts at 50.

For example, a new wizard named Merlin who levels up once:

Wizard merlin = new Wizard("Merlin");
merlin.LevelUp();
Level Health Mana
New 1 100 50
After one LevelUp() 1 + 1 = 2 100 + 10 = 110 50 + 10 = 60

So after one level-up, Merlin is level 2, with 110 health and 60 mana. His SpecialAbility() returns "Fireball".

Your job: finish the Wizard class.

  • Override LevelUp(): run Character's version first, so the level and health still go up, then add 10 to Mana.
  • Override SpecialAbility() so it returns "Fireball".
  • Leave Character as it is.

Practiced live on the lesson page: write the C# right there and check it.

Check yourself

1 question to try on the lesson page.

So what

Inherit what's shared, add what's new, override what's different. Keep that picture in mind: an abstract class is the same picture with one rule added.

Key terms

Cheat sheets