SmartPascal
Interface
Keyword
Used for Unit external definitions, and as a Class skeleton System unit
1   Unit Name;
  Interface
    Declarations...
  Implementation
    Declarations...
  end.
2   type Name = Interface{(BaseInterface,...)};
Description
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.
Notes
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.
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
 
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;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  // An interface definition
  IVehicle = Interface(IInterface)
    // Properties and their functions
    function GetAge   : Integer;
    function GetMiles : Integer;
    property age   : Integer read GetAge;
    property miles : Integer read GetMiles;

    // Non-property function
    function GetValue : Currency;
  end;

  // Implement this interface in a car class
  // Note that TInterfaceObject defines QueryInterface, _AddRef
  // _AddRef functions for us
  TCar = Class(TInterfacedObject, IVehicle)
  private
    fAge, fMiles : Integer;
    fCarType : string;
    function GetAge   : Integer;
    function GetMiles : Integer;
  public
    property age     : Integer read GetAge;
    property miles   : Integer read GetMiles;
    property carType : string  read fCarType;

    // Non-property function
    function GetValue : Currency;
  published
    constructor Create(age, miles : Integer; carType : string);
  end;

  // The form class itself
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Car constructor
constructor TCar.Create(age, miles: Integer; carType: string);
begin
  // Save parameters
  fAge     := age;
  fMiles   := miles;
  fCarType := carType;
end;

// Get the age of the car
function TCar.GetAge: Integer;
begin
  Result := fAge;
end;

// Get the mileage of the car
function TCar.GetMiles: Integer;
begin
  Result := fMiles;
end;

// Calculate the car value
function TCar.GetValue: Currency;
begin
  Result := 10000.0 - ((age * miles)/10.0);
end;

// Main line code
procedure TForm1.FormCreate(Sender: TObject);
var
  car : TCar;
begin
  // Create a car!
  car := TCar.Create(1, 2076, 'Honda Jazz');

  // Show the current value of this car
  ShowMessageFmt('My %s car is %d years old, %d miles, value %m',
                 [car.carType, car.age, car.miles, car.GetValue]);
end;

end.
   My Honda Jazz car is 1 years old, 2076 miles, value ?9,792.40