Introduction to Inheritance in C#: an overview

Inheritance lets one class build on another. An archer is a character, so Archer inherits everything Character has, and adds what only archers have.

Apprentice · C# · OOP · Updated

By

After this lesson you can

  • Explain inheritance as an is-a relationship between a base class and a derived class

Key takeaways

  • Inheritance fits an is-a relationship: an archer is a character, so Archer can inherit from Character.
  • The derived (child) class gets the base (parent) class's properties and methods without writing them again, and adds only its own, such as Arrows.
  • Say the sentence out loud. If it's has-a, as in a car has an engine, use a property such as Engine instead of inheritance.

Inheritance is one of the pillars of object-oriented programming, beside encapsulation. It solves the problem from DRY Code in C#: shared code written once, with no tangle of ifs.

Is-a

Inheritance describes an is-a relationship. An archer is a character. So the Archer class can inherit from the Character class, and it gets everything Character has: its properties and its methods.

Archer inherits from Character: it has everything a character has, plus arrows

Because an archer is a character, an archer has a name, health, a level and experience, and can attack, take damage and level up, all without writing any of it again. On top of that, Archer adds what only archers have: Arrows, and an AddArrow() method.

The words

Word Means Example
Base class The class that's inherited from (also called the parent class) Character
Derived class The class that inherits (also called the child class) Archer
Inherit Get the base class's members without writing them again Archer inherits LevelUp()

Is-a, or has-a?

Inheritance only fits an is-a relationship. Test it by saying the sentence out loud:

Sentence True? So
An archer is a character Yes Archer can inherit from Character
A wizard is a character Yes Wizard can inherit from Character
An archer is a bow No: an archer has a bow Archer gets a Bow property instead
A car is an engine No: a car has an engine Car gets an Engine property

When the sentence is has a, the class holds the other as a property: an Order class would hold its products as a List<Product> property. Inheritance is only for is a.

Do it: give the archer a bow

An archer is a character, but an archer has a bow. So Archer doesn't inherit from Bow: it holds a bow, as a property. (Making Archer inherit from Character comes in the next lessons; here, Archer stands on its own.)

The Bow class is given. A bow has two values:

  • Kind: what kind of bow it is, such as "Longbow".
  • Damage: how much damage its arrows do, such as 8.

Your Archer class gets two values when it's made:

  • name: the archer's name, such as "Robin".
  • bow: the archer's bow, a Bow object.

For example:

Archer robin = new Archer("Robin", new Bow("Longbow", 8));
You ask for It gives
robin.Name "Robin"
robin.Bow.Damage 8
robin.Describe() "Robin has a Longbow"

Your job: write the class Archer.

  • A constructor, Archer(string name, Bow bow).
  • A Name property, a string, set from name.
  • A Bow property, a Bow, set from bow.
  • A method Describe() that returns a string: the name, then has a, then the bow's Kind.

Until Archer has these, the tests can't run, and the Tests tab names the first thing they're missing.

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

Inheritance turns "an archer is a character" into code: one base class with the shared parts, and a derived class for each kind with only its own parts. The next lessons show how to find those parts and write them in C#.

Key terms

Cheat sheets