In C#, an abstract class is a base class that can combine shared state and implementation with members that derived classes must provide; an interface defines a contract that a type can implement, including required members and, in modern C#, some default implementations. Choose an abstract class when related types need a common base or shared state, and an interface when different types should promise the same capability without sharing a class hierarchy.
Background: Two Ways to Define a Type Contract
Both features support abstraction: code can depend on what an object can do rather than on every detail of how it does it. They are not interchangeable, though. An abstract class participates in C# class inheritance, while an interface describes a set of members a class or struct agrees to provide.
A class marked abstract cannot be instantiated directly. It may contain implemented methods, fields, properties, constructors, and abstract members. A derived class must implement inherited abstract members unless it is also abstract. An interface cannot be instantiated either, but it does not provide instance fields or constructors. It specifies member signatures and may include certain implementations or static members under current C# language rules.
How Abstract Classes Work
An abstract class can hold data and provide common behavior. Its constructors run as part of creating a concrete derived object, allowing the base class to initialize shared state. Members can have access modifiers, and a derived class can override a virtual or abstract member to customize behavior.
abstract class Shape
{
public string Color { get; }
protected Shape(string color) => Color = color;
public abstract double Area();
public string Describe() => $"{Color} shape";
}
class Circle : Shape
{
public double Radius { get; }
public Circle(string color, double radius) : base(color)
=> Radius = radius;
public override double Area() => Math.PI * Radius * Radius;
}
Shape supplies the shared color and description behavior, but leaves the area calculation to each concrete shape. This is useful when the types have a genuine “is a” relationship and benefit from a common implementation or protected state.
How Interfaces Work
A class or struct can implement multiple interfaces, even though it can inherit from only one class. That makes interfaces useful for capabilities that cut across unrelated types—for example, objects that can be saved, sorted, or disposed of. A type does not need to share a base class with other implementers.
interface IPrintable
{
void Print();
}
class Invoice : IPrintable
{
public void Print() => Console.WriteLine("Printing invoice");
}
class Photo : IPrintable
{
public void Print() => Console.WriteLine("Printing photo");
}
Code accepting an IPrintable can work with either type without knowing its concrete class. An interface member is commonly implemented publicly, but C# also supports explicit interface implementation when a type needs to keep the implementation off its ordinary public API or resolve a naming conflict.
Key Differences at a Glance
- Inheritance: A class can have one direct base class, abstract or concrete, and can implement multiple interfaces.
- State: An abstract class can declare instance fields and maintain object state. An interface cannot declare instance fields.
- Constructors: Abstract classes can define constructors; interfaces cannot have instance constructors.
- Implementation: Abstract classes can provide ordinary methods and shared implementation. Interfaces can specify required members and, in modern C#, provide default interface implementations in supported scenarios.
- Access: Abstract-class members can use varied access levels, such as protected or private. Interface contracts are generally exposed through public members, although newer language features allow more nuanced interface member declarations.
- Relationship: Deriving from an abstract class expresses a class-family relationship. Implementing an interface expresses that a type supports a capability or contract.
These are general distinctions rather than a claim that every interface contains only signatures. C# has expanded interface features over time, so code targeting older language versions or runtimes may have different support. Check the project’s language version and target platform when using newer features.
When to Choose Each
Prefer an abstract class when the types are closely related and need shared instance data, a protected helper, common initialization, or a base algorithm with a few customizable steps. For example, several document parsers might share configuration and a parsing workflow while overriding format-specific operations.
Prefer an interface when unrelated types should be usable through the same capability, or when a type may need to satisfy several contracts. A file, network response, and in-memory object could all implement an interface for a shared operation without being forced into one class family.
- Use an abstract class for a reusable base implementation and a meaningful inheritance relationship.
- Use an interface for a role or capability that multiple, potentially unrelated types can support.
- Use both when helpful: a base class may implement an interface, while other types implement that same interface independently.
- Keep contracts focused. A very large interface or base class can make changes harder for every dependent type.
Common Misconceptions and Design Problems
A frequent misconception is that interfaces cannot contain any code. Modern C# permits default interface members, among other features, but an interface still does not become a substitute for an ordinary stateful base class: it cannot hold per-instance fields or run an instance constructor. Default implementations also have language and runtime compatibility considerations, so they should not be assumed to work in every older project.
Another mistake is choosing an abstract class solely to share a few methods. Doing so consumes the type’s single class-inheritance slot and couples it to that base class. A small helper or composition may be simpler; an interface may be more flexible if the goal is only to expose a capability.
Conversely, an interface is not automatically the better choice just because it is flexible. If derived types must share meaningful state and invariant-preserving behavior, duplicating that implementation across interface implementers can create maintenance problems. Also consider compatibility: adding a required member to a public interface can require existing implementers to change. A base class may offer more ways to evolve a shared implementation, though changes to either public abstraction need careful design.
Frequently Asked Questions
Can a C# class inherit from multiple abstract classes?
No. A class can have only one direct base class. It can implement multiple interfaces, and its base class can itself implement interfaces.
Can an abstract class implement an interface?
Yes. An abstract class can implement an interface and provide some or all of its members. If required members remain unimplemented, a concrete derived class must complete them.
Can an interface have fields or constructors?
An interface cannot have instance fields or instance constructors. It can declare members that define a contract and supports certain static members; exact features depend on the C# language version.
Can an abstract class have no abstract methods?
Yes. A class may be abstract to prevent direct instantiation or to establish a base type, even if all its members already have implementations.
Should I use an interface or an abstract class for dependency injection?
Either can represent a dependency. An interface is often useful when consumers need only a contract and several implementations are possible; an abstract class can fit when implementations share state or behavior. The appropriate choice depends on the design and framework configuration.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Valheim Server Not Showing in Community List Fixes
- DisplayPort vs HDMI: Which Cable Should You Use?
- Soundbar vs 2.1 Speaker System: Which Is Better?
- Survive Lava for Brainrot Tier List170 points
- MySQL Dump Import: Fix Common Errors
- Mouse Moving on Its Own? Causes & Fixes