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
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
Archercan inherit fromCharacter. - 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
Engineinstead 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.

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.
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#.