Skip to content
Codewilds

Lesson 25 of 34 · Inheritance & Interfaces

C# Abstract Classes and Abstract Methods

medium20 XP2 min read

Some classes exist only to be built upon. What is the area of "a shape"? There is no sensible answer until you know whether it is a circle or a rectangle. Yet every shape has an area, and code that works with shapes should be able to ask for it. Abstract classes express exactly that: a common base that defines what every derived class must provide, without being complete on its own.

Abstract classes and abstract methods

public abstract class Enemy
{
    public string Name { get; }

    protected Enemy(string name) => Name = name;

    public abstract int Damage();              // no body: every enemy must define it

    public string Threat() => $"{Name} hits for {Damage()}";   // normal method
}
  • An abstract class cannot be instantiated: new Enemy("x") does not compile. It only exists as a base class.
  • An abstract method has no body, just a semicolon. It is implicitly virtual, and every non-abstract derived class must override it — otherwise the compiler reports an error.
  • An abstract class can still contain fields, properties, constructors and fully implemented methods.

Implementing the abstract members

public class Goblin : Enemy
{
    public Goblin() : base("Goblin") { }
    public override int Damage() => 4;
}

public class Dragon : Enemy
{
    public int Age { get; }
    public Dragon(int age) : base("Dragon") => Age = age;
    public override int Damage() => 50 + Age / 10;
}

Enemy e = new Dragon(300);
Console.WriteLine(e.Threat());   // Dragon hits for 80

The constructor of Enemy is protected: only derived classes can call it, which documents that nobody else should.

The template method pattern

Look at Threat() again: it is written once in the base class, but calls Damage(), which each derived class supplies. The base class defines the algorithm, the derived classes fill in the details. This is the template method pattern, and it is one of the main reasons to use abstract classes:

public abstract class Report
{
    public string Render() => Header() + "\n" + Body() + "\n-- end --";
    protected virtual string Header() => "REPORT";
    protected abstract string Body();
}

Header() is virtual (a default that may be overridden), Body() is abstract (must be provided).

abstract vs virtual

virtual abstract
Has a body in the base class yes no
Derived classes must override no yes
Allowed in a normal class yes no — only in abstract classes

Common mistakes

  • Instantiating the abstract class: new Shape() is a compile error — create a concrete shape.
  • Giving an abstract method a body or forgetting override in the derived class.
  • Forgetting to implement a member: the error "does not implement inherited abstract member" tells you which one.

Assignment

  1. Make Shape abstract, and turn Area() and Perimeter() into abstract methods. Keep Name (virtual) and Describe() (a template method) as they are.
  2. Circle overrides Area (π r²), Perimeter (2 π r) and Name ("Circle").
  3. Rectangle overrides Area (w × h), Perimeter (2 × (w + h)) and Name ("Rectangle").
  4. Square is a Rectangle with equal sides — it only overrides Name ("Square").

new Circle(3).Describe() should return "Circle: area 28.27, perimeter 18.85".

Premium lesson

Unlock the full Learn C# course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Abstract classes in other languages

Need a quick reminder? The C# cheat sheet has every topic of Learn C# on one page.

Lesson last updated on .

Write code ↓
C#+20 XP

Lesson 25 is part of the full course. The first 8 lessons are free.

Premium lesson

Unlock the full Learn C# course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.