Previous section   Next section

8.2 Implementing an Interface

Suppose you are the author of a Document class that specifies that Document objects can be stored in a database. You decide to have Document implement the IStorable interface. It isn't required that you do so, but by implementing the IStorable interface you signal to potential clients that the Document class can be used just like any other IStorable object. This will, for example, allow your clients to add your Document objects to a collection of IStorable objects, and to otherwise interact with your Document in this very general and well-understood way.

To implement the IStorable interface, you must do two things:

  1. Declare a particular class that implements the interface, using the Implements keyword. The following code declares that the Document class implements IStorable:

    Public Class Document
           Implements IStorable

    The colon operator allows you to put two statements on a single line. It is not uncommon to write:

    Public Class Document : Implements IStorable

  2. Implement each of the interface methods, events, properties, and so forth, and explicitly mark each member as implementing the corresponding interface member. The following code would implement the IStorable interface's Read( ) method:

    Public Sub Read( ) Implements IStorable.Read
        Console.WriteLine("Implementing the Read Method for IStorable")
    End Sub 'Read

Note that with Sub and Function, the Implements keyword goes on the same line as the method definition, and so no colon is needed.

Visual Studio .NET will assist you in this effort through IntelliSense. When you enter the keyword Implements, IntelliSense prompts you with the various interfaces, as shown in Figure 8-1.

Figure 8-1. IntelliSense helps with Implements
figs/pvn2_0801.gif

Once you enter the name of the interface, IntelliSense can help you identify which member you are implementing, as shown in Figure 8-2.

Figure 8-2. Choosing a method from an interface
figs/pvn2_0802.gif

Your definition of this class might look like this:

Public Class Document : Implements IStorable

   Public Sub Read( ) Implements IStorable.Read
   '...
   End Sub 'Read

   Public Sub Write(ByVal o As Object) Implements IStorable.Write
   '...
   End Sub 'Write

   Public Property Status( ) As Integer Implements IStorable.Status
   '...
   End Property
End Class 'Document

It is now your responsibility, as the author of the Document class, to provide a meaningful implementation of the IStorable methods and property. Having designated Document as implementing IStorable, you must implement all the IStorable members, or you will generate an error when you compile. Defining and implementing the IStorable interface is illustrated in Example 8-1.

Example 8-1. Document class implementing IStorable
Option Strict On
Imports System
Namespace InterfaceDemo
    ' define the interface
    Interface IStorable
        Sub Read( )
        Sub Write(ByVal obj As Object)
        Property Status( ) As Integer
    End Interface 'IStorable

    ' create a class which implements the IStorable interface
    Public Class Document
        Implements IStorable

        Public Sub New(ByVal s As String)
            Console.WriteLine("Creating document with: {0}", s)
        End Sub 'New

        ' implement the Read method
        Public Sub Read( ) Implements IStorable.Read
            Console.WriteLine("Implementing the Read Method for IStorable")
        End Sub 'Read

        ' implement the Write method
        Public Sub Write(ByVal o As Object) Implements IStorable.Write
            Console.WriteLine( _
               "Implementing the Write Method for IStorable")
        End Sub 'Write

        ' implement the property
        Public Property Status( ) As Integer Implements IStorable.Status
            Get
                Return myStatus
            End Get
            Set(ByVal Value As Integer)
                myStatus = Value
            End Set
        End Property

        ' store the value for the property
        Private myStatus As Integer = 0

    End Class 'Document

    Class Tester

        Public Sub Run( )
            Dim doc As New Document("Test Document")
            doc.Status = -1
            doc.Read( )
            Console.WriteLine("Document Status: {0}", doc.Status)
        End Sub 'Run

        Public Shared Sub Main( )
            Dim t As New Tester( )
            t.Run( )
        End Sub 'Main

    End Class 'Tester

End Namespace 'InterfaceDemo

Output:
Creating document with: Test Document
Implementing the Read Method for IStorable
Document Status: -1

Example 8-1 defines a simple interface, IStorable, with two methods, Read( ) and Write( ), and a property, Status, of type Integer:

    ' define the interface
    Interface IStorable
        Sub Read( )
        Sub Write(ByVal obj As Object)
        Property Status( ) As Integer
    End Interface 'IStorable

Notice that the IStorable method declarations for Read( ) and Write( ) do not include access modifiers, as was explained earlier, because interface methods are implicitly public so that they can be used by other classes. Once you've defined the IStorable interface, you can define classes that implement the interface. Keep in mind that you cannot create an instance of an interface; instead you instantiate a class that implements the interface.[1]

[1] As will be demonstrated later in this chapter, you can make variables of an interface type, but you must assign to those variables objects of the implementing type.

The class implementing the interface must fulfill the contract exactly and completely. Thus, your Document class must provide a Read( ) and a Write( ) method and the Status property.

    ' create a class which implements the IStorable interface
    Public Class Document
        Implements IStorable

        Public Sub New(ByVal s As String)
            Console.WriteLine("Creating document with: {0}", s)
        End Sub 'New

        ' implement the Read method
        Public Sub Read( ) Implements IStorable.Read
            Console.WriteLine("Implementing the Read Method for IStorable")
        End Sub 'Read

        ' implement the Write method
        Public Sub Write(ByVal o As Object) Implements IStorable.Write
            Console.WriteLine( _
               "Implementing the Write Method for IStorable")
        End Sub 'Write

        ' implement the property
        Public Property Status( ) As Integer Implements IStorable.Status
            Get
                Return myStatus
            End Get
            Set(ByVal Value As Integer)
                myStatus = Value
            End Set
        End Property

        ' store the value for the property
        Private myStatus As Integer = 0

    End Class 'Document

How the Document class fulfills the requirements of the interface, however, is entirely up to you as the class designer. Although IStorable dictates that Document must have a Status property, it does not know or care whether Document stores the actual status as a member variable or looks it up in a database. Example 8-1 implements the Status property by returning and setting the value of a private member variable, myStatus.


  Previous section   Next section
Top