CSharp - Abstract Classes and Abstract Members

Introduction

You can declare a class as abstract.

An abstract class represents a concept rather something concrete. For example, number is a concept, int is concrete.

A class declared as abstract can never be instantiated.

Only its concrete subclasses can be instantiated.

Abstract classes can have abstract members, method, property, event, indexer, etc.

Abstract members are like virtual members without default implementation.

The implementation must be provided by the subclass, unless that subclass is also declared abstract:

abstract class Shape{
       // Note empty implementation
       public abstract decimal NetValue { get; }
}

class Circle : Shape{
       public long Radius;
       public decimal CurrentRadius;

       // Override like a virtual method.
       public override decimal NetValue => CurrentRadius * Radius;
}

Related Topics

Quiz