LearnAI ToolsCareerPractice BuildsPlayDevelopmentContact
Lesson 2615 min read

Abstract Classes

Learn how abstract classes and abstract methods force derived classes to provide their own implementation.

On this page 5 sections

What is an Abstract Class?

An abstract class cannot be instantiated directly with new — it exists only to be inherited from. It can mix fully implemented methods with ones that derived classes must implement themselves.

Abstract Methods

abstract class Shape
{
public abstract double GetArea(); // no body — must be implemented by derived classes
public void Describe() // regular method, shared by all shapes
{
Console.WriteLine($"This shape has an area of {GetArea():F2}");
}
}
class Circle : Shape
{
public double Radius;
public Circle(double radius) { Radius = radius; }
public override double GetArea() => Math.PI * Radius * Radius;
}
var circle = new Circle(5);
circle.Describe();
Output

Click Run to see what this code prints.

Why Use Abstract Classes?

They let you define a shared contract and some common behavior at once, while forcing every derived class to supply its own logic for the parts that genuinely differ (like how to calculate area for a Circle vs a Square).

Abstract Class vs Interface

Abstract classes can hold shared implementation and fields; interfaces (next lesson) traditionally can't hold state and are meant purely as a contract. A class can inherit only one abstract class, but implement multiple interfaces.

FAQs

No — new Shape() won't compile. Only concrete (non-abstract) derived classes like Circle can be instantiated.

Summary

  • Abstract classes can't be instantiated directly.
  • Abstract methods have no body and must be overridden in derived classes.
  • They combine shared implementation with an enforced contract.
Next Lesson →

Interfaces