0
0
CsharpComparisonBeginner · 4 min read

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

In C#, an abstract class can provide both method implementations and abstract methods, allowing shared code and state, while an interface only declares method signatures without implementations. Use abstract classes when you want to share common behavior and state, and interfaces to define a contract that multiple unrelated classes can implement.
⚖️

Quick Comparison

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

FeatureAbstract ClassInterface
PurposeBase class with shared code and abstract methodsDefines a contract with method signatures only
Method ImplementationCan have both implemented and abstract methodsCannot have method implementations (before C# 8.0)
Fields/StateCan have fields and constructorsCannot have fields or constructors
Multiple InheritanceCannot inherit from multiple abstract classesCan implement multiple interfaces
Access ModifiersCan have access modifiers (public, protected, etc.)All members are public by default
Version IntroducedSince early C# versionsSince early C# versions; default implementations added in C# 8.0
⚖️

Key Differences

Abstract classes allow you to define some common behavior and state that derived classes can share. They can have fields, constructors, and fully implemented methods alongside abstract methods that must be overridden. This makes them suitable when classes are closely related and share a base implementation.

On the other hand, interfaces define a contract that classes must follow, specifying what methods or properties they must implement without providing any code (except default implementations starting from C# 8.0). Interfaces support multiple inheritance, allowing a class to implement many interfaces, which is useful for defining capabilities across unrelated classes.

In summary, use abstract classes when you want to share code and state, and use interfaces to enforce a contract across different class hierarchies without sharing implementation.

⚖️

Code Comparison

Here is an example of an abstract class defining a shape with a shared method and an abstract method to calculate area.

csharp
using System;

abstract class Shape
{
    public string Name { get; set; }

    public Shape(string name)
    {
        Name = name;
    }

    public void Display()
    {
        Console.WriteLine($"Shape: {Name}");
    }

    public abstract double GetArea();
}

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

    public Circle(double radius) : base("Circle")
    {
        Radius = radius;
    }

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

class Program
{
    static void Main()
    {
        Circle c = new Circle(3);
        c.Display();
        Console.WriteLine($"Area: {c.GetArea():F2}");
    }
}
Output
Shape: Circle Area: 28.27
↔️

Interface Equivalent

The same shape example using an interface to define the contract without shared implementation or state.

csharp
using System;

interface IShape
{
    string Name { get; }
    double GetArea();
}

class Circle : IShape
{
    public string Name => "Circle";
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }

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

class Program
{
    static void Main()
    {
        IShape c = new Circle(3);
        Console.WriteLine($"Shape: {c.Name}");
        Console.WriteLine($"Area: {c.GetArea():F2}");
    }
}
Output
Shape: Circle Area: 28.27
🎯

When to Use Which

Choose an abstract class when: you want to share common code or state among closely related classes, need constructors or fields, and want to provide default behavior that derived classes can override.

Choose an interface when: you want to define a contract that multiple unrelated classes can implement, need to support multiple inheritance of types, or want to specify capabilities without enforcing a class hierarchy.

Key Takeaways

Abstract classes can have shared code, fields, and constructors; interfaces cannot (except default implementations in C# 8.0+).
Interfaces support multiple inheritance; abstract classes do not.
Use abstract classes for closely related types sharing behavior; use interfaces to define contracts across unrelated types.
Abstract classes can have access modifiers; interface members are always public.
Interfaces define what a class can do; abstract classes define what a class is.