IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
   ► KBTo/From GuidesReferenceOOP Details  Print This     

Interface (Cross Ref > OOP Details)

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.

Access VBA:  "Interfaces"

Same as in VB6. Access VBA has limited support for interfaces. You can create an interface of abstract methods and properties and then implement them in one or more descendant classes. It's a single level implementation though (you cannot inherit beyond that). The parent interface class is a pure abstract class (all methods and properites are abstract, you cannot implement any of them in the parent class).

In the single level descendant class, you have to implement all methods and properties and you cannot add any. Your first line of code is Implements InterfaceName.

More Info / Comment

More Info

Definition:  Interface

ASP Classic:  "Interfaces" Not Supported

Although ASP Classic does support simple classes, it does not support interfaces.


Interfaces
An element of coding where you define a common set of properties and methods for use with the design of two or more classes. Unlike classes, you cannot provide any implementation and interfaces are not based on inheritance. 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.

More Info

Code:  ASP Classic Interfaces (Not Supported)
Definition:  Interface

C#:  "Interfaces" interface

Classes and structs can inherit from interfaces in a manner similar to how classes can inherit a base class or struct, but a class or struct can inherit more than one interface and it inherits only the method names and signatures, because the interface itself contains no implementations.

class MyClass: IMyInterface
{  
  public object Clone()
{
return null;
}

// IMyInterface implemented here...
}
Syntax Example:
interface IMyInterface
{
  bool IsValid();
}

Classes and structs can inherit from interfaces in a manner similar to how classes can inherit a base class or struct, but a class or struct can inherit more than one interface and it inherits only the method names and signatures, because the interface itself contains no implementations.

class MyClass: IMyInterface
{  
  public object Clone()
{
return null;
}

// IMyInterface implemented here...
}

Uses of Interfaces

  • Interface-Based Polymorphism (Substitutability) - Substitutability allows a descendant class to be used anywhere an associated parent class is used. In object oriented programming a variable could refer to one object at one time and then another object another time. This allows the designer of software to create both a dog.run and a cat.run methods and then decide at runtime whether the variable will be a dog or a cat. Interfaces are a common coding element used to implement this type of polymorphism.
     
  • Interfaces allow for horizontal class design. In a class tree, inheritance is used to design classes vertically with "is-a" relationships. You add functionality from top to bottom adding methods and properties to decsendant classes. Interfaces allow you to design horizontally across your class tree using a "behaves-as" or "looks-like" relationship insisting certain classes implement a common interface.

Visual C# 2008 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:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
  
namespace CR_Interfaces
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
  
    private void button1_Click(object sender, EventArgs e)
    {
      CyborgHuman MyRobot = new CyborgHuman();
      MyRobot.HumanName = "Nicole";
      MyRobot.Speak("Hi, my name is " + MyRobot.HumanName + ".");
    }
  }
  
  public interface IHuman
  {
    //Interface property.
    string HumanName {get; set;}
  
    //Interface method.
    void Speak(string pMessage);
  }
  
  //Sample class for demonstration.
  public class Cyborg
  {
  }
  
  public class CyborgHuman: Cyborg, IHuman
  {
    public string HumanName {get; set;}
    public void Speak(string pMessage)
    {
      MessageBox.Show(pMessage);
    }
  }
}

Default Interface Visibility: Internal

If you do not specify an interfaces visibility, the default is Internal (accessible from types in the same assembly) but an interface's members are always public -- which makes sense but is noteworthy.

interface ITalk {..} //Default visibility is Internal.

More Info

Code:  C# Interfaces (interface)
Definition:  Interface

C++:  "Interfaces" No, but mimic it.

You can mimic an interface by using a class that has only pure-virtual functions and no member variables.


Interfaces
An element of coding where you define a common set of properties and methods for use with the design of two or more classes. Unlike classes, you cannot provide any implementation and interfaces are not based on inheritance. 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.

More Info

Code:  C++ Interfaces (No, but mimic it.)
Definition:  Interface

Corel Paradox:  "Interfaces" Not Supported

Delphi:  "Interfaces" IInterface, TInterfacedObject

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.

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

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
end;

Or you can specify the base interface class:

