The Book of Visual Studio .NET - OOP in VB .NET Crash Course
Adding Polymorphism or Interface-based Inheritance
This example features an interface called IPerson and a class named Employee that uses the IPerson interface:
1. First create a new Windows Form with the same controls as used in the encapsulation example and name it "frmPolymorphism".
2. Add a new Module to the PersonProj project and name it "MyInterfaces.vb".
3. Apply the following code to the MyInterface.vb module. The code defines
the interface. Public Interface IPerson Property FName() As String Property LName() As String ReadOnly Property FullName() As String Property BirthDate() As Date ReadOnly Property Age() As Integer 'Method for adding hours to m_TotalHours worked. Sub Work(ByVal intHours As Integer) ReadOnly Property TotalHoursWorked() As Integer End Interface
4. Right- click and select Properties, then change the StartUp object to "frmPolymorphism".
5. Create a new class to the Person.vb module. You will use it to inherit the new interface:
Public Class clsPerson3 Implements IPerson Private m_FName As String Private m_LName As String Private m_BirthDate As Date Private m_TotalHours As Integer Private m_HrRate As Integer Private m_TotalPay As Double Public Property FName() As String _ Implements IPerson.FName Get Return m_FName End Get Set(ByVal Value As String) m_FName = Value End Set End Property Public Property LName() As String _ Implements IPerson.LName Get Return m_LName End Get Set(ByVal Value As String) m_LName = Value End Set End Property Public ReadOnly Property FullName() As String _ Implements IPerson.FullName Get Return m_FName & " " & m_LName End Get End Property Public Property BirthDate() As Date _ Implements IPerson.BirthDate Get Return m_BirthDate End Get Set(ByVal Value As Date) If IsDate(Value) Then m_BirthDate = Value End If End Set End Property Public ReadOnly Property Age() As Integer _ Implements IPerson.Age Get If DatePart(DateInterval.Year, _ m_BirthDate) = 1 Then Exit Property Return DateDiff(DateInterval.Year, _ m_BirthDate, Now) End Get End Property 'Method for adding hours to m_TotalHours worked. Public Sub Work(ByVal intHours As Integer) _ Implements IPerson.Work m_TotalHours += intHours End Sub Public ReadOnly Property TotalHoursWorked() As Integer _ Implements IPerson.TotalHoursWorked Get Return m_TotalHours End Get End Property 'Additional Properties: 'HrRate Public WriteOnly Property HrRate() Set(ByVal Value) m_HrRate = Value End Set End Property 'TotalPay Public ReadOnly Property TotalPay() As Double Get Return m_HrRate * m_TotalHours End Get End Property End Class
6. Now run this example just as you ran the previous ones. You will not notice a difference in how the application runs, although the plumbing has changed quite a bit.
With this simple example, it is easy to question the usefulness of polymorphism. However, if you were to continue building an application that dealt with several aspects of a person, you might find polymorphism more helpful to use if you had to deal with Employees, Customers, Managers, and Contractors.
Working with Inheritance
This inheritance example inherits the interface and implementation code of the clsPerson3 class:
1. Add a new Windows Form item named "frmInheritance.vb". Use the same controls as we used in the encapsulation example.
2. Add the following code to the initialization section of the form:
' Here we are initializing the Person class. Normally this would be done ' when the class was needed for data access but in this case we are using ' the Person class to maintain our data. Dim objPerson As New Person.clsEmployee()
3. Add the following class to the Person.vb module of the PersonProj project:
Public Class clsEmployee Inherits clsPerson3 End Class4. Right- click and select Properties, then change the StartUp object to "frmInheritance".
5. Now Run the inheritance example as you did the previous ones. Notice that the clsEmployee class inherits the functionality of the clsPerson3 class, which in turn implements the IPerson interface. This example demonstrates both polymorphism and inheritance that have been combined to form a single solution.
Summary
In this chapter, you learned that Visual Basic has come a long way from a reduced featured-set language that promoted RAPID application development to a full featured language. Now employing full inheritance, Visual Basic promises to aid in the delivery of enterprise level applications that may previously have been better delivered in another OOP language.
This is the final part of a sample chapter from The Book of Visual Studio .NET, ISBN 1-886411-69-7 from No Starch Press
# # #
Page 7 of 7
This article was originally published on February 3, 2003