# Summary

Abstraction is a goal; the abstract keyword is one C# tool for reaching it. Choose an ordinary, virtual or abstract method by whether derived classes keep, may change or must write it.

Journeyman · C# · OOP · Updated November 15, 2026

By Gregor Dzierzon

## After this lesson you can

- Choose an ordinary, virtual or abstract method for a base class member, by whether derived classes keep it, may change it or must write it

## Key takeaways

- Abstraction is a goal; an `abstract` class is one C# tool for reaching it.
- Make a member ordinary when every kind does it the same way, `virtual` when a few kinds differ, and `abstract` when every kind does it differently.
- Ask "could I sensibly create one of these?" If not, make the class abstract: `Character` stays concrete, `BankAccount` is abstract.

**Abstraction** means using an object through only what matters, and hiding the rest. Base classes are one way to get there: the more general a class, the more abstract it is. When a class is so general that an object of just that class makes no sense, mark it **`abstract`**: C# then won't let anyone create one, and makes every specific kind finish it.

So *abstraction* is an idea, a goal for code that's easy to use and to change. An `abstract` class is one C# tool for reaching it. (The next topic brings a second tool: interfaces.)

## Three kinds of base class member

A base class decides, member by member, how much freedom its derived classes get:

| The member is | Derived classes | Use it when | Example |
|---|---|---|---|
| Ordinary | Keep it as it is | Every kind does it the same way | `Deposit` adds to the balance, for any account. |
| `virtual` | **May** override it | Most kinds share one way, a few differ | `LevelUp` is the same for most characters; an archer adds an arrow. |
| `abstract` | **Must** override it | Every kind does it differently, with no sensible default | `Withdraw` has a different rule for each kind of account. |

## Abstract class or concrete base class?

Ask: *could I sensibly create one of these?*

- `new Character("Villager")`: yes, a villager is a fine character. `Character` stays concrete.
- `new BankAccount("1001", 50m)`: no, the bank would ask *"What kind?"*. `BankAccount` is abstract.
- `new Shape()`: no, a shape with no kind has no area. `Shape` is abstract, with an abstract `Area()`.

## Do it: make Shape abstract

A drawing app works out how much paint each shape needs from its area. Right now `Shape` is an ordinary class with a `virtual` `Area()` that returns `0`, and `Rectangle` and `Triangle` forgot to override it. So every shape says it needs no paint.

Every kind of shape works out its area differently, and a shape of no kind has no area at all. That's the third row of the table above: make it `abstract`, and the compiler makes every kind write its own `Area()`.

The two kinds of shape each get two values when they're created:

- `Rectangle(double width, double height)`: its area is width × height.
- `Triangle(double baseWidth, double height)`: its area is half of base × height.

For example, a 4 by 3 rectangle and a triangle with a base of 4 and a height of 3:

| Shape | Area |
|---|---|
| `new Rectangle(4, 3)` | 4 × 3 = `12` |
| `new Triangle(4, 3)` | 4 × 3 ÷ 2 = `6` |

So `new Rectangle(4, 3).Area()` returns `12`.

**Your job:** change `Shape` and finish the two kinds of shape.

- Make `Shape` an `abstract` class.
- Make `Area()` an `abstract` method that returns a `double`, with no body.
- In `Rectangle`, override `Area()` to return `width * height`.
- In `Triangle`, override `Area()` to return `baseWidth * height / 2`.

Practiced live on the lesson page: write the C# right there and check it.

## Check yourself

1 question to try on the lesson page.

## So what

Every base class member is a choice: keep it, may change it, must write it. Make that choice on purpose, and the compiler will hold every derived class to it.

## Key terms

- [Abstraction](https://www.ask-codey.com/glossary/abstraction)
- [Virtual Method](https://www.ask-codey.com/glossary/virtual-method)

## Cheat sheets

- [Summary](https://www.ask-codey.com/cheat-sheets/abstraction-in-csharp-summary-cheat-sheet)

Source: https://www.ask-codey.com/lessons/abstraction-in-csharp-summary
