Abstract Class in C#

An abstract class in C# is a class that cannot be instantiated directly and is designed to be used as a base class. It can contain abstract members that derived classes must implement, and it can also contain normal members with shared implementation.

Abstract classes are useful when related classes share common behavior but still need to provide their own specific implementation for some operations. They sit between normal classes and interfaces: more concrete than an interface, but not complete enough to create objects directly.

In C#, abstract classes are commonly used in frameworks, base services, game objects, document processors, report generators, UI controls, and domain models where several related types follow the same structure.


What Is Abstract Class in C#?

An abstract class is declared using the abstract keyword. It can have abstract methods, normal methods, fields, properties, constructors, and events. A derived class must implement inherited abstract members unless the derived class is also abstract.

abstract class Shape
{
    public abstract double GetArea();

    public void Display()
    {
        Console.WriteLine("This is a shape");
    }
}

The Shape class cannot be used with new Shape(). It only defines common structure for derived classes.

Creating a Derived Class from Abstract Class

class Circle : Shape
{
    public double Radius { get; set; }

    public override double GetArea()
    {
        return Math.PI * Radius * Radius;
    }
}

The Circle class inherits from Shape and implements the abstract GetArea method using override.

Abstract Method in C#

An abstract method has no body in the abstract class. It only declares the method signature. Every non-abstract derived class must override it and provide the actual implementation.

abstract class Animal
{
    public abstract void MakeSound();
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Dog barks");
    }
}

class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Cat meows");
    }
}

The base class requires every animal to make a sound, but each derived class decides what that sound is. This is a clean use of abstract methods.

Concrete Method in Abstract Class

An abstract class can also contain normal methods with complete implementation. These are called concrete methods. Derived classes inherit and reuse them.

abstract class Document
{
    public abstract void PrintContent();

    public void PrintHeader()
    {
        Console.WriteLine("Document Header");
    }
}

class Invoice : Document
{
    public override void PrintContent()
    {
        Console.WriteLine("Invoice content");
    }
}

The derived class must implement PrintContent, but it can reuse PrintHeader from the base class. This is one key difference between abstract classes and simple contracts.

Can We Create Object of Abstract Class?

No. You cannot create an object directly from an abstract class. The abstract class is incomplete by design.

// Not allowed
// Shape shape = new Shape();

Shape shape = new Circle();

You can use an abstract class reference to point to a derived class object. This supports runtime polymorphism.

Abstract Properties in C#

An abstract class can declare abstract properties. The derived class must implement those properties.

abstract class Employee
{
    public abstract string Role { get; }
    public abstract decimal CalculateSalary();
}

class Developer : Employee
{
    public override string Role => "Developer";

    public override decimal CalculateSalary()
    {
        return 80000;
    }
}

The base class says every employee must have a role and salary calculation. The derived class provides the actual details.

Constructors in Abstract Class

An abstract class can have a constructor. Even though you cannot create the abstract class directly, its constructor runs when a derived class object is created. This is useful for initializing common base data.

abstract class Person
{
    public string Name { get; }

    protected Person(string name)
    {
        Name = name;
    }
}

class Student : Person
{
    public int RollNumber { get; }

    public Student(string name, int rollNumber) : base(name)
    {
        RollNumber = rollNumber;
    }
}

The Student constructor calls the abstract base class constructor using base(name). The base constructor initializes shared data.

Abstract Class and Polymorphism

Abstract classes are often used with polymorphism. A variable of the abstract base type can refer to any derived class object.

List<Shape> shapes = new List<Shape>
{
    new Circle { Radius = 5 },
    new Rectangle { Length = 10, Width = 4 }
};

foreach (Shape shape in shapes)
{
    Console.WriteLine(shape.GetArea());
}

The loop works with the abstract type Shape. The correct derived implementation runs for each object.

Abstract Class vs Interface in C#

PointAbstract ClassInterface
Object creationCannot instantiate directlyCannot instantiate directly
InheritanceA class can inherit only one classA class can implement many interfaces
FieldsCan contain fieldsUsually does not contain instance fields
Shared codeGood for shared base logicBest for behavior contracts
Use caseRelated classes with common implementationCapability shared by related or unrelated classes

Use an abstract class when derived classes are strongly related and need shared code or shared state. Use an interface when you need to define a capability that many different classes can support.

Abstract Class vs Normal Class

A normal class can be instantiated directly. An abstract class cannot. A normal class usually represents a complete type. An abstract class represents an incomplete base that must be completed by derived classes.

