IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Prism
Search Prism Group:

Advanced
-Collapse +Expand Prism To/From
To/FromCODEGuides
-Collapse +Expand Prism Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Prism Store
PRESTWOODSTORE

Prestwood eMagazine

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

   ► KBProgrammingDelphi PrismOOP   Print This     
  From the March 2016 Issue of Prestwood eMag
 
Prism OOP:
Delphi Prism Interfaces
 
Posted 16 years ago on 12/25/2008 and updated 2/1/2009
Prism Code Snippet:
 A flashcard from our Prism Flashcards Library
 A code snippet from our Prism Code Snippets Page

KB101733

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 Prism Interfaces

With Prism, you use the Interface keyword to define an interface and then you include one or more interfaces where you specify the single class inheritance (separated by commas).

Syntax Example:
//Interface section of unit.
IHuman = public interface
//Specify interface methods and properties here.
end;

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

Specify Multiple Interfaces

You can also specify more than one interface to implement. Just separate each interface with a comma.

Cyborg600 = public class(Cyborg, IChat, IMove, IAdvancedMove)
end;

Working WinForms Example

The following example demonstrates implementing a very simple interface. The interface is named IHuman which includes one property and one method. Our resulting class is named CyborgHuman and, for clarity, our CyborgHuman class also inherits from a class called Cyborg.

 

Create a form and place a button on it and alter the code as follows:

namespace CR_Interfaces;
  
interface
  
uses
  System.Drawing,
  System.Collections,
  System.Collections.Generic,
  System.Linq,
  System.Windows.Forms,
  System.ComponentModel;
  
type
  /// <summary>
  /// Summary description for MainForm.
  /// </summary>
  MainForm = partial class(System.Windows.Forms.Form)
  private
    method button1_Click(sender: System.Object; e: System.EventArgs);
  protected
    method Dispose(disposing: Boolean); override;
  public
    constructor;
  end;
  
  IHuman = interface
    property HumanName: String read write;
    procedure Speak(pSentence: String);
  end;
  
  Cyborg = public class(System.Object)
  end;
  
  CyborgHuman = public class(Cyborg, IHuman)
  private
    FHumanName: String;
  public
    property HumanName: String read FHumanName write FHumanName;
    procedure Speak(pSentence: String);
  end;
  
implementation
{$REGION Construction and Disposition}
constructor MainForm;
begin
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
method MainForm.Dispose(disposing: Boolean);
begin
  if disposing then begin
    if assigned(components) then
      components.Dispose();
    //
    // TODO: Add custom disposition code here
    //
  end;
  inherited Dispose(disposing);
end;
{$ENDREGION}
  
procedure CyborgHuman.Speak(pSentence: String);
begin
  MessageBox.Show(pSentence);
end;
  
method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
begin
  var MyRobot: CyborgHuman;
  
  MyRobot := New CyborgHuman;
  MyRobot.HumanName := "Nicole";
  MyRobot.Speak("Hi, my name is " + MyRobot.HumanName + ".");
end;
  
end.

More Info

Definition:  Interface

Comments

1 Comments.
Share a thought or comment...
Comment 1 of 1

A good tip for implementing interfaces is, after you have added the IHuman to the class that is implementing it, you can right click on that IHuman and choose the Implement Interface Members.  It will then stub out all the pieces/parts associated with that interface.  BIG time saver.

Posted 13 years ago
 
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 = P133A1
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 play-drums.com 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 #101733 Counter
18466
Since 12/25/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


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