What is the difference between an abstract class and an interface?
Quick Answer
An abstract class is a base class you can't instantiate; it can hold state (fields), constructors, and a mix of implemented and abstract members, and a class can inherit only one. An interface is a contract — historically members only, now optionally with default implementations — holds no instance state, and a class can implement many. Use an abstract class for an “is-a” family sharing code; use an interface for a “can-do” capability.
An abstract class is a base class you can't create directly. It can have fields, constructors and real method implementations alongside abstract members, and you only get one because C# has single inheritance. An interface is a contract — just the signatures, though since C# 8 you can add default implementations — it has no instance state, and a class can implement as many as it wants. I use an abstract class when there's a family of types that share code and state, like Stream. I use an interface when I want unrelated types to offer the same capability, or when I need a seam for dependency injection and testing. It's common to have both — an interface for the contract and an abstract class providing a partial default implementation.
| Abstract class | Interface | |
|---|---|---|
| Instantiate | No | No |
| Multiple per class | No (single inheritance) | Yes |
| Instance fields / state | Yes | No |
| Constructors | Yes | No |
| Member implementations | Yes (and abstract members) | Default implementations (C# 8+), otherwise none |
| Access modifiers on members | Full range | Public by default |
| Models | “is-a” + shared behaviour | “can-do” capability / role |
Choosing:
- Need shared implementation or fields across a family (
Stream,Controller)? Abstract class. - Need unrelated types to expose the same capability (
IDisposable,IComparable<T>,IEmailSender)? Interface. - Designing for DI and testing? Interface — it's the seam you mock.
- Often both: an interface for the contract, an abstract base for a default partial implementation.