Inheritance in C#
A key component of object-oriented programming (OOP) in C# is inheritance. It enables a new class to inherit all of an existing class’s traits and behaviours. Improving code changeability and reusing existing code are the main goals of inheritance.
Terminology
- The existing class that properties and behaviours are transferred from is called the base class, sometimes referred to as the parent class or superclass.
- A derived class, sometimes referred to as a subclass or child class, is a new class that inherits traits and behaviours from the base class.
- A derived class essentially has two types: the type of its base class and its own type.
Implementation in C#
- The operator : (colon) is used in C# to indicate inheritance.
- Single inheritance is supported in C#, which means that a class can only directly inherit from one base class. In order to prevent complications like method duplication problems that arise in languages like C++ that allow multiple inheritance with classes, this design decision was made.
- The System is the implicit parent of all C# classes.Object class, which makes it the hierarchy’s top base class.
- The constructor is not passed down to subclasses. But the constructor of the subclass needs to invoke the constructor of the base class. When base() is not used to explicitly call a base constructor, the compiler automatically inserts a call to the base class’s parameterless constructor.
- It is not possible for derived classes to inherit private members from their parent class. The subclass may inherit and utilise the base class’s protected or public properties (get and set methods) if they allow access to its private fields.
- Through the use of method overriding, derived classes can reimplement base class methods. This is accomplished by utilising the override keyword in the derived class method declaration after the virtual keyword was used to declare the method in the base class. The base class’s implementation of a method can be explicitly invoked in an overridden method of the derived class by using the base keyword.
- Additionally, when generating instances of the derived class, you can indicate which base-class constructor should be called by using the base keyword.
- It is possible for derived classes to use the fields and methods of their parent class, but a parent class cannot use members that are only declared in its child class.
Types of Inheritance
Single Inheritance: One base class is the ancestor of all classes.

Example
using System;
// 1. Define the Base Class (Parent Class)
// This class represents a generic Animal.
public class Animal
{
// A property common to all animals
public string Name { get; set; }
// A method common to all animals
public void Eat()
{
Console.WriteLine($"{Name} is eating.");
}
// A constructor for the base class
public Animal(string name)
{
Name = name;
Console.WriteLine($"Animal '{Name}' created.");
}
}
// 2. Define the Derived Class (Child Class)
// The 'Dog' class inherits from the 'Animal' class using the ':' symbol.
// This means a Dog 'is an' Animal.
public class Dog : Animal
{
// A property specific to Dog
public string Breed { get; set; }
// A method specific to Dog
public void Bark()
{
Console.WriteLine($"{Name} (a {Breed}) barks: Woof! Woof!");
}
// Constructor for the Derived Class
// The 'base(name)' call invokes the constructor of the base class (Animal).
public Dog(string name, string breed) : base(name)
{
Breed = breed;
Console.WriteLine($"Dog '{Name}' ({Breed}) created.");
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("--- Demonstrating Single Inheritance ---");
// 3. Create an instance of the Derived Class (Dog)
Dog myDog = new Dog("Buddy", "Golden Retriever");
Console.WriteLine("\n--- Actions for myDog (Derived Class) ---");
// 4. Access members inherited from the Base Class (Animal)
Console.WriteLine($"My dog's name is: {myDog.Name}"); // Name is inherited from Animal
myDog.Eat(); // Eat() is inherited from Animal
// 5. Access members specific to the Derived Class (Dog)
Console.WriteLine($"My dog's breed is: {myDog.Breed}");
myDog.Bark(); // Bark() is specific to Dog
Console.WriteLine("\n--- End of Single Inheritance Example ---");
// You can also create an instance of the base class directly:
// Animal genericAnimal = new Animal("Lion");
// genericAnimal.Eat();
}
}
Output
--- Demonstrating Single Inheritance ---
Animal 'Buddy' created.
Dog 'Buddy' (Golden Retriever) created.
--- Actions for myDog (Derived Class) ---
My dog's name is: Buddy
Buddy is eating.
My dog's breed is: Golden Retriever
Buddy (a Golden Retriever) barks: Woof! Woof!
--- End of Single Inheritance Example ---
Hierarchical Inheritance: From a single base class, several derived classes are produced.

