wasAsked.

Default Interface Methods vs Abstract Classes

EPAM · Role not specified · technical

Published

Year not specified · C#

Community answers

Posted by wasAsked editorial collection

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

Sign in to add your answer

Related interview topics