Inheritance

A base class and a derived one

A class may derive from one other class: the colon names the base, and the derived class gets every member of it except the constructors and the private members. A derived class with nothing of its own is a renamed base, which is still useful when the name carries meaning:

var cat = new Cat("Mochi", 4); Console.WriteLine(cat.Describe()); Console.WriteLine($"legs {cat.Legs}"); class Animal { public Animal(string name, int legs) { Name = name; Legs = legs; } public string Name { get; } public int Legs { get; } public string Describe() => $"{Name} has {Legs} legs"; } class Cat : Animal { public Cat(string name, int legs) : base(name, legs) { } }

dotnet run

Mochi has 4 legs
legs 4

Cat declares no property and no method and still has Name, Legs and Describe. Its constructor does one thing: pass the arguments to base. Without that line the compiler would look for a parameterless constructor in Animal and report that there is none, because a class with a constructor of its own loses the generated one.

The constructor chain

Constructors run from the base to the derived: the base constructor finishes before the derived body starts. That order is what makes a derived constructor able to use the fields of the base, and the program prints it:

var derived = new Derived(); Console.WriteLine(derived.Describe()); class Base { protected string tag = "base"; public Base() { Console.WriteLine("base constructor"); } } class Derived : Base { public Derived() { Console.WriteLine("derived constructor"); } public string Describe() => $"tag {tag}"; }

dotnet run

base constructor
derived constructor
tag base

protected is the modifier that sits between private and public: the field is invisible outside the family, and Describe reads it. The same order holds for field initialisers: the fields of the base are set before the base constructor body, and the fields of the derived after the base constructor returns.

Upcast, is and GetType

A derived object may be used wherever the base is expected, without a cast. The reference sees the base members, and the object keeps its real type, which is what GetType reports and what a downcast needs to check:

Animal animal = new Cat("Mochi"); Console.WriteLine(animal.Describe()); Console.WriteLine($"is Cat {animal is Cat}"); Console.WriteLine($"type {animal.GetType().Name}"); if (animal is Cat cat) { Console.WriteLine($"cast name {cat.Name}"); } class Animal { public Animal(string name) => Name = name; public string Name { get; } public string Describe() => $"{Name} is an animal"; } class Cat : Animal { public Cat(string name) : base(name) { } }

dotnet run

Mochi is an animal
is Cat True
type Cat
cast name Mochi

Describe printed the base text even though the object is a Cat, because the method is not virtual: the compiler bound the call to the base implementation. The next two lessons make that method virtual and show what changes; the type checks here work the same either way.

What is inherited

Exercises

  1. Add a Dog class next to Cat and put both in an array of Animal.
  2. Remove the : base(name, legs) line and read the error.
  3. Make the tag field private and see what Describe reports.
  4. Cast an Animal that is not a Cat with as Cat and print the result.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. In which order do the constructors of a base and a derived class run?

    Answer

    Base first, then derived

    The base constructor must finish before the derived body can use the base state.

  2. Which members does a derived class not inherit?

    Answer

    The constructors and the private members

    Constructors are not inherited, and private members are not accessible though they exist.

  3. What does this program print? Animal a = new Cat(); Console.WriteLine(a.GetType().Name); class Animal { } class Cat : Animal { }

    Answer

    Cat

    GetType reports the runtime type of the object, which is Cat.

  4. What does protected allow?

    Answer

    Access from the class and from classes derived from it

    protected sits between private and public for the family of the type.

  5. What happens when a derived constructor does not call base and the base has no parameterless constructor?

    Answer

    A compile error: no constructor takes zero arguments

    The compiler inserts a base() call, and the base class has no such constructor to call.

  6. How many base classes may a C# class have?

    Answer

    One

    Single class inheritance, with any number of interfaces.

  7. What does this program print? var d = new D(); Console.WriteLine(d.N); class B { public int N = 1; } class D : B { public D() { N = 2; } }

    Answer

    2

    The derived constructor writes the inherited field, so the read gives 2.

  8. What does an upcast from Cat to Animal need?

    Answer

    Nothing, it is implicit

    A derived reference is always usable as the base type, so no cast is written.

  9. Which of these methods comes from System.Object?

    Answer

    ToString

    Every type inherits ToString, Equals, GetHashCode and GetType from object.

  10. What is the result of animal is Cat when animal holds a Cat object?

    Answer

    True

    is tests the runtime type, so a Cat object passes even through an Animal reference.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev