Inheritance in C#

Last Updated : 22 Oct, 2026

Inheritance is a fundamental concept of Object-Oriented Programming (OOP) that allows a class to acquire accessible fields, properties, and methods from another class. It helps developers reuse existing code and establish relationships between classes.

  • A derived class (child class) inherits accessible members from a base class (parent class).
  • A derived class can add new functionality or override inherited virtual or abstract methods.
  • C# uses the colon (:) symbol to specify inheritance.

Example : Program to demonstrates how multiple classes inherit from a common base class and provide their own implementations of an inherited method.

C#
using System;

// Base class
class Animal{
    public virtual void Sound(){
        Console.WriteLine("Animal makes a sound");
    }
}

// Derived class
class Dog : Animal{
    public override void Sound(){
        Console.WriteLine("Dog barks");
    }
}

// Derived class
class Cat : Animal{
    public override void Sound(){
        Console.WriteLine("Cat meows");
    }
}

// Derived class
class Cow : Animal{
    public override void Sound(){
        Console.WriteLine("Cow moos");
    }
}

class Geeks{
    static void Main(){
        Animal a;

        a = new Dog();
        a.Sound();

        a = new Cat();
        a.Sound();

        a = new Cow();
        a.Sound();
    }
}

Output
Dog barks
Cat meows
Cow moos

Explanation:

  • Animal is the base class containing the virtual method Sound().
  • Dog, Cat, and Cow inherit from Animal and override Sound() with their own implementations.
  • The Animal reference stores objects of different derived classes.
  • When Sound() is called, C# executes the overridden method based on the actual object type, demonstrating runtime polymorphism.
animal_class
Inheritance of C#

Syntax: A class inherits from another class using the colon (:) symbol.

class Parent
{
// Fields, properties, and methods
}
class Child : Parent
{
// Additional members
}

Here, Parent is the base class, while Child is the derived class.

Key Terminologies

Term

Description

Class

Blueprint used to create objects.

Base Class

Class whose members are inherited by another class.

Derived Class

Class that inherits from a base class.

:

Symbol used to specify inheritance or interface implementation.

virtual

Modifier that allows a method or property to be overridden.

override

Modifier used to override an inherited virtual or abstract member.

base

Keyword used to access base-class members or invoke its constructor.

Types of Inheritance

C# supports single, multilevel, and hierarchical class inheritance. Multiple inheritance of classes is not supported, but a class can implement multiple interfaces.

types_of_inheritance
types of Inheritance

The following four types are commonly discussed in C# inheritance.

1. Single Inheritance

Single inheritance occurs when a derived class inherits from one base class. It allows the derived class to reuse the functionality of its parent class.

single_inheritance
Single Inheritance
C#
using System;

// Base class
class Vehicle
{
    public Vehicle()
    {
        Console.WriteLine("This is a Vehicle");
    }
}

// Derived class
class Car : Vehicle
{
    public Car()
    {
        Console.WriteLine("This Vehicle is Car");
    }
}

class Geeks
{
    static void Main()
    {
        Car obj = new Car();
    }
}

Output
This is a Vehicle
This Vehicle is Car

Explanation: Car inherits from Vehicle. When a Car object is created, the Vehicle constructor executes first, followed by the Car constructor.

2. Multilevel Inheritance

Multilevel inheritance occurs when a derived class inherits from another derived class, creating a chain of inheritance.

multilevel_inheritance
Multileel Inheritance

Example:

C#
using System;

class Vehicle{
    public Vehicle(){
        Console.WriteLine("This is a Vehicle");
    }
}

class FourWheeler : Vehicle{
    public FourWheeler(){
        Console.WriteLine("4 Wheeler Vehicles");
    }
}

class Car : FourWheeler{
    public Car(){
        Console.WriteLine("This 4 Wheeler Vehicle is a Car");
    }
}

class Geeks{
    static void Main(){
        Car obj = new Car();
    }
}

Output
This is a Vehicle
4 Wheeler Vehicles
This 4 Wheeler Vehicle is a Car

Explanation: Car inherits from FourWheeler, which inherits from Vehicle. Creating a Car object invokes constructors in the order Vehicle -> FourWheeler -> Car.

3. Hierarchical Inheritance

Hierarchical inheritance occurs when multiple derived classes inherit from the same base class. Each derived class can extend the base-class functionality independently.

hierarchical_inheritance
Hierarchy Inheritance
C#
using System;

