Your full service technology partner! 
-Collapse +Expand
Search Delphi Group:

-Collapse +Expand Delphi To/From
-Collapse +Expand Delphi Store

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...OOP   Print This     
  From the October 2015 Issue of Prestwood eMag
Delphi OOP:
Delphi Interfaces (IInterface, TInterfacedObject)
Posted 14 years ago on 11/2/2008 and updated 2/2/2009
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page


General Info: Interface

An element of coding where you define a common set of properties and methods for use with the design of two or more classes.

Both interfaces and abstract classes are types of abstraction. With interfaces, like abstract classes, you cannot provide any implementation. However, unlike abstract classes, interfaces are not based on inheritance. You can apply an Interface to any class in your class tree. In a real sense, interfaces are a technique for designing horizontally in a class hierarchy (as opposed to inheritance where you design vertically). Using interfaces in your class design allows your system to evolve without breaking existing code.

Delphi Interfaces

In Delphi, you use interfaces for both com objects and language interfaces and make use of IUnknown, IInterface, and/or TInterfacedObject.

For a pure language interface, add your specified proprieties, procedures, and functions to an interface that descends from IInterface (the base interface) as an interface, no implementation. Then have your implementing class inherit from TInterfacedObject and implement the interface.

For extending the VCL, you descend from the class you wish to extend, then implement an interface from IInterface and add the required functions QueryInterface, _AddRef, and _Release methods (refer to TInterfacedObject for an example).

For a com object, you descend from IUnknown. Descending from IUnknown instead of IInterface informs the Delphi compiler that the interface must be compatible with COM objects -- a Windows feature).

When defining an interface, define it in the type block just like you do for a class but you use the interface keyword instead of the class keyword and in the interfaces section only. Since interfaces, by definition, do not have any implementation details, all you do is specify it in the type block. Then implement in all classes that support the interface.

Syntax Example:
//Language interface:
//Interface section of unit.
IHuman = Interface(IInterface)
  //Specify interface methods and properties here.

TCyborg = class(TInterfacedObject)
TCyborgHuman = class(TCyborg, IHuman)
//Specify each here and implement in
//implementation section.

Now let's dive deeper...

Base IInterface Class

The base interface class in the VCL is IInterface. When defining an interface, you can specify IInterface, leave it out, or specify an already defined interface.

Declare from base interface class:

ITrainedDog = Interface

Or you can specify the base interface class:

ITrainedDog = Interface(IInterface)

Or another existing interface:

ITrainedByMike = Interface(ITrainedDog)

Class Declarations With Interface Implementations

You specify the interface you wish to implement as part of your class declaration comma separated after the parent class.

The following example shows a single T1 class descending from TInterfacedObject so I didn't have to implement the QueryInterface, _AddRef, and _Release functions.

T1 = Class(TInterfacedObject)

The following example  shows a single class that implements one interface. This new class inherits from T1 (a cyborg class) but acts like a human.

T800Human = Class(T1, IHuman)

The following example  shows a single class that implements three interfaces. This new class inherits from T1 (a cyborg class) but this cyborg acts like a very well trained dog.

T800Dog = Class(T1, ITrainedDog, IShowDog, IGuardDog)

Delphi 2009 Working Example of a Language Interface

The following example demonstrates implementing a very simple language interface. The interface is named IHuman which includes one property and one method. Our resulting class is named TCyborgHuman and, for clarity, our TCyborgHuman class also inherits from a class called TCyborg which for ease of implementation descends from TInterfacedObject (again, so I didn't have to implement the QueryInterface, _AddRef, and _Release functions).

Create a form and place a button on it. Then add a unit called CyborgUnit.pas as follows:

unit CyborgUnit;
  IHuman = Interface(IInterface)
    //These methods are required to support our property.
    function GetHumanName: String;
    procedure SetHumanName(const Value: String);
    //Defined properties and methods for interface.
    property HumanName: String read GetHumanName write SetHumanName;
    procedure Speak(pSentence: String);
  TCyborg = class(TInterfacedObject)
  TCyborgHuman = class(TCyborg, IHuman)
    FHumanName: String;
    function GetHumanName: String;
    procedure SetHumanName(const Value: String);
    property HumanName: String read GetHumanName write SetHumanName;
    procedure Speak(pSentence: String);
function TCyborgHuman.GetHumanName: String;
  Result := FHumanName;
procedure TCyborgHuman.SetHumanName(const Value: String);
  FHumanName := Value;
procedure TCyborgHuman.Speak(pSentence: String);
  //We had to add the Dialogs unit to this unit
  //because we are using ShowMessage.



Now let's use our new CyborgHuman class which supports the IHuman interface. Alter the code of your button on your form as follows:

procedure TForm1.Button1Click(Sender: TObject);
  MyRobot: TCyborgHuman;
  MyRobot := TCyborgHuman.Create;
  MyRobot.HumanName := 'Nicole';
  MyRobot.Speak('Hi, my name is ' + MyRobot.HumanName + '.');


Com Interfaces

The scope of this article was on overview material and implementing a language interface. There are plenty of good examples on the internet for implementing a com interface. The main points of implementing a Windows com object are:

  • Your interface descends from IUnknown which is the fundamental interface for com objects that indicates you must support QueryInterface, AddRef, and Release.
  • You should use a GUID in the interface declaration to uniquely identify it (Ctrl+Shift+G to insert into code).

More Info

Definition:  Interface


Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P143A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101439 Counter
Since 11/2/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site:

©1995-2022 Prestwood IT Solutions.   [Security & Privacy]