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.
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 |
|
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;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DateUtils, StdCtrls;
type
// An interface definition
IRecyclable = Interface(IInterface)
// A single function supporting the property
function GetIsRecyclable : Boolean;
// A single property
property isRecyclable : Boolean read GetIsRecyclable;
end;
 // Define our Car class
TCar = class// (TInterfacedObject, IRecyclable)
private
carName : String;
carAge : Byte;
carIsRecyclable : Boolean;
function GetIsRecyclable : Boolean; // Added for IRecyclable
published
 // 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);
end;
 // Define our Bicycle class
TBicycle = class// (TInterfacedObject, IRecyclable)
private
bikeIsMale : Boolean;
bikeWheelSize : Byte;
function GetIsRecyclable : Boolean; // Added for IRecyclable
published
 // 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);
end;
 // The definition of the program form
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
// Constructor implmentation for the car class
constructor TCar.Create(name : String);
begin
 // Save the car name and set default age and miles
carName := name;
carAge := 0;
carIsRecyclable :=true  // Sets the recyclable indicator
end;
// Car function required for IsRecyclable attribute
function TCar.GetIsRecyclable : Boolean;
begin
Result := carIsRecyclable;
end;
// Constructor implmentation for the bicycle class
constructor TBicycle.Create(isMale : Boolean; wheelSize : Byte);
begin
 // Save the passed parameters
bikeIsMale := isMale;
bikeWheelSize := wheelSize;
end;
// Bicycle function required for IsRecyclable attribute
function TBicycle.GetIsRecyclable : Boolean;
begin
 // We'll asy that only male bicycles are recyclable
if self.isMale
then Result := true
else Result := false;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
mumsBike : TBicycle;
dadsCar : TCar;
begin
 // 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');
end;
end.
| |
Dads car is recyclable
Mums bike is not recyclable |
|
|