Roadmap › OOP Concepts in C# › Module: The Four Pillars
Lessons in this course (15)

7. Inheritance

Basic★ Must knowLesson 7 of 15

A derived class inherits members of a base class and can extend or override them. C# supports single class inheritance and multiple interface implementation.

csharp
public abstract class Employee
{
    public string Name { get; }
    protected Employee(string name) => Name = name;
    public abstract decimal MonthlyPay();                       // must override
    public virtual string Describe() => $"{Name}: {MonthlyPay():C}";  // may override
}

public class Permanent(string name, decimal salary) : Employee(name)
{
    public override decimal MonthlyPay() => salary / 12;
}

public sealed class Contractor(string name, decimal rate, int hours) : Employee(name)
{
    public override decimal MonthlyPay() => rate * hours;
    public override string Describe() => base.Describe() + " (contract)";
}

Types of inheritance: single, multilevel (A→B→C), hierarchical (B and C from A). Multiple class inheritance is not allowed (avoids the diamond problem); use interfaces instead.