← Foundations path
.NETFoundations 7 / 37

Abstract class and method

“What is an abstract class? Why can't you create one, and why does it still have a constructor?”

A parent class that is only a starting point. You cannot build it on its own, and it can force every child to write certain methods.

See it

1/8Before. Contract is a normal class, so new Contract(500) builds fine.

In plain words

Mark a class when it only makes sense as a parent. The compiler then refuses on it, so a "contract that is no kind of contract" can never exist. An method has no body; every child must it or fail to build. The abstract class can still hold shared data and a constructor, because every child object has a parent part that needs setting up.

Say it in the interview

In the lending API

The abstract parent, a child that compiles, and the lines that do not. Press Next line to walk through them.

Lending.sln · where these 3 files sit
  • src/
    • Domain/
    • Billing/
Diagram · step 3The fix: abstract class Contract, and becomes : a method with no body.
Contract.cs · line 3 · 1 of 3: this class is only a parent. new Contract(...) will not compile.

What they ask next

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

Traps

Read more: Microsoft Learn: abstract · Abstract and sealed classes