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
An abstract class is a base class that cannot be instantiated and can contain abstract members (no implementation) as well as normal members with implementation. Derived classes must implement all abstract members, unless they are also abstract.
Syntax:
public abstract class Shape
{
public string Name { get; }
protected Shape(string name) => Name = name; // abstract classes can have constructors
public abstract double Area(); // abstract method: no body
public virtual string Describe() => $"{Name} with area {Area():F2}"; // can be overridden
public void Print() => Console.WriteLine(Describe()); // normal method, inherited as is
}
public class Circle : Shape
{
private readonly double _radius;
public Circle(double radius) : base("Circle") => _radius = radius;
public override double Area() => Math.PI * _radius * _radius; // implement with override
}
public sealed class Rectangle : Shape
{
private readonly double _w, _h;
public Rectangle(double w, double h) : base("Rectangle") => (_w, _h) = (w, h);
public override double Area() => _w * _h;
public override string Describe() => base.Describe() + $" ({_w} x {_h})";
}
Usage:
Shape[] shapes = [new Circle(2), new Rectangle(3, 4)];
foreach (var s in shapes) s.Print(); // runtime polymorphism
// var s = new Shape("x"); // compile error: cannot create an instance of an abstract class
Rules to remember
- Abstract methods have no body and end with a semicolon; they are implicitly virtual.
- Implementing classes use the override keyword with the same signature and access modifier.
- Abstract members can only exist inside abstract classes.
- Abstract properties, indexers and events are also possible: public abstract decimal Price { get; }
- An abstract method cannot be private, static or sealed.
- Abstract classes can have fields, constructors and state — unlike interfaces (which can have static and default members but no instance state).
When to use: when related classes share common state or behaviour (template method pattern), and each subclass must supply specific parts.