Inheritance in C#
Learn the fundamentals of inheritance in C#, including base and derived classes, with clear examples.
What is Inheritance?
Inheritance is an object-oriented concept that allows a class, called the derived class, to acquire the properties and behaviors of another class, called the base class.
Base and Derived Classes
A derived class is created using a colon followed by the base class name, and it automatically gains access to the non-private members of that base class.
The base Keyword
The base keyword is used inside a derived class to access members of the base class, such as calling a base class constructor or an overridden method.
Benefits of Inheritance
Inheritance promotes code reuse and establishes a natural hierarchy between related classes, making it easier to extend and maintain large codebases.
A Complete Example: Base and Derived Classes
public class Employee
{
public string Name { get; }
protected decimal BaseSalary;
public Employee(string name, decimal baseSalary)
{
Name = name;
BaseSalary = baseSalary;
}
public virtual decimal MonthlyPay() => BaseSalary;
public override string ToString() => $"{Name}: {MonthlyPay()}";
}
public class Manager : Employee
{
private decimal allowance;
public Manager(string name, decimal baseSalary, decimal allowance)
: base(name, baseSalary) // runs Employee's constructor first
{
this.allowance = allowance;
}
public override decimal MonthlyPay() => base.MonthlyPay() + allowance;
}
Usage:
List<Employee> staff = new()
{
new Employee("Kiran", 40000m),
new Manager("Meera", 60000m, 15000m)
};
foreach (var person in staff)
Console.WriteLine(person); // Kiran: 40000, then Meera: 75000
The list is typed as Employee, yet the manager's own MonthlyPay runs. This is polymorphism: the method that executes depends on the object's real type, not the variable's type.
virtual, override, sealed and abstract
| Keyword | Meaning |
|---|---|
virtual | The base method may be replaced in a derived class |
override | The derived class provides its own version of a virtual or abstract method |
abstract | The class cannot be created directly; abstract methods have no body and must be overridden |
sealed | A sealed class cannot be inherited; a sealed override cannot be overridden further |
new (on a member) | Hides a base member instead of overriding it |
The difference between overriding and hiding matters. With override, a base-typed variable still calls the derived version. With new, the base version runs whenever the variable is typed as the base class, which surprises many beginners.
Inheritance Types and Design Choices
Types C# supports
- Single: one class inherits from one base class.
- Multilevel: a chain, such as Employee to Manager to RegionalManager.
- Hierarchical: several classes inherit from the same base.
C# does not allow a class to inherit from more than one class. To combine abilities from several sources, a class can implement multiple interfaces, for example class Report : IPrintable, IExportable. Every class ultimately inherits from System.Object, which is why methods like ToString() are always available.
When to inherit and when to compose
Use inheritance when there is a genuine "is a" relationship and the derived class can be used anywhere the base is expected. A Manager is an Employee, so it fits. If the link is "has a", such as a Car having an Engine, give the car an engine field instead. This is called composition, and it keeps classes loosely connected.
Common Mistakes
- Forgetting to mark a base method
virtual, then wondering why the override does not compile. - Calling virtual methods from a base constructor, where derived fields are not yet set.
- Building deep hierarchies that are hard to change; two or three levels is usually plenty.
- Making fields public in the base class instead of using
protectedmembers or properties.