wasAsked.

Abstart class & how to you implement abstract method - write syntax.

HCA Healthcare · Role not specified · technical

Published

Year not specified · C#

Community answers

Posted by wasAsked editorial collection

An abstract class cannot be instantiated directly. It can contain fields, constructors, implemented members, virtual members, and abstract members. An abstract member declares a contract without an implementation; a non-abstract derived class must override it. ```csharp public abstract class PaymentProcessor { protected PaymentProcessor(ILogger logger) => Logger = logger; protected ILogger Logger { get; } public abstract Task<PaymentResult> ChargeAsync( decimal amount, CancellationToken cancellationToken); public virtual bool CanRefund => true; } public sealed class CardPaymentProcessor : PaymentProcessor { public CardPaymentProcessor(ILogger<CardPaymentProcessor> logger) : base(logger) { } public override async Task<PaymentResult> ChargeAsync( decimal amount, CancellationToken cancellationToken) { if (amount <= 0) throw new ArgumentOutOfRangeException(nameof(amount)); await Task.Delay(10, cancellationToken); return new PaymentResult(true); } } ``` Choose an abstract class when related implementations share state or behavior. Choose an interface when consumers need a capability contract across unrelated types or multiple implementations without shared construction and state.

Pooja Parmar · · 0 likes

Sign in to add your answer

Related interview topics