0
0
CsharpComparisonBeginner · 4 min read

Abstract Class vs Interface in C#: Key Differences and Usage

Use a abstract class when you want to share common code among related classes and provide default behavior. Use an interface when you want to define a contract that multiple unrelated classes can implement without sharing code.
⚖️

Quick Comparison

This table summarizes the main differences between abstract classes and interfaces in C#.

FactorAbstract ClassInterface
PurposeShare common code and base functionalityDefine a contract without implementation
Multiple inheritanceNo, a class can inherit only one abstract classYes, a class can implement multiple interfaces
Members allowedMethods with or without implementation, fields, propertiesOnly method/property/event signatures (default implementations allowed since C# 8)
Access modifiersSupports access modifiers (public, protected, etc.)Members are public by default, no access modifiers allowed
When to useWhen classes are closely related and share codeWhen unrelated classes need to follow the same contract
ConstructorsCan have constructorsCannot have constructors
⚖️

Key Differences

An abstract class allows you to define some common behavior and state that derived classes can reuse or override. It can contain fields, constructors, and fully implemented methods. This makes it ideal when you have a clear class hierarchy where subclasses share code.

On the other hand, an interface defines a set of methods and properties that implementing classes must provide. It does not hold state or implementation (except default interface methods introduced in C# 8). Interfaces enable multiple inheritance of type, allowing a class to implement many interfaces, which is useful for defining capabilities across unrelated classes.

Another key difference is that abstract classes can control access to members with modifiers like protected, while interface members are always public. Also, abstract classes can have constructors to initialize state, but interfaces cannot.

⚖️

Code Comparison

csharp
using System;

abstract class Animal
{
    public abstract void Speak();

    public void Eat()
    {
        Console.WriteLine("Eating food...");
    }
}

class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Woof!");
    }
}

class Program
{
    static void Main()
    {
        Dog dog = new Dog();
        dog.Speak();
        dog.Eat();
    }
}
Output
Woof! Eating food...
↔️

Interface Equivalent

csharp
using System;

interface IAnimal
{
    void Speak();
    void Eat();
}

class Dog : IAnimal
{
    public void Speak()
    {
        Console.WriteLine("Woof!");
    }

    public void Eat()
    {
        Console.WriteLine("Eating food...");
    }
}

class Program
{
    static void Main()
    {
        Dog dog = new Dog();
        dog.Speak();
        dog.Eat();
    }
}
Output
Woof! Eating food...
🎯

When to Use Which

Choose an abstract class when:

  • You have a clear class hierarchy with shared code or state.
  • You want to provide default method implementations and control access modifiers.
  • You need constructors to initialize common data.

Choose an interface when:

  • You want to define a contract for unrelated classes.
  • You need multiple inheritance of types.
  • You want to ensure classes implement certain methods without forcing shared code.

Key Takeaways

Use abstract classes to share code and state among closely related classes.
Use interfaces to define contracts for unrelated classes and enable multiple inheritance.
Abstract classes can have constructors and access modifiers; interfaces cannot.
Interfaces focus on what a class can do, abstract classes focus on what a class is.
Choose based on your design needs: shared implementation vs flexible contracts.