Defining an Interface
The syntax for defining an interface is as follows:
[attributes] [access-modifier] Interface identifier
[InterfaceBases]
interface-body
End InterfaceThe 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 InterfaceNote 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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access