Example
using System;
// 1. Define the Base Class (Parent Class)
// This class contains properties and methods common to all vehicles.
public class Vehicle
{
public string Manufacturer { get; set; }
public int Year { get; set; }
public void StartEngine()
{
Console.WriteLine($"{Manufacturer}'s engine starts.");
}
public void StopEngine()
{
Console.WriteLine($"{Manufacturer}'s engine stops.");
}
}
// 2. Define the First Derived Class
// The 'Car' class inherits from the 'Vehicle' base class.
public class Car : Vehicle
{
public int NumberOfDoors { get; set; }
public void Drive()
{
Console.WriteLine($"The {Year} {Manufacturer} car is driving.");
}
}
// 3. Define the Second Derived Class
// The 'Motorcycle' class also inherits from the same 'Vehicle' base class.
public class Motorcycle : Vehicle
{
public bool HasSidecar { get; set; }
public void Ride()
{
Console.WriteLine($"The {Year} {Manufacturer} motorcycle is riding.");
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("--- Demonstrating Hierarchical Inheritance ---");
// 4. Create an instance of the Car class
Car myCar = new Car
{
Manufacturer = "Honda",
Year = 2022,
NumberOfDoors = 4
};
Console.WriteLine("\n--- Actions for the Car ---");
// Access inherited methods and properties
myCar.StartEngine();
myCar.StopEngine();
// Access Car-specific method and property
myCar.Drive();
Console.WriteLine($"This car has {myCar.NumberOfDoors} doors.");
Console.WriteLine("\n----------------------------------");
// 5. Create an instance of the Motorcycle class
Motorcycle myMotorcycle = new Motorcycle
{
Manufacturer = "Harley-Davidson",
Year = 2021,
HasSidecar = false
};
Console.WriteLine("\n--- Actions for the Motorcycle ---");
// Access inherited methods and properties
myMotorcycle.StartEngine();
myMotorcycle.StopEngine();
// Access Motorcycle-specific method and property
myMotorcycle.Ride();
Console.WriteLine($"Does this motorcycle have a sidecar? {myMotorcycle.HasSidecar}");
Console.WriteLine("\n--- End of Hierarchical Inheritance Example ---");
}
}
Output
--- Demonstrating Hierarchical Inheritance ---
--- Actions for the Car ---
Honda's engine starts.
Honda's engine stops.
The 2022 Honda car is driving.
This car has 4 doors.
----------------------------------
--- Actions for the Motorcycle ---
Harley-Davidson's engine starts.
Harley-Davidson's engine stops.
The 2021 Harley-Davidson motorcycle is riding.
Does this motorcycle have a sidecar? False
--- End of Hierarchical Inheritance Example ---
Multilevel Inheritance: Another derived class is the source of a derived class.

Example
using System;
// 1. Base Class (Grandparent)
// Represents a very general concept - a Device.
public class Device
{
public string Manufacturer { get; set; }
public string Model { get; set; }
public Device(string manufacturer, string model)
{
Manufacturer = manufacturer;
Model = model;
Console.WriteLine($"Device: {Manufacturer} {Model} created.");
}
public void PowerOn()
{
Console.WriteLine($"{Manufacturer} {Model} is powering on.");
}
public void PowerOff()
{
Console.WriteLine($"{Manufacturer} {Model} is powering off.");
}
}
// 2. Intermediate Derived Class (Parent)
// Inherits from Device. Represents a more specific type - a Computer.
public class Computer : Device
{
public string OperatingSystem { get; set; }
public Computer(string manufacturer, string model, string os)
: base(manufacturer, model) // Call the base class (Device) constructor
{
OperatingSystem = os;
Console.WriteLine($"Computer: {Manufacturer} {Model} with {OperatingSystem} created.");
}
public void RunProgram(string programName)
{
Console.WriteLine($"{Manufacturer} {Model} is running {programName} on {OperatingSystem}.");
}
}
// 3. Final Derived Class (Child)
// Inherits from Computer. Represents a highly specific type - a Laptop.
public class Laptop : Computer
{
public bool HasTouchscreen { get; set; }
public Laptop(string manufacturer, string model, string os, bool hasTouchscreen)
: base(manufacturer, model, os) // Call the base class (Computer) constructor
{
HasTouchscreen = hasTouchscreen;
Console.WriteLine($"Laptop: {Manufacturer} {Model} (Touchscreen: {HasTouchscreen}) created.");
}
public void CloseLid()
{
Console.WriteLine($"{Manufacturer} {Model}'s lid is closing.");
// A laptop closing its lid often means it's powering off or going to sleep
PowerOff(); // Accessing a method from the Grandparent (Device) class
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("--- Demonstrating Multilevel Inheritance ---");
// Create an instance of the lowest-level derived class (Laptop)
Laptop myLaptop = new Laptop("Dell", "XPS 15", "Windows 11", true);
Console.WriteLine("\n--- Actions for myLaptop ---");
// Access members inherited from Device (Grandparent)
Console.WriteLine($"Manufacturer: {myLaptop.Manufacturer}");
myLaptop.PowerOn();
// Access members inherited from Computer (Parent)
Console.WriteLine($"Operating System: {myLaptop.OperatingSystem}");
myLaptop.RunProgram("Visual Studio");
// Access members specific to Laptop (Child)
Console.WriteLine($"Has Touchscreen: {myLaptop.HasTouchscreen}");
myLaptop.CloseLid(); // This method internally calls PowerOff from the Device class
Console.WriteLine("\n--- End of Multilevel Inheritance Example ---");
}
}
Output
--- Demonstrating Multilevel Inheritance ---
Device: Dell XPS 15 created.
Computer: Dell XPS 15 with Windows 11 created.
Laptop: Dell XPS 15 (Touchscreen: True) created.
--- Actions for myLaptop ---
Manufacturer: Dell
Dell XPS 15 is powering on.
Operating System: Windows 11
Dell XPS 15 is running Visual Studio on Windows 11.
Has Touchscreen: True
Dell XPS 15's lid is closing.
Dell XPS 15 is powering off.
--- End of Multilevel Inheritance Example ---
Hybrid Inheritance: A mix of inheritances at any level, including hierarchical and multilevel.

