← Foundations path
.NETFoundations 5 / 37

Inheritance

“What is inheritance in C#, and in what order do the constructors run?”

One class is built on another and gets its data and methods. Write the shared part once.

See it

1/10Before. Loan and Lease each hold their own copy of Balance and .

In plain words

Inheritance lets one class (the child) be built on another (the parent, or base class). class Loan : Contract means every Loan has everything a Contract has, plus its own extras. Building a Loan makes one object; the parent's constructor runs first, then the child's. You then call the parent's methods on that same object, with no extra .

Say it in the interview

In the lending API

The parent, one child, and the code that builds and uses it. Press Next line to walk through them.

Lending.sln · where these 3 files sit
  • src/
    • Domain/
    • Billing/
Diagram · step 3The fix. Balance and move out of Loan and Lease into one parent, . Fixed once.
Contract.cs · line 3 · 1 of 3The parent. It holds only what every kind of contract shares.

What they ask next

Interviewers check that you can explain every line. Tap a question to open it.

Traps

Read more: Microsoft Learn: inheritance · Using constructors (base)