Skip to main content
C# Fundamentals for Beginners to Advanced
CHAPTER 15 Beginner

Inheritance in C#

Updated: May 17, 2026
5 min read

# CHAPTER 15

Inheritance in C#

1. Introduction

If you are building a game with a Warrior class and a Mage class, both will need an X position, a Y position, and a Health property. Writing these identical properties in both classes violates the DRY (Don't Repeat Yourself) principle. Inheritance solves this by allowing classes to inherit properties and methods from a Parent class.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define Base (Parent) and Derived (Child) classes.
  • Use the : syntax to establish inheritance.
  • Use the protected access modifier.
  • Understand the virtual and override keywords.
  • Prevent inheritance using the sealed keyword.

3. Base and Derived Classes

In C#, you inherit from a class using a colon (:).
csharp
12345678910111213141516171819202122232425262728293031323334353637
using System;

// 1. The Base Class (Parent)
class Enemy
{
    public int Health { get; set; } = 100;

    public void Move()
    {
        Console.WriteLine("Enemy moves forward.");
    }
}

// 2. The Derived Class (Child)
// Goblin INHERITS FROM Enemy
class Goblin : Enemy
{
    public void StealGold()
    {
        Console.WriteLine("Goblin stole 10 gold!");
    }
}

class Program
{
    static void Main()
    {
        Goblin myGoblin = new Goblin();
        
        // Goblin has access to its own methods...
        myGoblin.StealGold();
        
        // ... AND all public methods/properties from the Enemy class!
        myGoblin.Move();
        Console.WriteLine($"Goblin Health: {myGoblin.Health}");
    }
}

4. The protected Access Modifier

We know public is visible everywhere, and private is visible only inside the class. protected is the middle ground: It is visible inside the class AND inside any derived (child) classes, but hidden from the outside world.
csharp
123456789101112
class Enemy
{
    protected int attackPower = 50; // Hidden from Main(), visible to Goblin
}

class Goblin : Enemy
{
    public void Attack()
    {
        Console.WriteLine($"Goblin attacks for {attackPower} damage!"); // Allowed!
    }
}

5. virtual and override (Method Overriding)

What if the Base class has a method, but the Derived class needs to do it *differently*? You must mark the Base method as virtual (meaning "allow children to change this"), and the Derived method as override (meaning "I am replacing the parent's version").
csharp
1234567891011121314151617
class Enemy
{
    // virtual: "You are allowed to override me"
    public virtual void Shout()
    {
        Console.WriteLine("Ahhhh!");
    }
}

class Zombie : Enemy
{
    // override: "I am changing the parent's logic"
    public override void Shout()
    {
        Console.WriteLine("Braaaaiiinnnsss!");
    }
}

6. The base Keyword

Sometimes, you want to override a parent's method, but you *also* want to run the parent's original code. You can call the parent's code using the base keyword.
csharp
12345678
class Boss : Enemy
{
    public override void Shout()
    {
        base.Shout(); // Calls Enemy.Shout() ("Ahhhh!")
        Console.WriteLine("I am the Boss!"); // Adds new functionality
    }
}

7. The sealed Keyword

If you create a highly optimized class and you absolutely do NOT want any other developers inheriting from it and changing its behavior, you mark it as sealed.
csharp
1234
sealed class MathEngine
{
    // No class can ever inherit from MathEngine
}

8. OOP and Memory Explanation

When you instantiate a Goblin object, the CLR allocates a single block of memory on the Heap. This block contains the memory footprint for the Goblin properties, AND the memory footprint for all the Enemy properties. It is treated as one unified object in memory.

9. Common Mistakes

  • Multiple Inheritance: C++ allows a class to inherit from two parent classes. C# does NOT. A class in C# can only have ONE direct base class. (We use Interfaces to get around this, covered in Chapter 18).
  • Forgetting virtual: If you try to override a method that isn't marked virtual in the base class, the C# compiler will throw an error.

10. Best Practices

  • Favor "Composition over Inheritance". Do not create massive, 7-level deep inheritance chains. If the relationship isn't strictly an "Is-A" relationship (e.g., A Car "Is-A" Vehicle), do not use inheritance.

11. Exercises

  1. 1. Create a Vehicle base class with a Speed property and a Drive() method. Create a Motorcycle child class that inherits from it.
  1. 2. Make the Drive() method virtual, and override it in the Motorcycle class to print "Zooming on two wheels!".

12. MCQs with Answers

Question 1

What symbol is used to denote inheritance in C#?

Question 2

A class that inherits from another class is called the:

Question 3

Which access modifier allows visibility to child classes, but hides it from the outside world?

Question 4

To allow a child class to modify a parent's method, the parent method must be marked with:

Question 5

To actually modify the parent's virtual method, the child method must use the keyword:

Question 6

What keyword prevents a class from being inherited by any other class?

Q7. Can a C# class inherit from two base classes simultaneously? a) Yes b) No (C# does not support multiple class inheritance) Answer: b) No
Question 8

Which keyword is used to access members or methods of the parent class from within the child class?

Question 9

If Car inherits from Vehicle, what type of relationship is this?

Question 10

Does a derived class inherit the private members of the base class?

13. Interview Questions

  • Q: Explain the difference between virtual/override and method hiding (new keyword).
  • Q: Why does C# not support multiple inheritance for classes?
  • Q: Explain the exact visibility of the protected access modifier.

14. Summary

Inheritance promotes code reuse by allowing Derived classes to adopt the fields and methods of a Base class. Using virtual and override, child classes can customize behaviors, while protected ensures data remains secure but accessible within the family tree.

15. Next Chapter Recommendation

Inheritance sets the stage for the most powerful concept in OOP. In Chapter 16: Polymorphism in C#, we will see how inheritance allows us to treat different objects interchangeably.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·