Example
using System;
// 1. Base Class (Top of the Hierarchy)
public class Vehicle
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public Vehicle(string make, string model, int year)
{
Make = make;
Model = model;
Year = year;
Console.WriteLine($"Vehicle created: {Year} {Make} {Model}");
}
public void Start()
{
Console.WriteLine($"{Make} {Model} starts its engine.");
}
public void Stop()
{
Console.WriteLine($"{Make} {Model} stops its engine.");
}
}
// 2. An Interface (Defining a capability)
public interface ITowable
{
void Tow(string item);
int MaxTowingCapacity { get; }
}
// 3. First Derived Class (Hierarchical)
// Inherits from Vehicle. Represents a general car.
public class Car : Vehicle
{
public int NumDoors { get; set; }
public Car(string make, string model, int year, int numDoors)
: base(make, model, year)
{
NumDoors = numDoors;
Console.WriteLine($"Car created: {Year} {Make} {Model} ({NumDoors} doors)");
}
public void Accelerate()
{
Console.WriteLine($"{Make} {Model} is accelerating.");
}
}
// 4. Second Derived Class (Hierarchical + Interface Implementation = Hybrid)
// Inherits from Vehicle AND implements ITowable.
public class Truck : Vehicle, ITowable // Inherits from one class, implements one interface
{
public int CargoCapacityKg { get; set; }
// ITowable interface property implementation
public int MaxTowingCapacity { get; private set; } // Implements the interface property
public Truck(string make, string model, int year, int cargoCapacity, int maxTowCapacity)
: base(make, model, year)
{
CargoCapacityKg = cargoCapacity;
MaxTowingCapacity = maxTowCapacity; // Set the interface property
Console.WriteLine($"Truck created: {Year} {Make} {Model} ({CargoCapacityKg}kg capacity)");
}
// ITowable interface method implementation
public void Tow(string item) // Implements the interface method
{
Console.WriteLine($"{Make} {Model} is towing a {item} (Max: {MaxTowingCapacity} lbs).");
}
public void HaulCargo()
{
Console.WriteLine($"{Make} {Model} is hauling {CargoCapacityKg} kg of cargo.");
}
}
// 5. Third Derived Class (Another Hierarchical Branch)
// Inherits from Vehicle. Represents a motorcycle.
public class Motorcycle : Vehicle
{
public bool HasFairing { get; set; }
public Motorcycle(string make, string model, int year, bool hasFairing)
: base(make, model, year)
{
HasFairing = hasFairing;
Console.WriteLine($"Motorcycle created: {Year} {Make} {Model} (Fairing: {hasFairing})");
}
public void LeanIntoTurn()
{
Console.WriteLine($"{Make} {Model} is leaning into a turn.");
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("--- Demonstrating Hybrid Inheritance (Class Inheritance + Interface) ---");
// Create a Car (Vehicle -> Car)
Car myCar = new Car("Toyota", "Camry", 2023, 4);
myCar.Start(); // Inherited from Vehicle
myCar.Accelerate(); // Specific to Car
Console.WriteLine($"Car has {myCar.NumDoors} doors.\n");
// Create a Truck (Vehicle -> Truck, and Truck implements ITowable)
Truck myTruck = new Truck("Ford", "F-150", 2024, 1500, 10000);
myTruck.Start(); // Inherited from Vehicle
myTruck.HaulCargo(); // Specific to Truck
myTruck.Tow("boat"); // Implemented from ITowable
Console.WriteLine($"Truck's max towing capacity: {myTruck.MaxTowingCapacity} lbs.\n");
// Create a Motorcycle (Vehicle -> Motorcycle)
Motorcycle myMotorcycle = new Motorcycle("Harley-Davidson", "Iron 883", 2020, false);
myMotorcycle.Start(); // Inherited from Vehicle
myMotorcycle.LeanIntoTurn(); // Specific to Motorcycle
Console.WriteLine($"Motorcycle has fairing: {myMotorcycle.HasFairing}\n");
Console.WriteLine("--- End of Hybrid Inheritance Example ---");
}
}
Output
--- Demonstrating Hybrid Inheritance (Class Inheritance + Interface) ---
Vehicle created: 2023 Toyota Camry
Car created: 2023 Toyota Camry (4 doors)
Toyota Camry starts its engine.
Toyota Camry is accelerating.
Car has 4 doors.
Vehicle created: 2024 Ford F-150
Truck created: 2024 Ford F-150 (1500kg capacity)
Ford F-150 starts its engine.
Ford F-150 is hauling 1500 kg of cargo.
Ford F-150 is towing a boat (Max: 10000 lbs).
Truck's max towing capacity: 10000 lbs.
Vehicle created: 2020 Harley-Davidson Iron 883
Motorcycle created: 2020 Harley-Davidson Iron 883 (Fairing: False)
Harley-Davidson Iron 883 starts its engine.
Harley-Davidson Iron 883 is leaning into a turn.
Motorcycle has fairing: False
--- End of Hybrid Inheritance Example ---
Multiple Inheritance: A derived class is made up of multiple base classes. Interfaces can be used to accomplish this, however classes in C# do not support it.

You can also read Objects In C#: Creating And Using Instances Of Classes