Skip to content

Abstract Classes

An abstract class is a class that can’t be instantiated directly β€” it exists to be inherited from. It can mix fully-implemented methods (shared logic every subclass gets for free) with abstract methods, which have no body and must be overridden by any non-abstract derived class. Use it when subclasses share real, common implementation, not just a shared contract (that’s what an interface is for).

public abstract class Shape
{
public abstract double GetArea(); // no implementation -- subclasses must provide one
public void Describe() // shared implementation, inherited as-is
{
Console.WriteLine($"This shape has an area of {GetArea():F2}");
}
}
public class Circle : Shape
{
private readonly double _radius;
public Circle(double radius) => _radius = radius;
public override double GetArea() => Math.PI * _radius * _radius;
}
// var shape = new Shape(); // Error: cannot create an instance of an abstract class
var circle = new Circle(5);
circle.Describe(); // "This shape has an area of 78.54"

Forgetting override on the derived class’s implementation of an abstract member β€” without it, the compiler reports an error demanding you implement the abstract member, since a plain method with the same name doesn’t count as fulfilling the contract.

public abstract class Shape
{
public abstract double GetArea();
}
public class Circle : Shape
{
public double GetArea() => Math.PI * 25; // Error: Circle does not implement Shape.GetArea()
}
// Fix: add `override`
public class Circle : Shape
{
public override double GetArea() => Math.PI * 25;
}
  1. Can you create an instance of an abstract class directly with new?

    AnswerNo β€” abstract classes can't be instantiated; only their concrete (non-abstract) subclasses can.
  2. When should you use an abstract class instead of an interface?

    AnswerWhen subclasses need to share real implementation, not just a method signature β€” abstract classes can hold shared logic and state; interfaces (until default interface methods) traditionally only define contracts.
  3. What must every non-abstract class that inherits from an abstract class do?

    AnswerProvide an override implementation for every abstract member it inherits, or the class itself must also be declared abstract.