PointNormal ClassAbstract Class
Can create objectYesNo
Can contain abstract methodsNoYes
Can contain concrete methodsYesYes
PurposeComplete object typeBase type for derived classes

When to Use Abstract Class

  • Use it when related classes share common data or behavior.
  • Use it when derived classes must implement certain methods.
  • Use it when the base type should not be created directly.
  • Use it when shared constructor logic is needed.
  • Use it when polymorphism needs a common base class.

An abstract class is a design tool. It should express a real common foundation, not just a place to dump shared helper methods.

Common Mistakes with Abstract Classes

  • Using an abstract class when an interface would be enough.
  • Putting too many unrelated responsibilities into the base class.
  • Creating deep inheritance chains from abstract classes.
  • Making every method abstract even when shared behavior exists.
  • Using abstract classes only to avoid duplicate code without a real is-a relationship.
  • Forgetting that derived classes must implement abstract members.

The most common design mistake is using abstract classes as a general utility container. If the derived classes are not truly related, an interface or composition may be better.

Best Practices for Abstract Classes in C#

  • Use abstract classes for related types that share common structure.
  • Keep the base class focused and stable.
  • Use abstract members only when derived classes must provide behavior.
  • Use concrete methods for shared reusable logic.
  • Avoid deep inheritance hierarchies.
  • Prefer interfaces when only a capability contract is needed.

Abstract Class Design Example

Suppose an application processes different file types. Every processor needs a file path and a common validation step, but each file type has different processing logic. An abstract class can store the common path and validation method, while derived classes implement the file-specific processing.

abstract class FileProcessor
{
    protected string FilePath { get; }

    protected FileProcessor(string filePath)
    {
        FilePath = filePath;
    }

    public void Validate()
    {
        if (string.IsNullOrWhiteSpace(FilePath))
        {
            throw new ArgumentException("File path is required");
        }
    }

    public abstract void Process();
}

This is a good abstract class candidate because the derived processors are related and share real base behavior.

Abstract Class Interview Points

For interviews, remember that an abstract class cannot be instantiated directly. It can contain both abstract and concrete members. Abstract methods do not have a body and must be overridden by non-abstract derived classes. Abstract classes can have constructors, fields, properties, methods, and access modifiers.

Also remember that a class can inherit from only one abstract class because C# supports single class inheritance. If you need multiple capability contracts, interfaces are usually the better choice.

Abstract Class and Template Method Pattern

Abstract classes are often used in the template method pattern. The base class defines the fixed workflow, and derived classes provide specific steps. This keeps the high-level algorithm consistent while allowing controlled customization.

abstract class ReportGenerator
{
    public void Generate()
    {
        LoadData();
        FormatReport();
        SaveReport();
    }

    protected abstract void LoadData();
    protected abstract void FormatReport();

    private void SaveReport()
    {
        Console.WriteLine("Report saved");
    }
}

The workflow is controlled by the base class. Derived classes can change specific steps but cannot accidentally skip the save step. This is a strong use case for abstract classes.

Abstract Method vs Virtual Method

An abstract method has no implementation in the base class and must be implemented by the derived class. A virtual method already has an implementation in the base class, but derived classes are allowed to override it if they need different behavior.

PointAbstract MethodVirtual Method
Method bodyNo bodyHas body
Derived classMust overrideMay override
Class requirementOnly inside abstract classCan be inside normal or abstract class
Use caseBase class cannot provide default behaviorBase class has default behavior but allows customization

Choose abstract when every derived class must define its own behavior. Choose virtual when the base behavior is valid for many cases but should remain customizable.

This distinction helps keep base classes honest. If the base class cannot safely provide a meaningful default implementation, an abstract member makes the requirement clear and forces every concrete derived class to handle that behavior explicitly, making maintenance safer in larger production codebases over time with fewer surprises.

FAQs on Abstract Class in C#

Can we create object of abstract class in C#?

No. An abstract class cannot be instantiated directly. You must create an object of a derived class.

Can abstract class have constructor?

Yes. An abstract class can have constructors. The constructor runs when a derived class object is created.

Can abstract class have normal methods?

Yes. An abstract class can contain normal concrete methods as well as abstract methods.

When should I use abstract class instead of interface?

Use an abstract class when related classes need shared state or shared implementation. Use an interface when you only need to define a capability contract.


Continue learning C# in order
Follow the topic sequence with the previous and next lesson.