class Vehicle{
    public Vehicle(){
        Console.WriteLine("This is a Vehicle");
    }
}

class Car : Vehicle{
    public Car(){
        Console.WriteLine("This Vehicle is Car");
    }
}

class Bus : Vehicle{
    public Bus(){
        Console.WriteLine("This Vehicle is Bus");
    }
}

class Geeks{
    static void Main(){
        Car obj1 = new Car();
        Bus obj2 = new Bus();
    }
}

Output
This is a Vehicle
This Vehicle is Car
This is a Vehicle
This Vehicle is Bus

Explanation: Both Car and Bus inherit from Vehicle. Each derived class invokes the Vehicle constructor before executing its own constructor.

4. Multiple Inheritance Through Interfaces

C# does not allow a class to inherit from more than one base class. However, a class can implement multiple interfaces, allowing it to provide functionality defined by different interfaces.

multiple_inheritance
Multiple Inheritance through Interfaces
C#
using System;

interface ILandVehicle{
    void LandInfo();
}

interface IWaterVehicle{
    void WaterInfo();
}

// Implements both interfaces
class AmphibiousVehicle : ILandVehicle, IWaterVehicle{
    public AmphibiousVehicle(){
        Console.WriteLine("This is an AmphibiousVehicle");
    }

    public void LandInfo(){
        Console.WriteLine("This is a LandVehicle");
    }

    public void WaterInfo(){
        Console.WriteLine("This is a WaterVehicle");
    }
}

class Geeks{
    static void Main(){
        AmphibiousVehicle obj = new AmphibiousVehicle();

        obj.WaterInfo();
        obj.LandInfo();
    }
}

Explanation: AmphibiousVehicle implements both ILandVehicle and IWaterVehicle. It provides implementations for the methods declared in each interface.

Note: Implementing multiple interfaces is not the same as inheriting from multiple classes. A C# class can inherit from only one direct base class but can implement multiple interfaces.

IS-A Relationship

The IS-A relationship represents inheritance, where a derived class is a specialized type of its base class.

For example, a Dog IS-A Animal, and a Cat IS-A Animal.

C#
using System;

class Animal{
}

class Dog : Animal{
}

class Cat : Animal{
}

class Puppy : Dog{
}

class Geeks{
    static void Main(){
        Animal a = new Animal();
        Dog d = new Dog();
        Cat c = new Cat();
        Puppy p = new Puppy();

        Console.WriteLine(a is Animal);
        Console.WriteLine(d is Animal);
        Console.WriteLine(c is Animal);
        Console.WriteLine(p is Dog);
    }
}

Output
True
True
True
True

Explanation:

  • Dog and Cat inherit directly from Animal.
  • Puppy inherits from Dog and indirectly from Animal.
  • The is operator checks whether an object is compatible with a specified type.

What Can Be Done in a Derived Class?

A derived class can reuse, extend, or customize the functionality provided by its base class.

  • Access Inherited Members: Use accessible fields, properties, and methods defined in the base class.
  • Add New Members: Declare additional fields, properties, and methods specific to the derived class.
  • Override Methods: Provide new implementations of inherited virtual or abstract methods using override.
  • Hide Members: Use the new keyword to explicitly hide inherited members.
  • Access Base-Class Members: Use the base keyword to call accessible base-class methods or properties.
  • Invoke Base-Class Constructors: Use base() in a constructor initializer to invoke a particular base-class constructor.

Note: Constructors are not inherited. Also, private members of a base class cannot be accessed directly from a derived class, although they remain part of the object's base-class state.

Advantages of Inheritance

  • Code Reusability: Allows derived classes to reuse existing functionality, reducing code duplication.
  • Extensibility: Makes it possible to add specialized functionality without changing the base class.
  • Runtime Polymorphism: Allows derived classes to override virtual or abstract members and provide different implementations.
  • Class Hierarchy: Organizes related classes into meaningful relationships, making code easier to understand and manage.

Limitations of Inheritance

  • Tight Coupling: Changes to a base class can affect its derived classes.
  • Increased Complexity: Deep inheritance hierarchies can make code harder to understand, debug, and maintain.
  • No Multiple Class Inheritance: A C# class cannot directly inherit from more than one base class.
  • Reduced Flexibility: Inheritance creates a fixed type relationship, which may be unsuitable when behavior needs to change independently.
Comment

Explore