Object Oriented Programming/Interfaces
Interfaces
[edit | edit source]An Interface allows a class to expose a different set of properties and methods depending on context. For example, say that you were writing a Visual Basic 2005 application that manages clients. A client could be an individual person, or an organization. Part of the program generates address labels for clients.
You might define a person like this:
Class Person
Public LastName As String
Public FirstName As String
Public Address As String
End Class
You might also define an Organization like this:
Class Organization
Public Name As String
Public MailingAddress As String
Public BuildingAddress As String
End Class
As you can see, the main differences between a Person and an Organization are:
- a Person has both a first name and a last name, while an organization has only a single name
- an Organization has a separate mailing address, while a Person only has one address
So that you can have the one routine to print address labels, you create an interface called "IAddressLabel" (by convention, the name of an interfaces should start with a capital I).
Interface IAddressLabel
ReadOnly Property Name() As String
ReadOnly Property Address() As String
End Interface
You now change the Person class to implement this interface. The main thing to notice is that the Person's first and last names are combined on the label.
Class Person
Implements IAddressLabel
Public LastName As String
Public FirstName As String
Public Address As String
Public ReadOnly Property Address1() As String Implements IAddressLabel.Address
Get
Return Address
End Get
End Property
Public ReadOnly Property Name() As String Implements IAddressLabel.Name
Get
Return FirstName & " " & LastName 'combine the first and last names for the address label
End Get
End Property
End Class
You now change the Organization class to implement the IAddressLabel interface. Notice that the address for the label is the mailing address, not the building address.
Class Organization
Implements IAddressLabel
Public Name As String
Public MailingAddress As String
Public BuildingAddress As String
Public ReadOnly Property Address() As String Implements IAddressLabel.Address
Get
Return MailingAddress 'Use the mailing address for the address label
End Get
End Property
Public ReadOnly Property Name1() As String Implements IAddressLabel.Name
Get
Return Name
End Get
End Property
End Class
Now you can group the disparate objects together as a collection of address labels!
Sub Main()
'create a person object
Dim Bill As New Person
Bill.FirstName = "William"
Bill.LastName = "Jones"
Bill.Address = "123 Test Rd Testville 9123"
'create another person object
Dim Sally As New Person
Sally.FirstName = "Sally"
Sally.LastName = "Smith"
Sally.Address = "999 Sample Ave Testburg 9222"
'create an Organization object
Dim WidgetsInc As New Organization
WidgetsInc.Name = "Widgets Incorporated"
WidgetsInc.BuildingAddress = "9123 Avenue Rd Sampletown 9876"
WidgetsInc.MailingAddress = "P.O. Box 123 Sampletown 9876"
'Because all three objects implement the IAddressLabel interface, we can put them in the same array
Dim MailingList(2) As IAddressLabel
MailingList(0) = Bill
MailingList(1) = Sally
MailingList(2) = WidgetsInc
'loop through, displaying the address label for each object
Dim i As Integer
For i = 0 To 2
MsgBox(MailingList(i).Name & vbCrLf & MailingList(i).Address)
Next i
End Sub
Interfaces vs. Inheritance
[edit | edit source]Interfaces and Inheritance can both be used to solve the problem of treating dissimilar objects collectively. For example, if you have a Cat class and a Dog class, but have some routine that needs to process them together, you could either:
- Create an Animal base class, which contains procedures common to both Cats and Dogs, and have Cat and Dog inherit from Animal, or
- Create an IAnimal interface, and have both Cat and Dog implement the interface.
Which approach to use depends on several factors. In general:
- If you are extending an existing class, then use inheritance. For example, if you already have an Animal class, and then discover a need to distinguish between Cats and Dogs
- If you are simply wanting to treat different objects as the same, then use Interfaces. For example, you already have Cat and Dog classes, and then discover a need to manipulate them in a similar fashion