JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating Interfaces

Although a class can inherit only from one base class, it can implement multiple interfaces. As in Java, an interface is a specification for a set of class members—not an implementation, just a specification. There's an example on the CD-ROM named Interfaces to show how this works. In this example, I'll define an interface named person and implement that interface in a class named employee to show that implementing interfaces is a little like inheriting from a base class. First, I create the person interface with the Interface statement, indicating that this interface consists of two methods, SetName and GetName, which set and get the name of a person:

Public Interface person
    Sub SetName(ByVal PersonName As String)
    Function GetName() As String
End Interface

Notice that there's no implementation of these methods here, just their declarations. As mentioned above, all an interface does is specify members; when you implement the interface, you must implement all the members yourself. You do that with the Implements keyword (which must come after any Inherits statements and before any Dim statements in a class). Here's how I implement the person interface in a class named employee; note that I specify which class method implements which interface method using the Implements keyword:

Public Class employee
    Implements person
    Dim Name As String

    Sub SetName(ByVal PersonName As String) Implements person.SetName
        Name = PersonName
    End Sub

    Function GetName() As String Implements person.GetName
        Return Name
    End Function
End Class

Now I can create a new object of the employee class named Edward:

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        Dim Edward As New employee()
        Edward.SetName("Edward")
        TextBox1.Text = "You created " & Edward.GetName()
    End Sub
End Class

That's how the Interfaces example functions. You can see it at work in Figure 12.3, where the Edward object of the employee class is created when you click the "Create an employee" button.


Figure 12.3: Using an interface.

One class also can implement multiple interfaces, which is as close as you're going to come to multiple inheritance in Visual Basic (and it's not all that close). For example, in the Interfaces example, I define an interface named executive in addition to the employee example; the executive interface specifies these methods—SetTitle, GetTitle, SetName, and GetName—like this:

Public Interface person
    Sub SetName(ByVal PersonName As String)
    Function GetName() As String
End Interface

Public Interface executive
    Sub SetTitle(ByVal PersonName As String)
    Function GetTitle() As String
    Sub SetName(ByVal ExecutiveTitle As String)
    Function GetName() As String
End Interface

Now I can create a class that implements the executive interface, and I'll call it vicepresident. This class will implement both the person and executive interfaces. Here's how it looks—note in particular that one method can implement multiple interface methods at the same time:

Public Class vicepresident
    Implements person, executive
    Dim Name As String
    Dim Title As String

    Sub SetTitle(ByVal ExecutiveTitle As String) Implements _
        executive.SetTitle
        Title = ExecutiveTitle
    End Sub

    Function GetTitle() As String Implements executive.GetTitle
        Return Title
    End Function

    Sub SetName(ByVal PersonName As String) Implements person.SetName, _
        executive.SetName
        Name = PersonName
    End Sub

    Function GetName() As String Implements person.GetName, _
        executive.GetName
        Return Name
    End Function
End Class

Now when the user clicks the "Create an executive" button in the Interfaces example, I'll create a new vicepresident object named Sam, and set Sam's title to "vice president" like this:

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code

    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
        Dim Sam As New vicepresident()
        Sam.SetName("Sam")
        Sam.SetTitle("vice president")
        TextBox1.Text = "You created " & Sam.GetName() & ", " _
            & Sam.GetTitle ()
    End Sub
End Class

When you click the "Create an executive" button, the Sam object, which implements multiple interfaces, is created, as you see in Figure 12.4.


Figure 12.4: Using multiple interfaces.
Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor