Building a Class from an Existing Class: Using Inheritance
In this lesson, you will learn how to use inheritance to create a class based on an existing class.
Many real-life objects have attributes and behaviors in common—for example, all cars have wheels and engines, and can roll and stop. Some cars, however, have attributes that are not common—for example, a convertible has a removable top, which may be lowered electronically or by hand.
If you created an object to represent a car, you would want to include properties and methods for all of the common attributes and behaviors, but you would not want to add attributes such as a convertible top, since that attribute does not apply to all cars.
Using inheritance, you can create a "convertible" class that is derived from the car class. It inherits all of the attributes of the car class, and it can add those attributes and behaviors that are unique to a convertible.
Inheriting from an Existing Class
The Inherits statement is used to declare a new class, called a derived class, based on an existing class, known as a base class. Derived classes inherit all of the properties, methods, events, fields, and constants defined in the base class. The following code shows the declaration for a derived class.
Class DerivedClass
Inherits BaseClass
End Class
The new class, DerivedClass, can then be instantiated, its properties and methods accessed just like BaseClass, and you can add new properties and methods that are specific to the new class. For an example, look at the Persons class that you created in the previous lessons.
Suppose you wanted a class that represented baseball players—baseball players have all of the attributes defined in the Persons class, but they also have unique attributes such as number and position. Rather than adding those properties to the Persons class, you will create a new derived class that inherits from Persons, and add the new properties to that class.
Try It!
To create a derived class
Open the Persons project that you created in the previous lesson. If you did not save it, go back to, Testing a Class, and complete the procedures.
In Solution Explorer, select the Persons project node.
On the Project menu, choose Add Class.
In the Add New Item dialog box, type Players in the Name box, and then click Add.
A new class module is added to the project.
In the Code Editor, add the following just below the Public Class Players line.
Inherits Persons
Add the following code to define two new properties.
Private numberValue As Integer Private positionValue As String Public Property Number() As Integer Get Number = numberValue End Get Set(ByVal value As Integer) numberValue = value End Set End Property Public Property Position() As String Get Position = positionValue End Get Set(ByVal value As String) positionValue = value End Set End Property
On the File menu, click Save All.
Testing the Players Class
You have now created a Players class that is derived from the Persons class. In the following procedure, you will create a new program to test the Players class.
To create a test project for your class
On the File menu, point to Add, and then click New Project.
In the Add New Project dialog box, in the Templates pane, select Windows Application.
In the Name box, type PlayerTest and then click OK.
A new Windows Forms project is added to Solution Explorer, and a new form appears.
In Solution Explorer, select the PlayerTest project, and then, on the Project menu, click Set as StartUp Project.
In Solution Explorer, select the PlayerTest project, and then, on the Project menu, click Add Reference.
The Add Reference dialog box opens.
Click the Projects tab and choose Persons, and then click OK.
Double-click the form to open the Code Editor, and then enter the following declaration just below the line Public Class Form1.
Dim player1 As New Persons.Players Dim player2 As New Persons.Players
This declares two new instances of the Players class.
On the File menu, click Save All.
To test the derived class
In Solution Explorer, select Form1 in the PlayerTest project, and then, on the View menu, click Code.
In the Code Editor, add the following code to the Form1_Load event procedure.
With player1 .FirstName = "Andrew" .LastName = "Davis" .Number = 43 .Position = "Shortstop" End With With player2 .FirstName = "Robert" .LastName = "Brown" .Number = 11 .Position = "Catcher" End With
In Solution Explorer, select Form1 in the PlayerTest project, and then, on the View menu, click Designer.
From the Toolbox, drag two Button controls onto the form.
Select the first Button control, and then, in the Properties window, set its Text property to At Bat.
Select the second Button control, and then, in the Properties window, set its Text property to On Deck.
Double-click the first button (At Bat) to open the Code Editor, and then enter the following code in the Button1_Click event handler.
MsgBox(player1.Position & " " & player1.FullName & ", #" & _ CStr(player1.Number) & " is now at bat.")
Notice that you are using the FullName method, which was inherited from the base class Persons.
In the Button2_Click event handler, add the following code.
MsgBox(player2.Position & " " & player2.FullName & ", #" & _ CStr(player2.Number) & " is on deck.")
Press F5 to run the program. Click each button to see the results.
On the File menu, choose Save All.
Next Steps
In this lesson, you learned how to inherit from an existing class and how to extend the derived class. You can learn more about inheritance in Closer Look: Overriding Members, or you can go on to the next lesson and learn about collections.
Next Lesson: Using Collections to Manage Multiple Objects