Defines a class method only implemented in subclasses
Function|Procedure declaration; Virtual; Abstract;
The Abstract directive defines a class method as being implemented only in derived classes. It is abstract in the sense that it is a placeholder - it has no implementation in the current class, but must be implemented in any derived classes.
It is used where the base class is always treated as a skeleton class. Where such a class is never directly used - only based classes are ever instantiated into objects.
For example, a TAnimal class may have an abstract method for how the animal moves. Only when creating, say, a TCat class based in TAnimal will you implement the method. In this instance, the cat moves by walking.
An Abstract class must be used to qualify a virtual class, since we are not implementing the class (see Virtual for more details).
If you create an instance of a class that has an Abstract method, then delphi warns you that it contains an uncallable method.

If you then try to call this method, Delphi will try to call AbstractErrorProc. If not found, it will throw an EAbstractError exception.
Related commands
AbstractErrorProcDefines a proc called when an abstract method is called
FunctionDefines a subroutine that returns a value
InheritedUsed to call the parent class constructor or destructor method
OverloadAllows 2 or more routines to have the same name
OverrideDefines a method that replaces a virtual parent class method
ProcedureDefines a subroutine that does not return a value
VirtualAllows a class method to be overriden in derived classes
DynamicAllows a class method to be overriden in derived classes
 Download this web site as a Windows program.

Example code : Polygon skeleton class with triangle and square based classes
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.

unit Unit1;


  Forms, Dialogs, Classes, Controls, StdCtrls, SysUtils;

  // Define a base TPolygon class :
  // This class is a triangle if 3 sides, square if 4 sides ...
  TPolygon = class
    sideCount  : Integer;  // How many sides?
    sideLength : Integer;  // How long each side?
    shapeArea  : Double;  // Area of the polygon
     procedure setArea; Virtual; Abstract;  // Cannot code until sides known
    property count  : Integer read sideCount;
    property length : Integer read sideLength;
    property area   : Double  read shapeArea;
    constructor Create(sides, length : Integer);

  // Define triangle and square descendents
  TTriangle = class(TPolygon)
    procedure setArea; override;  // Override the abstract method

  TSquare = class(TPolygon)
    procedure setArea; override;  // Override the abstract method

  // Define the form class used by this unit
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    { Private declarations }
    { Public declarations }

  Form1: TForm1;

{$R *.dfm} // Include form definitions

// Create the TPolygon object
constructor TPolygon.Create(sides, length : Integer);
  // Save the number and length of the sides
  sideCount := sides;
  sideLength := length;

  // Set the area using the abstract setArea method :
  // This call will be satisfied only by a subclass

// Implement the abstract setArea parent method for the triangle
procedure TTriangle.setArea;
  // Calculate and save the area of the triangle
  shapeArea := (sideLength * sideLength*0.5) / 2;

// Implement the abstract setArea parent method for the square
procedure TSquare.setArea;
  // Calculate and save the area of the square
  shapeArea := sideLength * sideLength;

// Main line code
procedure TForm1.FormCreate(Sender: TObject);
  triangle : TTriangle;
  square   : TSquare;
  // Create a triangle and a square
  triangle := TTriangle.Create(3, 10);
  square   := TSquare.Create(4, 10);

  // Show the areas of our polygons:
  ShowMessageFmt('Triangle area = %f',[triangle.area]);
  ShowMessageFmt('Square   area = %f',[square.area]);

  Triangle area = 43.3
  Square area = 100.0
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page