Curriculum
Inheritance in C# is one of the four fundamental pillars of Object-Oriented Programming (OOP). Inheritance in C# allows one class to acquire the properties, methods, and behaviors of another class. Inheritance in C# helps developers promote code reusability, reduce duplication, improve maintainability, and build scalable applications. Every professional .NET developer uses Inheritance in C# while developing ASP.NET Core Applications, MVC Projects, Web APIs, Desktop Applications, Cloud Solutions, Mobile Applications, and Enterprise Software Systems.
Understanding Inheritance in C# is essential because it enables developers to establish relationships between classes and create hierarchical software architectures.
Inheritance in C# is a mechanism where one class inherits members from another class.
The class that provides members is called:
Base Class
Parent Class
Super Class
The class that receives members is called:
Derived Class
Child Class
Sub Class
Inheritance promotes reusability by allowing child classes to use existing functionality from parent classes.
Inheritance in C# helps developers:
Most enterprise applications use inheritance extensively.
Consider:
Vehicle
Different vehicle types:
Car
Bike
Bus
Truck
All vehicles share common properties:
Brand
Color
Speed
Instead of repeating these properties in every class, they can be inherited from a common Vehicle class.
This is the primary purpose of Inheritance in C#.
Example:
public class Vehicle
{
public string Brand;
}
public class Car : Vehicle
{
}
The colon (:) symbol indicates inheritance.
Here:
Car
inherits from:
Vehicle
Example:
public class Animal
{
public void Eat()
{
Console.WriteLine(
"Animal is Eating");
}
}
The Animal class contains a common method.
Example:
public class Dog : Animal
{
}
Dog automatically inherits all public members from Animal.
Example:
Dog dog =
new Dog();
dog.Eat();
Output:
Animal is Eating
The Dog object can access methods from the Animal class.
This demonstrates inheritance in action.
Example:
public class Person
{
public string Name;
}
Derived Class:
public class Student : Person
{
}
Usage:
Student student =
new Student();
student.Name = "Rahul";
Console.WriteLine(
student.Name);
Output:
Rahul
The Name property is inherited from Person.
Example:
Person
|
Student
Parent Class:
Person
Child Class:
Student
The child class inherits all accessible members from the parent class.
Example:
public class Person
{
public string Name;
}
public class Student : Person
{
public int RollNumber;
}
Usage:
Student student =
new Student();
student.Name = "Rahul";
student.RollNumber = 101;
Output:
Rahul
101
The child class contains both inherited and custom members.
One child class inherits from one parent class.
Example:
Person
|
Student
Example:
public class Person
{
}
public class Student : Person
{
}
This is the most common type of inheritance.
A child class becomes a parent for another class.
Example:
Person
|
Student
|
GraduateStudent
Example:
public class Person
{
}
public class Student : Person
{
}
public class GraduateStudent : Student
{
}
Members flow through multiple levels.
Multiple child classes inherit from the same parent class.
Example:
Person
/ \
Student Teacher
Example:
public class Person
{
}
public class Student : Person
{
}
public class Teacher : Person
{
}
This structure is widely used in enterprise applications.
C# does not support multiple inheritance using classes.
Example (Invalid):
public class Student :
Teacher, Employee
{
}
Reason:
Diamond Problem
Multiple inheritance can create ambiguity.
Instead, C# uses Interfaces to achieve multiple inheritance behavior.
Protected members are accessible within child classes.
Example:
public class Person
{
protected string Name;
}
Derived Class:
public class Student : Person
{
public void SetName()
{
Name = "Rahul";
}
}
Protected access improves encapsulation while supporting inheritance.
Parent constructors execute before child constructors.
Example:
public class Person
{
public Person()
{
Console.WriteLine(
"Person Constructor");
}
}
public class Student : Person
{
public Student()
{
Console.WriteLine(
"Student Constructor");
}
}
Output:
Person Constructor
Student Constructor
Constructor execution follows inheritance hierarchy.
Example:
public class Employee
{
public void Display()
{
Console.WriteLine(
"Employee Details");
}
}
Derived Class:
public class Manager :
Employee
{
}
Usage:
Manager manager =
new Manager();
manager.Display();
Output:
Employee Details
Methods are inherited automatically.
Reuse existing code without duplication.
Changes in parent classes automatically affect child classes.
Applications can expand easily.
Developers write less code.
Class hierarchies improve structure.
These benefits make Inheritance in C# essential for enterprise software.
Account
|
SavingsAccount
CurrentAccount
LoanAccount
Person
|
Student
Teacher
Admin
Person
|
Doctor
Patient
Nurse
Product
|
Electronics
Books
Clothing
Inheritance helps model real-world relationships effectively.
Do not use inheritance when no “is-a” relationship exists.
Avoid excessive inheritance levels.
Use protected members carefully.
Reuse parent functionality whenever possible.
Inheritance in C# is heavily used in:
A strong understanding of Inheritance in C# is necessary for mastering Object-Oriented Programming and professional .NET development.
Inheritance is a mechanism that allows one class to acquire members from another class.
A Parent Class is the class whose members are inherited by another class.
A Child Class is the class that inherits members from a Parent Class.
Multiple Inheritance through classes is not supported to avoid ambiguity and the Diamond Problem.
Inheritance improves code reusability, maintainability, scalability, and software organization.
Inheritance helps developers build scalable and reusable enterprise software applications.
WhatsApp us