ITrainedDog = Interface(IInterface)
end;

Or another existing interface:

ITrainedByMike = Interface(ITrainedDog)
end;

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)
End

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)
End

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)
End

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;
  
interface
  
uses
  Dialogs;
  
type
  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);
  end;
  
  TCyborg = class(TInterfacedObject)
  end;
  
  TCyborgHuman = class(TCyborg, IHuman)
  private
    FHumanName: String;
  protected
    function GetHumanName: String;
    procedure SetHumanName(const Value: String);
  public
    property HumanName: String read GetHumanName write SetHumanName;
    procedure Speak(pSentence: String);
  end;
  
implementation
  
function TCyborgHuman.GetHumanName: String;
begin
  Result := FHumanName;
end;
  
procedure TCyborgHuman.SetHumanName(const Value: String);
begin
  FHumanName := Value;
end;
  
procedure TCyborgHuman.Speak(pSentence: String);
begin
  //We had to add the Dialogs unit to this unit
  //because we are using ShowMessage.

  ShowMessage(pSentence);
end;
  
end.

 

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);
var
  MyRobot: TCyborgHuman;
begin
  MyRobot := TCyborgHuman.Create;
  MyRobot.HumanName := 'Nicole';
  MyRobot.Speak('Hi, my name is ' + MyRobot.HumanName + '.');

end;

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

Code:  Delphi Interfaces (IInterface, TInterfacedObject)
Definition:  Interface

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

Code:  Delphi Prism Interfaces
Definition:  Interface

Java:  "Interfaces" Yes

 

More Info

Definition:  Interface
Code:  Java Interfaces (Yes)

VB Classic:  "Interfaces"

VB6 has limited support for interfaces. You can create an interface of abstract methods and properties and then implement them in one or more descendant classes. It's a single level implementation though (you cannot inherit beyond that). The parent interface class is a pure abstract class (all methods and properites are abstract, you cannot implement any of them in the parent class).

In the single level descendant class, you have to implement all methods and properties and you cannot add any. Your first line of code is Implements InterfaceName.

Default Interface Visibility: Friend

If you do not specify an interfaces visibility, the default is Friend but an interface's members are always public -- which makes sense but is noteworthy.

More Info

Definition:  Interface

VB.Net:  "Interfaces" Interface, Implements

With VB.Net you define an interface with the Interface keyword and use it in a class with the Implements keyword. In the resulting class, you implement each property and method and add Implements Interface.Object to each as in:

Sub Speak(ByVal pSentence As String) Implements IHuman.Speak
  MessageBox.Show(pSentence)
End Sub
Syntax Example:
Public Interface IHuman
'Specify interface methods and properties here.
End Interface

Public Class Cyborg
Inherits System.Object
End Class

Public Class CyborgHuman
Inherits Cyborg
Implements IHuman
'Implement interface methods and properties here.
End Class

VB.Net 2008 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:

Public Class Form1
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim MyRobot As New CyborgHuman
    MyRobot.HumanName = "Nicole"
    MyRobot.Speak("Hi, my name is " & MyRobot.HumanName & ".")

  End Sub
End Class
  

'Notice you do not specify visibility in an interface.
'Visibility is determined by the implementing class.

Public Interface IHuman
  Property HumanName()
  Sub Speak(ByVal pSentence As String)
End Interface

Public Class Cyborg
  Inherits System.Object
End Class
Public Class CyborgHuman
  Inherits Cyborg
  Implements IHuman
  Private FHumanName As String

  Public Property HumanName() Implements IHuman.HumanName
    Get
      Return FHumanName
    End Get
    Set(ByVal value)
      FHumanName = value
    End Set
  End Property
  Sub Speak(ByVal pSentence As String) Implements IHuman.Speak
    MessageBox.Show(pSentence)
  End Sub

End Class

Default Interface Visibility: Friend

If you do not specify an interfaces visibility, the default is Friend (accessible from types in the same assembly) but an interface's members are always public -- which makes sense but is noteworthy.

Interface ITalk //Default visibility is Internal.
  '...
End Interface

More Info

Definition:  Interface
Code:  VB.Net Interfaces (Interface, Implements)




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


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