Export (0) Print
Expand All

Inheritance for Visual Basic 6.0 Users

Inheritance allows you to create new classes from existing classes. Inheritance can simplify the design of the application by providing a relationship structure between the various classes. It also supports code reuse, because only the new or different class behavior must be coded.

Visual Basic 6.0

Visual Basic 6.0 supports interface inheritance, covered in more detail in Interface Changes for Visual Basic 6.0 Users.

Visual Basic 2008

Visual Basic 2008 supports interface inheritance with the Interface Statement (Visual Basic) and Implements Statement. For more information on interfaces, see Interfaces in Visual Basic.

When a new class is created through implementation inheritance, the new class automatically gains all the members and implementation of the base class. The existing class is called the "base class." The new class is the "derived class." Additional members can be added to the class. The behavior of the base class can be changed by writing code in the new class. This technique is called overriding.

You can create classes by inheriting from the classes you create, from classes in references you add to your project, or from objects in the .NET Framework. Many classes of the .NET Framework are related by inheritance. For example, the TextBox class inherits from the System.Windows.Forms.TextBoxBase class.

Visual Basic 6.0

Visual Basic 6.0 does not support implementation inheritance.

Visual Basic 2008

Inheritance is declared in Visual Basic 2008 by the Inherits statement. In this example, the derived class Oak inherits from a base class Tree.

Public Class Oak
    Inherits Tree
    ' Add code here to extend or 
    ' modify the behavior of the Tree class.
End Class


Visual Basic 6.0

In interface inheritance in Visual Basic 6.0, any class can act as an interface base class. There is no syntax for preventing any class to act as an interface base class. Likewise, there is no syntax to demand that a class act only as an interface base class.

Visual Basic 2008

Along with implementation inheritance, Visual Basic 2008 defines two class modifiers, MustInherit and NotInheritable. These modifiers let the programmer control the inheritance relationships in an application.

The MustInherit modifier on a class declaration indicates a class that cannot be instantiated:

Public MustInherit Class BaseClass
End Class


This means that BaseClass cannot appear after the New keyword. Only classes that inherit from BaseClass and that do not also have the MustInherit modifier can be instantiated. In object-oriented literature and in other object-oriented languages, a MustInherit class is called an abstract class. A class that is not a MustInherit class, one that can be instantiated, is called a concrete class.

A related concept is the sealed class, a class that cannot serve as a base class. The NotInheritable keyword on a class definition indicates this status:

Public NotInheritable Class DerivedClass
End Class


A NotInheritable class marks the edge of an inheritance hierarchy.

For more information, see Inheritance in Visual Basic.

Classes acting as interfaces in Visual Basic 6.0 are upgraded to interfaces in Visual Basic 2008. Consider this example of a base class and derived class from Visual Basic 6.0:

' Contents of class BaseClass
Public Sub BaseMethod()
End Sub

' Contents of class DerivedClass
Implements BaseClass
Private Sub BaseClass_BaseMethod()
End Sub

The Upgrade Wizard produces this upgraded code:

Option Strict Off
Option Explicit On
Interface _BaseClass
    Sub BaseMethod()
End Interface

Friend Class BaseClass
    Implements _BaseClass
    Public Sub BaseMethod() Implements _BaseClass.BaseMethod
    End Sub
End Class

Friend Class DerivedClass
    Implements _BaseClass
    Private Sub BaseClass_BaseMethod() Implements _BaseClass.BaseMethod
    End Sub
End Class


Using inheritance instead of interfaces, the upgraded code can be modified to:

Friend Class BaseClass
    Public Sub BaseMethod()
        ' Add code here to define BaseMethod.
    End Sub
End Class

Friend Class DerivedClass
    Inherits BaseClass
End Class


This eliminates one level of indirection, _BaseClass. Also, it eliminates one method definition, BaseClass_BaseMethod, because the method is inherited from the base class and does not need to be coded again. If the programmer wanted a different behavior for the derived class, the BaseMethod can be overridden as shown:

Friend Class BaseClass
    Public Overridable Sub BaseMethod()
        ' Add code here to define BaseMethod.
    End Sub
End Class

Friend Class DerivedClass
    Inherits BaseClass
    Public Overrides Sub BaseMethod()
        ' Add code here to define behavior for DerivedClass.
    End Sub
End Class


Some techniques to consider in upgraded interfaces include:

  • Replace Interface statements with Inherits statements.

  • Implement the base class as an interface, rather than as a class that implements an interface. For more information see Interface Changes for Visual Basic 6.0 Users.

  • Implement base classes as MustInherit classes.

  • Implement derived classes as NotInheritable.

Community Additions

ADD
Show:
© 2015 Microsoft