OOP/OOD  

Understanding Inheritance in C# with Practical Project Examples

Introduction

While developing applications in C#, we often notice that many classes share similar data and logic.
Writing the same code repeatedly not only increases file size but also makes maintenance difficult.

This problem is solved using Inheritance, one of the most powerful concepts of Object-Oriented Programming (OOP).
Inheritance allows us to build new classes based on existing ones, making applications cleaner and easier to manage.

This article explains Inheritance in C# using real-life and real-project examples, not textbook definitions.

What is Inheritance in C#

Inheritance is a mechanism in C# where one class automatically acquires the properties and methods of another class.

  • The existing class is called the Base (Parent) class

  • The new class is called the Derived (Child) class

The child class can:

  • Use parent class members

  • Add new features

  • Extend existing functionality

Why Inheritance Is Needed

Inheritance helps developers to:

  • Reduce duplicate code

  • Improve reusability

  • Create a clear hierarchy

  • Simplify maintenance

  • Follow real-world design patterns

In large applications, inheritance is not optional—it is essential.

Real-Time Scenario: Vehicle System

Consider a real-world vehicle system.

Different vehicles such as cars, bikes, and buses have many common characteristics:

  • They have wheels

  • They belong to a brand

  • They can start and stop

Instead of repeating this logic in every class, we store it in one base class.

Base Class: Vehicle

public class Vehicle
{
    public int Wheels;
    public string Brand;

    public void Start()
    {
        Console.WriteLine("Vehicle started");
    }

    public void Stop()
    {
        Console.WriteLine("Vehicle stopped");
    }
}

This class defines common behavior shared by all vehicles.

Child Class: Car

public class Car : Vehicle
{
    public void OpenSunroof()
    {
        Console.WriteLine("Sunroof opened");
    }
}

Child Class: Bike

public class Bike : Vehicle
{
    public void KickStart()
    {
        Console.WriteLine("Bike kick started");
    }
}

Using the Child Classes

Car myCar = new Car();
myCar.Brand = "BMW";
myCar.Wheels = 4;
myCar.Start();          // inherited
myCar.OpenSunroof();    // own feature

Bike myBike = new Bike();
myBike.Wheels = 2;
myBike.Start();         // inherited
myBike.KickStart();     // own feature

What Happens Internally?

  • Car and Bike automatically receive Start(), Stop(), Brand, and Wheels

  • No duplicate code is written

  • Each child class adds only what is unique

This is the true power of inheritance.

Real-Time Scenario: Employee Management System

In company applications, employees belong to different categories:

  • Full-time employees

  • Part-time employees

  • Interns

Although their work type differs, basic employee details remain the same.

Base Class: Employee

public class Employee
{
    public int EmployeeId;
    public string Name;

    public void ShowDetails()
    {
        Console.WriteLine($"ID: {EmployeeId}, Name: {Name}");
    }
}

Child Class: Full-Time Employee

public class FullTimeEmployee : Employee
{
    public int MonthlySalary;
}

Child Class: Part-Time Employee

public class PartTimeEmployee : Employee
{
    public int HourlyRate;
}

Usage Example

FullTimeEmployee emp1 = new FullTimeEmployee();
emp1.EmployeeId = 101;
emp1.Name = "Sandhiya";
emp1.MonthlySalary = 30000;
emp1.ShowDetails(); // inherited

PartTimeEmployee emp2 = new PartTimeEmployee();
emp2.EmployeeId = 102;
emp2.Name = "Priya";
emp2.HourlyRate = 400;
emp2.ShowDetails(); // inherited

Inheritance in Real .NET Projects

Inheritance is heavily used in ASP.NET and .NET Core applications.

1. Controller Inheritance

BaseController
   ├── AccountController
   ├── ProductController
   └── ReportController

Common logic:

  • Authentication

  • Logging

  • Error handling

2. Model Inheritance

BaseEntity
   ├── User
   ├── Product
   └── Order

Common properties:

  • CreatedDate

  • ModifiedDate

  • IsActive

3. Service Layer Inheritance

BaseService
   ├── UserService
   ├── OrderService
   └── PaymentService

Common logic:

  • Database access

  • Validation

  • Transactions

This structure improves code quality and scalability.

Important Rules of Inheritance in C#

  • A child class can inherit only one base class

  • Private members are not accessible in child classes

  • Public and protected members can be inherited

  • Constructors are not inherited

  • Inheritance represents an “IS-A” relationship

Example:

  • Car is a Vehicle

  • Employee is an Entity

Common Mistakes Beginners Make

  • Using inheritance where composition is better

  • Creating deep inheritance chains

  • Forgetting access modifiers

  • Putting too much logic in base classes

Understanding the purpose of inheritance avoids these mistakes.

Summary Table

ConceptDescription
Base ClassContains common code
Derived ClassExtends base class
ReusabilityAvoids duplicate logic
MaintenanceEasier to update
Project UsageControllers, Models, Services

Conclusion

Inheritance is a fundamental concept that helps build structured, reusable, and maintainable C# applications.
By placing shared logic in a base class and extending it through child classes, developers can write clean and professional code.

Once inheritance is understood properly, learning advanced topics such as polymorphism, abstraction, and design patterns becomes much easier.