Used for Unit external definitions, and as a Class skeleton System unit
1   Unit Name;
2   type Name = Interface{(BaseInterface,...)};
The Interface keyword is used in two different ways.
Version 1
It starts the definition of external interface of a Unit. Declarations here are externally visible by other units. All of these declarations must be implemented in the Implementation section.
The Uses statement, if present, must be at the start.
Version 2
In Object Oriented programming, we often use Abstract class methods in a base class as a placeholder. All derived classes must implement these methods.
Taking this one step further, an Interface defines a grouping of just abstract properties and methods. It provides a template for a class to use to ensure consistency. It is like a class with only abstract methods. It has the benefits that classes can be based on one parent class, and implement one or more interfaces. It adds a predictable flavour of operation to each class that implements the interface.
Take a look at the Delphi tutorial for more on this complex subject.
When implementing an interface, you must implement QueryInterface, _AddRef and _Release standard interface methods, unless you base your class on one that already has these implemented, such as TInterfacedObject.

Thanks to Dietmar Brueckmann for the example code.
Related commands
Abstract Defines a class method only implemented in subclasses
Class Starts the declaration of a type of object class
Constructor Defines the method used to create an object from a class
Destructor Defines the method used to destroy an object
Function Defines a subroutine that returns a value
Implementation Starts the implementation (code) section of a Unit
Object Allows a subroutine data type to refer to an object method
Procedure Defines a subroutine that does not return a value
TObject The base class type that is ancestor to all other classes
Unit Defines the start of a unit file - a Delphi module
Uses Declares a list of Units to be imported
 Author links

 Download this web site as a Windows program.

Example code : Creating a car class from a vehicle interface
// 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;


  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DateUtils, StdCtrls;

   // An interface definition
   IRecyclable = Interface(IInterface)
     // A single function supporting the property
     function GetIsRecyclable : Boolean;
     // A single property
     property isRecyclable : Boolean read GetIsRecyclable;

  // Define our Car class
  TCar = class // (TInterfacedObject, IRecyclable)
    carName  : String;
    carAge   : Byte;
    carIsRecyclable : Boolean;
    function GetIsRecyclable : Boolean;   // Added for IRecyclable
    // Car properties
    property Name : String read carName;
    property Age : Byte read carAge write carAge;

    // Added for IRecyclable
    property isRecyclable : Boolean read GetIsRecyclable;

    // Car constructor
    constructor Create(name : String);

  // Define our Bicycle class
  TBicycle = class // (TInterfacedObject, IRecyclable)
    bikeIsMale       : Boolean;
    bikeWheelSize    : Byte;
    function GetIsRecyclable : Boolean;   // Added for IRecyclable
    // Bicycles properties
    property isMale : Boolean read bikeIsMale;
    property wheelSize : Byte read bikeWheelSize write bikeWheelSize;

    // Added for IRecyclable
    property isRecyclable : Boolean read GetIsRecyclable;

    // Bicycle constructor
    constructor Create(isMale : Boolean; wheelSize : Byte);

  // The definition of the program form
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    { private declarations }
    { public declarations }

  Form1: TForm1;


{$R *.dfm}

// Constructor implmentation for the car class
constructor TCar.Create(name : String);
  // Save the car name and set default age and miles
  carName         := name;
  carAge          := 0;
  carIsRecyclable :=true    // Sets the recyclable indicator

// Car function required for IsRecyclable attribute
function TCar.GetIsRecyclable : Boolean;
  Result := carIsRecyclable;

// Constructor implmentation for the bicycle class
constructor TBicycle.Create(isMale : Boolean; wheelSize : Byte);
  // Save the passed parameters
  bikeIsMale    := isMale;
  bikeWheelSize := wheelSize;

// Bicycle function required for IsRecyclable attribute
function TBicycle.GetIsRecyclable : Boolean;
  // We'll asy that only male bicycles are recyclable
  if self.isMale
  then Result := true
  else Result := false;

procedure TForm1.FormCreate(Sender: TObject);
  mumsBike : TBicycle;
  dadsCar  : TCar;
  // Instantiate our bike and car objects
  mumsBike := TBicycle.Create(false, 24);
  dadsCar  := TCar.Create('Nissan bluebird');

  // Ask if each is recyclable
  if dadsCar.isRecyclable
  then ShowMessage('Dads car is recyclable')
  else ShowMessage('Dads car is not recyclable');

  if mumsBike.isRecyclable
  then ShowMessage('Mums bike is recyclable')
  else ShowMessage('Mums bike is not recyclable');

   Dads car is recyclable
   Mums bike is not recyclable
Delphi Programming Neil Moffatt 2002 - 2020. All rights reserved.  |  Home Page