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.
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
Cross Reference Examples:
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
ASP Classic:
"Interfaces" Not Supported
Although ASP Classic does support simple classes, it does not support interfaces.
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(); }
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.
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;
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 classend; TCyborgHuman = public class(TCyborg, IHuman ) //Specify each here and implement in //implementation section. end;
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 .