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

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.

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