codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

19 / 60 topics
17Classes and Objects18Constructors in C#19Inheritance in C#20Polymorphism in C#21Encapsulation in C#22Access Modifiers in C#23Properties in C#24Indexers in C#25Events in C#26Delegates in C#
Tutorials/C# Programming/Inheritance in C#
🔷C# Programming

Inheritance in C#

Updated 2026-05-15
10 min read

Inheritance in C#

Introduction

In object-oriented programming (OOP), inheritance is a fundamental concept that allows one class to inherit the properties and methods of another class. This promotes code reusability, reduces redundancy, and helps in creating a hierarchical classification of classes.

In this tutorial, we'll explore how to use inheritance in C# by extending existing classes and inheriting their properties and behaviors. We'll cover both basic and advanced aspects of inheritance, making it suitable for beginners as well as intermediate developers.

Concept

What is Inheritance?

Inheritance is a mechanism where a new class (derived or child class) inherits the attributes and behaviors (methods) from an existing class (base or parent class). This allows the derived class to reuse the code of the base class, reducing duplication and promoting a clean and organized code structure.

Why Use Inheritance?

  1. Code Reusability: You can define common properties and methods in a base class and let multiple derived classes inherit them.
  2. Hierarchical Classification: Classes can be organized in a hierarchy, making it easier to manage complex systems.
  3. Polymorphism: Inheritance is closely related to polymorphism, which allows objects of different classes to be treated as objects of a common superclass.

Types of Inheritance

C# supports single inheritance, meaning a class can inherit from only one base class. However, it supports multiple interfaces, allowing a class to implement multiple behaviors.

Examples

Let's dive into some practical examples to understand how inheritance works in C#.

Basic Example

Suppose we have a Vehicle class that represents a generic vehicle with properties like Make, Model, and Year. We can create a derived class called Car that inherits from Vehicle.

// Base class
public class Vehicle
{
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

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

// Derived class
public class Car : Vehicle
{
    public string Color { get; set; }

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

In this example, the `Car` class inherits from the `Vehicle` class. It automatically gains all the properties and methods of `Vehicle`, including `Make`, `Model`, `Year`, and `DisplayInfo()`. Additionally, it has its own property `Color` and method `StartEngine()`.

### Using Inherited Properties and Methods

Here's how you can use the `Car` class:

```csharp
public class Program
{
    public static void Main()
    {
        Car myCar = new Car();
        myCar.Make = "Toyota";
        myCar.Model = "Corolla";
        myCar.Year = 2023;
        myCar.Color = "Red";

        myCar.DisplayInfo(); // Inherited method from Vehicle
        myCar.StartEngine();  // Method specific to Car
    }
}

<OutputBlock>
{`Make: Toyota, Model: Corolla, Year: 2023  
Engine started.`}
</OutputBlock>

### Overriding Methods

You can override methods in the derived class to provide a specific implementation. For example, let's override the `DisplayInfo()` method in the `Car` class:

```csharp
public class Car : Vehicle
{
    public string Color { get; set; }

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

    // Overriding the DisplayInfo method
    public override void DisplayInfo()
    {
        base.DisplayInfo();
        Console.WriteLine($"Color: {Color}");
    }
}

Now, when you call `DisplayInfo()` on a `Car` object, it will display both the vehicle information and the color:

```csharp
public class Program
{
    public static void Main()
    {
        Car myCar = new Car();
        myCar.Make = "Toyota";
        myCar.Model = "Corolla";
        myCar.Year = 2023;
        myCar.Color = "Red";

        myCar.DisplayInfo(); // Overridden method in Car
        myCar.StartEngine();  // Method specific to Car
    }
}
Output
Make: Toyota, Model: Corolla, Year: 2023  
Color: Red  
Engine started.

Protected Members

You can also use protected access modifiers in the base class to allow derived classes to access certain members without exposing them to the outside world.

public class Vehicle
{
    public string Make { get; set; }
    protected string Model { get; set; } // Protected member
    public int Year { get; set; }

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

public class Car : Vehicle
{
    public string Color { get; set; }

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

    // Accessing protected member from derived class
    public void ShowModel()
    {
        Console.WriteLine($"Model: {Model}");
    }
}

In this example, the Model property is marked as protected, allowing it to be accessed within the Car class.

Abstract Classes and Methods

C# also supports abstract classes and methods, which must be implemented by any non-abstract derived class.

public abstract class Vehicle
{
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    // Abstract method
    public abstract void StartEngine();

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

public class Car : Vehicle
{
    public string Color { get; set; }

    // Implementing the abstract method
    public override void StartEngine()
    {
        Console.WriteLine("Engine started.");
    }
}

In this example, Vehicle is an abstract class with an abstract method StartEngine(). The Car class must implement this method.

What's Next?

Now that you understand inheritance in C#, the next step is to explore Polymorphism in C#. Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling more flexible and dynamic code. Stay tuned for our next tutorial on polymorphism!

If you have any questions or need further clarification, feel free to ask in the comments below!


PreviousConstructors in C#Next Polymorphism in C#

Recommended Gear

Constructors in C#Polymorphism in C#