Dela via


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.

Interface Inheritance

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.

Implementation Inheritance

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.

PublicClass Oak
    Inherits Tree
    ' Add code here to extend or     ' modify the behavior of the Tree class.EndClass

MustInherit and NotInheritable

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:

PublicMustInheritClass BaseClass
EndClass

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:

PublicNotInheritableClass DerivedClass
EndClass

A NotInheritable class marks the edge of an inheritance hierarchy.

For more information, see Inheritance in Visual Basic.

Upgrade Suggestions

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 OnInterface _BaseClass
    Sub BaseMethod()
EndInterfaceFriendClass BaseClass
    Implements _BaseClass
    PublicSub BaseMethod() Implements _BaseClass.BaseMethod
    EndSubEndClassFriendClass DerivedClass
    Implements _BaseClass
    PrivateSub BaseClass_BaseMethod() Implements _BaseClass.BaseMethod
    EndSubEndClass

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

FriendClass BaseClass
    PublicSub BaseMethod()
        ' Add code here to define BaseMethod.EndSubEndClassFriendClass DerivedClass
    Inherits BaseClass
EndClass

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:

FriendClass BaseClass
    PublicOverridableSub BaseMethod()
        ' Add code here to define BaseMethod.EndSubEndClassFriendClass DerivedClass
    Inherits BaseClass
    PublicOverridesSub BaseMethod()
        ' Add code here to define behavior for DerivedClass.EndSubEndClass

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.

See Also

Other Resources

Inheritance in Visual Basic

Interfaces in Visual Basic