Defining an Interface
The syntax for defining an interface is as follows:
[attributes] [access-modifier] Interface identifier
[InterfaceBases]
interface-body
End Interface
The keyword Interface
is followed by an identifier (the interface name). It is common (but not required) to begin the name of your interface with a capital I
. Thus, IStorable
, ICloneable
, IAndThou
, etc.
The body of the interface is terminated with the keywords End Interface
.
Suppose you wish to create an interface to define the contract for being stored. Your interface will define the methods and properties a class will need to be stored to a database or file. You decide to call this interface IStorable
. The purpose of this interface is to define the capabilities that you want to have available in any class that can be stored.
In the IStorable
interface you might specify two methods: Read
and Write
, and a property Status
:
Interface IStorable Sub Read() Sub Write(ByVal obj As Object) Property Status() As Integer End Interface
Note that when declaring the methods of the interface you provide a prototype:
Sub Read()
but no implementation and no End Function
, End Sub
, or End Property
statement. Notice also that the IStorable
method declarations do not include access modifiers (e.g., public, protected, internal, private). In fact, providing an access modifier generates a compile error. Interface methods are implicitly public because an interface is a contract meant to be used by other classes.
Get Programming Visual Basic 2005 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.