C# Inheritance

Understanding Inheritance in C#

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class (called a derived class or subclass) to inherit properties and methods from an existing class (called a base class or superclass). This promotes code reusability and establishes a relationship between classes that share common characteristics.

The 'is-a' Relationship

Inheritance models an 'is-a' relationship. For example, a Car is a Vehicle. A Dog is an Animal. This means that a derived class is a specialized version of its base class.

Syntax for Inheritance

In C#, you use a colon (:) followed by the base class name to indicate inheritance.

public class DerivedClass : BaseClass
{
    // Members of DerivedClass
}

Example: A Simple Inheritance Hierarchy

Let's consider a Vehicle base class and a Car derived class.

Base Class: Vehicle

public class Vehicle
{
    public string Brand { get; set; }
    public int Year { get; set; }

    public void StartEngine()
    {
        Console.WriteLine("Engine started.");
    }

    public virtual void DisplayInfo()
    {
        Console.WriteLine($"Brand: {Brand}, Year: {Year}");
    }
}

Derived Class: Car

public class Car : Vehicle
{
    public int NumberOfDoors { get; set; }

    public void Drive()
    {
        Console.WriteLine("Car is driving.");
    }

    // Overriding a virtual method from the base class
    public override void DisplayInfo()
    {
        base.DisplayInfo(); // Calls the base class's DisplayInfo
        Console.WriteLine($"Number of Doors: {NumberOfDoors}");
    }
}

Key Concepts in Inheritance

Constructor Chaining

When a derived class object is created, the base class constructor is called first. You can use the base() syntax in the derived class constructor to explicitly call a specific base class constructor.

public class Car : Vehicle
{
    public int NumberOfDoors { get; set; }

    // Constructor for Car, calling the base constructor
    public Car(string brand, int year, int doors) : base(brand, year)
    {
        NumberOfDoors = doors;
    }

    // ... other members
}

// Assuming Vehicle has a constructor like this:
public class Vehicle
{
    public string Brand { get; set; }
    public int Year { get; set; }

    public Vehicle(string brand, int year)
    {
        Brand = brand;
        Year = year;
    }
    // ...
}

Access Modifiers and Inheritance

Access modifiers (public, private, protected, internal) play a role in what members can be accessed by derived classes:

Single vs. Multiple Inheritance

C# supports single inheritance for classes, meaning a class can inherit from only one direct base class. However, C# supports multiple interface implementation, which allows a class to implement multiple interfaces. This design choice helps to avoid the complexity and potential issues associated with multiple inheritance of implementation.

Benefits of Inheritance

Inheritance is a powerful tool for building well-structured and maintainable object-oriented applications. Mastering its principles is crucial for effective C# development.