A default interface method lets an interface add behavior without immediately breaking every implementation. An abstract class can hold instance state, define constructors, expose protected members, and combine abstract and implemented members. A class can implement multiple interfaces but inherit from only one base class.
```csharp
public interface IPriceRule
{
decimal Apply(decimal subtotal);
string Describe() => GetType().Name;
}
public abstract class AuditedRule
{
protected AuditedRule(ILogger logger) => Logger = logger;
protected ILogger Logger { get; }
public abstract decimal Apply(decimal subtotal);
}
```
Use an interface to describe a capability across unrelated types and keep consumers decoupled. Use an abstract base when implementations genuinely share state, invariants, construction logic, or protected behavior. Default interface implementations should be small and contract-oriented; they are not a substitute for a stateful base class. Also remember that a default method is normally called through the interface reference unless the class provides its own public member.
Prince Yadav · · 0 likes
Default interface methods (DIM, C# 8+) let an interface provide an implementation for a member. That overlaps a little with abstract classes, but they are meant for different purposes.
Default interface method example:
public interface ILogger
{
void Log(string level, string message);
void Info(string message) => Log("INFO", message); // default implementation
}
public class ConsoleLogger : ILogger
{
public void Log(string level, string message) => Console.WriteLine($"[{level}] {message}");
}
ILogger logger = new ConsoleLogger();
logger.Info("Started"); // available through the interface
// new ConsoleLogger().Info(...) does not compile — default members are not inherited by the class
Main purpose: evolving an interface — adding new members to a published interface without breaking existing implementations.
Key differences
State
- Interfaces cannot have instance fields or constructors (they can have static fields and members).
- Abstract classes can hold fields, constructors and protected state.
Inheritance
- A class can implement many interfaces but inherit only one class.
Access to members
- Default interface members are accessible only through the interface type, not through the implementing class reference, unless the class implements them itself.
- Abstract class members are inherited normally.
Access modifiers
- Interfaces now allow private, protected and static members, but most members are public.
- Abstract classes have the full range of access modifiers.
Runtime and versioning
- DIM requires runtime support (.NET Core 3.0+), not .NET Framework.
- Diamond problems: if two interfaces provide conflicting defaults, the class must implement the member explicitly.
When to use which
- Default interface methods: adding a convenience method or new capability to an existing interface used by many implementers (common in libraries), or providing helper behaviour built on other interface members.
- Abstract class: sharing real implementation and state across a family of related types, enforcing construction rules, or the template method pattern.
They are not a replacement for abstract classes; DIM is an evolution tool for interfaces. Overusing it to share large amounts of logic makes designs harder to understand.