![]() ![]() | ||
Access Modifiers Inheritance Modifiers Overloading, Overriding, and Shadowing Creating Interfaces Polymorphism Early and Late Binding Immediate Solutions: Inheriting from a Base Class Using Public Inheritance Using Protected Inheritance Using Private Inheritance Using Friend Access Overriding Base Class Members Inheriting Constructors Overloading Base Class Members Creating Interfaces Using Multiple Interfaces Using the MustInherit Keyword (Creating Abstract Classes) Using MustOverride, Overridable, and NotOverridable Creating Shadowing Using the MyBase Keyword Using the MyClass Keyword Inheritance-based Polymorphism Interface-based Polymorphism Early and Late Binding
This chapter is all about inheritance, which is the process you use to derive one class from another. This is more useful than it may sound, because Visual Basic comes with thousands of built-in base classes for you to create derived classes from and then customize. We're already familiar with this process from our work with Windows forms, of course, because we derive our forms from System.Windows.Forms.Form and then customize them with buttons and event handlers, like this:
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 ⋮ End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click ⋮ End Sub End Class
The Inherits statement you see above is used to declare a new class, called the derived class, based on an existing class, known as the base class. Derived classes inherit, and can extend, the properties, methods, events, fields, and constants defined in the base class (the exception here are constructors, which are not inherited). For example, if you have a class named Animal that has a method named Breathe and then derive a class named Dog from Animal, Dog will already have the Breathe method built in.
By default, any class can serve as a base class unless you explicitly mark it with the NotInheritable keyword, as we'll see in this chapter. And in Visual Basic, you can inherit only from one base class, not more. As with Java, Visual Basic allows you to implement multiple interfaces, which we'll see in a few pages, and that can accomplish much that multiple inheritance could, but with more work on our part.
Let's take a look at an example, named Inheritance on the CD-ROM, that puts this into a practical light and will give us a good start on inheritance. In fact, here I'll implement the base class Animal and the derived class Dog, discussed earlier. I start by creating the Animal class. This class will have a method named Breathing, which displays the text "Breathing…" in a text box in the program's main Windows form. To get access to that form, I can pass that form to the constructor, New, which will store it as MainForm:
Public Class Animal Public MainForm As Form1 Public Sub New(ByVal form1 As Form1) MainForm = form1 End Sub ⋮ End Class
Now, in Breathing, I can use MainForm to display the text "Breathing…" in a text box, TextBox1, in the main form:
Public Class Animal Public MainForm As Form1 Public Sub New(ByVal form1 As Form1) MainForm = form1 End Sub Public Sub Breathing() MainForm.TextBox1.Text = "Breathing..." End Sub End Class
That's simple enough so far. Now I can derive a new class, Dog, from Animal—note the Inherits statement here:
Public Class Dog Inherits Animal ⋮ End Class
However, this raises an issue—how do we pass the form to display text into the Animal class's constructor so it can store it in MainForm? In other words, when you create an object of the Dog class, how can you pass necessary data back to the base class's constructor? You can do that with the special MyBase keyword, which refers to the base class. This means I can call the base class's constructor as MyBase.New. Note that if you do call a base class's constructor, you must do so as the very first line (Visual Basic insists on this) in your derived class's constructor:
Public Class Dog Inherits Animal Public Sub New(ByVal form1 As Form1) MyBase.New(form1) End Sub ⋮ End Class
Now the Dog class inherits everything the Animal class had, such as the Breathing method. In addition, it inherits the MainForm data member, so we can use that data member when we add a new method to Dog, Barking:
Public Class Dog Inherits Animal Public Sub New(ByVal form1 As Form1) MyBase.New(form1) End Sub Public Sub Barking() MainForm.TextBox1.Text = "Barking..." End Sub End Class
In this way, we're augmenting and customizing the base class in the derived class; for example, now Dog supports a Barking method in addition to the Breathing method. To see the Dog class at work, I can create a new Dog object, passing its constructor the current form so it knows what form to display results in, and calling its Breathing method like this (recall that the Me keyword, which we first saw in Chapter 4, refers to the current form):
Public Class Form1 Inherits System.Windows.Forms.Form 'Windows Form Designer generated code Dim spot As Dog Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click spot = New Dog(Me) spot.Breathing() End Sub End Class
This is all in the Inheritance example on the CD-ROM; you can see it at work in Figure 12.1. When you click the "Create a dog…" button, the code creates a new Dog object, and the Dog class's constructor passes the main form back to the Animal base class. When you call the Breathing method, which is inherited from the base class, the program displays the text "Breathing…" in the main form, as you see in that figure. Now we're using inheritance.
As we've discussed as far back as Chapter 2, you can control the access that derived classes have to base class members by using access modifiers.
![]() ![]() | ||