Inheritance
Inheritance models an is-a relationship and should be used when derived objects genuinely satisfy the base contract.
How it works
Reuse and specialize behavior through a base class.
The base constructor initializes shared state. The derived class overrides a virtual method to specialize behavior.
Runnable C# example
Program.cs
using System;
class Employee(string name)
{
public string Name { get; } = name;
public virtual string Describe() => Name;
}
class Developer(string name, string language) : Employee(name)
{
public override string Describe() => $"{Name} writes {language}";
}
class Program
{
static void Main() => Console.WriteLine(new Developer("Ada", "C#").Describe());
}
Ada writes C#Practice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.