Previous section   Next section

6.2 Inheritance

In VB.NET, the specialization relationship is implemented using inheritance. This is not the only way to implement specialization, but it is the most common and most natural way to implement this relationship.

Saying that ListBox inherits from (or derives from) Window indicates that it specializes Window. Window is referred to as the base class, and ListBox is referred to as the derived class. That is, ListBox derives its characteristics and behaviors from Window and then specializes to its own particular needs.

6.2.1 Implementing Inheritance

In VB.NET, you create a derived class by adding the Inherits keyword after the name of the derived class, followed by the name of the base class:

Public Class ListBox
   Inherits Window

Or you might combine these two lines onto one as follows:

Public Class ListBox : Inherits Window

This code declares a new class, ListBox, that derives from Window. You can read the Inherits keyword as "derives from."

The derived class inherits all the members of the base class, both member variables and methods. These members can be treated just as if they were created in the derived class, as shown in Example 6-1.

Example 6-1. Deriving a new class
Option Strict On
Imports System

Public Class Window

    ' constructor takes two integers to
    ' fix location on the console
    Public Sub New(ByVal top As Integer, ByVal left As Integer)
        Me.top = top
        Me.left = left
    End Sub 'New

    ' simulates drawing the window
    Public Sub DrawWindow( )
        Console.WriteLine("Drawing Window at {0}, {1}", top, left)
    End Sub 'DrawWindow

    ' these members are private and thus invisible
    ' to derived class methods; we'll examine this 
    ' later in the chapter
    Private top As Integer
    Private left As Integer

End Class 'Window

' ListBox derives from Window
Public Class ListBox

    Inherits Window

    ' constructor adds a parameter
    Public Sub New(ByVal top As Integer, ByVal left As Integer, ByVal theContents 
As String)
        MyBase.New(top, left) ' call base constructor
        mListBoxContents = theContents
    End Sub 'New

    ' a shadow version (note keyword) because in the
    ' derived method we change the behavior
    Public Shadows Sub DrawWindow( )
        MyBase.DrawWindow( ) ' invoke the base method
        Console.WriteLine("Writing string to the listbox: {0}", mListBoxContents)
    End Sub 'DrawWindow

    Private mListBoxContents As String ' new member variable

End Class 'ListBox

Module Module1

    Sub Main( )
        ' create a base instance
        Dim w As New Window(5, 10)
        w.DrawWindow( )

        ' create a derived instance
        Dim lb As New ListBox(20, 30, "Hello world")
        lb.DrawWindow( )
    End Sub

End Module

Output:
Drawing Window at 5, 10
Drawing Window at 20, 30
Writing string to the listbox: Hello world

Example 6-1 starts with the declaration of the base class Window. This class implements a constructor and a simple DrawWindow( ) method. There are two private member variables, top and left. The program is analyzed in detail in the following sections.

6.2.2 Calling Base Class Constructors

In Example 6-1, the new class ListBox derives from Window and has its own constructor, which takes three parameters (top, left, and theContents). The ListBox constructor invokes the constructor of its parent by calling MyBase.New and passing in the parameters (using the ByVal keyword, as described in Chapter 5):

Public Sub New( _
   ByVal top As Integer, _
   ByVal left As Integer, _
   ByVal theContents As String)
   MyBase.New(top, left) ' call base constructor
   mListBoxContents = theContents
End Sub 'New

Because classes cannot inherit constructors, a derived class must implement its own constructor and can only make use of the constructor of its base class by calling it explicitly.

If the base class has an accessible default constructor, the derived constructor is not required to invoke the base constructor explicitly; instead, the default constructor is called implicitly. However, if the base class does not have a default constructor, every derived constructor must explicitly invoke one of the base class constructors using the MyBase keyword, which refers to the base class for the current object.

As discussed in Chapter 5, if you do not declare a constructor of any kind, the compiler will create a default constructor for you. Whether you write it yourself or you use the one provided "by default" by the compiler, a default constructor is one that takes no parameters. Note, however, that once you do create a constructor of any kind (with or without parameters) the compiler does not create a default constructor for you.

6.2.3 Shadowing Base Methods

Also notice in Example 6-1 that ListBox implements a new version of DrawWindow( ):

Public Shadows Sub DrawWindow( )

The keyword Shadows here indicates that the programmer is intentionally creating a new version of this method in the derived class.

In Example 6-1, the DrawWindow( ) method of ListBox hides and replaces the base class method. When you call DrawWindow( ) on an object of type ListBox, it is ListBox.DrawWindow( ) that will be invoked, not Window.DrawWindow( ). Note, however, that ListBox.DrawWindow( ) can invoke the DrawWindow( ) method of its base class with the code:

MyBase.DrawWindow( ) 'invoke the base method

6.2.4 Controlling Access

The visibility of a class and its members can be restricted through the use of access modifiers, such as Public, Private, and Protected. As explained in Chapter 5, Public allows a member to be accessed by the member methods of other classes, while Private indicates that the member is visible only to member methods of its own class. The Protected keyword extends visibility to methods of derived classes.

Classes as well as their members can be designated with any of these accessibility levels. If a class member has a different access designation than the class, the more restricted access applies. Thus, if you define a class, SomeClass, as follows:

Public Class SomeClass
   '...
   Protected myValue As Integer
End Class 'SomeClass

the accessibility for myValue is protected even though the class itself is public. A public class is one that is visible to any other class that wishes to interact with it.


  Previous section   Next section
Top