FreePascal Information Logo Friend of FreePascal Compiler Title
Articles with Feedback, FPC News Library, PDF Collection, Mail Lists, Books, Newsgroups, IRC Open online discussion areas Research and Tutorials Tools, Compilers and Utilities Blurbs about us, advertising, etc.
Welcome to the FoFPC Pascal Language ABSTRACT

abstract


     Defines a class method only implemented in subclasses. 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, the compiled binary will try to call AbstractErrorProc. If not found, it will throw an EAbstractError exception.

Example

    Download Source IconDownload This Source for Free Pascal
{$MODE DELPHI} // class support
{$M+} // constructor support

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

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

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

// Create the TPolygon object
constructor TPolygon.Create(sides, length : Integer);
begin
  // 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
  setArea;
end;

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

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

var
  triangle : TTriangle;
  square   : TSquare;

begin
  // Create a triangle and a square
  triangle := TTriangle.Create(3, 10);
  square   := TSquare.Create(4, 10);
  // Show the areas of our polygons:
  Writeln('Triangle area = ',triangle.area);
  Writeln('Square   area = ',square.area);
end.

Output

Triangle area =  4.330000000000000E+001
Square   area =  1.000000000000000E+002

See Also

AbstractErrorProc, Dynamic, Function, Inherited, Overload, Override, Procedure, Virtual.
 Links and Products we find useful



ButtonGenerator.com
Valid XHTML 1.0 Transitional Internet Map
Programmer's Heaven
grat-i-fi-ca-tion - noun
the state of being gratified; great satisfaction.


"Wow! We are so pleased to see Friends of FreePascal Compiler ... with such a cool look and feel!"

"We like the fact you wrote all of the server scripts using FPC!"

Ian Wright
Codemasters
Locations of visitors to this page world map hits counter
Copyright 2009 by 3F, LLC. All rights reserved. Worldwide.
Your request was processed by server #3 in 0.294749 secs.

sponsor
Click to visit our paid sponsor