Introduction to Inheritance - Reusing and Extending Behavior in C#

Vaibhav • September 10, 2025

Welcome to Chapter 10 of our C# learning journey. In the previous chapter, we explored the foundations of object-oriented programming - how to design classes, encapsulate state, and build maintainable systems using principles like composition, immutability, and object lifecycle management. Now, we begin our exploration of inheritance - a powerful mechanism that allows classes to reuse and extend behavior.

Inheritance is one of the core pillars of object-oriented programming. It enables you to define a base class with common functionality and then create derived classes that inherit and specialize that behavior. This promotes code reuse, supports polymorphism, and helps model real-world relationships in software.

What is Inheritance?

Inheritance allows one class to derive from another. The derived class inherits all accessible members (fields, properties, methods) of the base class and can add new members or override existing ones. This creates an “is-a” relationship - for example, a Dog is an Animal.

public class Animal
{
    public void Eat()
    {
        Console.WriteLine("Eating...");
    }
}

public class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Barking...");
    }
}

In this example, Dog inherits from Animal. It gains access to the Eat() method and adds its own Bark() method. This allows you to reuse the common behavior of Animal while extending it with dog-specific functionality.

Why Use Inheritance?

Inheritance helps you:

  • Reuse code across related classes.
  • Model hierarchical relationships.
  • Enable polymorphism (covered later in this chapter).
  • Simplify maintenance by centralizing shared logic.

For example, if you have multiple types of employees - Manager, Developer, Intern - you can define a base Employee class with shared properties like Name and Salary, and let each derived class add its own behavior.

Inheritance is best used when there is a clear “is-a” relationship. If the relationship is more “has-a” or “uses-a”, prefer composition instead.

Syntax of Inheritance

To create a derived class, use the colon (:) syntax:

public class BaseClass
{
    public void ShowMessage()
    {
        Console.WriteLine("Hello from base class");
    }
}

public class DerivedClass : BaseClass
{
    public void ShowAnotherMessage()
    {
        Console.WriteLine("Hello from derived class");
    }
}

The derived class inherits all public and protected members of the base class. Private members are not accessible directly but can be used through public/protected methods.

Accessing Base Class Members

A derived class can access base class members directly if they are public or protected. You can also use the base keyword to refer to the base class explicitly.

public class Animal
{
    public void Eat()
    {
        Console.WriteLine("Animal is eating");
    }
}

public class Cat : Animal
{
    public void EatAndMeow()
    {
        base.Eat(); // Calls base class method
        Console.WriteLine("Cat says meow");
    }
}

The base.Eat() call ensures that the base class’s Eat() method is executed before adding new behavior.

Inheritance and Constructors

Constructors are not inherited, but a derived class can call a base class constructor using the base keyword. This is useful when the base class requires initialization.

public class Person
{
    public string Name { get; }

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

public class Student : Person
{
    public int Grade { get; }

    public Student(string name, int grade) : base(name)
    {
        Grade = grade;
    }
}

The Student constructor calls the Person constructor to initialize the Name property. This ensures that the base class is properly constructed.

If the base class has a parameterless constructor, the derived class can omit the : base() call - it will be invoked automatically.

Inheritance Depth and Design

C# supports single inheritance - a class can inherit from only one base class. However, you can create deep inheritance chains if needed:

class A { }
class B : A { }
class C : B { }

While this is allowed, deep inheritance hierarchies can become hard to manage. Prefer shallow hierarchies and use interfaces or composition for flexibility.

Inheritance vs Composition

Inheritance is useful for modeling “is-a” relationships. Composition is better for “has-a” or “uses-a” relationships. For example:

// Inheritance
class Bird : Animal { }

// Composition
class Car
{
    private Engine engine = new Engine();
}

Use inheritance when you want to extend behavior. Use composition when you want to delegate behavior.

Common Mistakes to Avoid

Inheritance is powerful, but it can lead to problems if misused. Avoid these common mistakes:

  • Using inheritance when composition is more appropriate.
  • Creating deep or fragile hierarchies.
  • Exposing internal details through base classes.
  • Overriding methods without understanding base behavior.

Always design base classes carefully. Document which methods are safe to override and which are not.

Summary

Inheritance is a key feature of object-oriented programming in C#. It allows you to define base classes with shared behavior and create derived classes that extend or specialize that behavior. Inheritance promotes code reuse, supports polymorphism, and helps model real-world relationships.

In this article, we explored how inheritance works, how to define base and derived classes, how to use constructors and the base keyword, and how to avoid common pitfalls. We also discussed the difference between inheritance and composition and when to use each.

As you continue learning C#, inheritance will help you build more expressive and reusable systems. In the next article, we’ll explore Base and Derived Classes - how to structure class hierarchies and design relationships between types.