Inheritance Basics

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, and can extend, the properties, methods, events, fields, and constants defined in the base class. The following section describes some of the rules for inheritance, and the modifiers you can use to change the way classes inherit or are inherited:

  • By default, all classes are inheritable unless marked with the NotInheritable keyword. Classes can inherit from other classes in your project or from classes in other assemblies that your project references.

  • Unlike languages that allow multiple inheritance, Visual Basic allows only single inheritance in classes; that is, derived classes can have only one base class. Although multiple inheritance is not allowed in classes, classes can implement multiple interfaces, which can effectively accomplish the same ends.

  • To prevent exposing restricted items in a base class, the access type of a derived class must be equal to or more restrictive than its base class. For example, a Public class cannot inherit a Friend or a Private class, and a Friend class cannot inherit a Private class.

Inheritance Modifiers

Visual Basic introduces the following class-level statements and modifiers to support inheritance:

  • Inherits statement — Specifies the base class.

  • NotInheritable modifier — Prevents programmers from using the class as a base class.

  • MustInherit modifier — Specifies that the class is intended for use as a base class only. Instances of MustInherit classes cannot be created directly; they can only be created as base class instances of a derived class. (Other programming languages, such as C++ and C#, use the term abstract class to describe such a class.)

Overriding Properties and Methods in Derived Classes

By default, a derived class inherits properties and methods from its base class. If an inherited property or method has to behave differently in the derived class it can be overridden. That is, you can define a new implementation of the method in the derived class. The following modifiers are used to control how properties and methods are overridden:

  • Overridable — Allows a property or method in a class to be overridden in a derived class.

  • Overrides — Overrides an Overridable property or method defined in the base class.

  • NotOverridable — Prevents a property or method from being overridden in an inheriting class. By default, Public methods are NotOverridable.

  • MustOverride — Requires that a derived class override the property or method. When the MustOverride keyword is used, the method definition consists of just the Sub, Function, or Property statement. No other statements are allowed, and specifically there is no End Sub or End Function statement. MustOverride methods must be declared in MustInherit classes.

For more information about overriding methods, see Overriding Properties and Methods.

The MyBase Keyword

You can use the MyBase keyword to call methods in a base class when you override methods in a derived class. For example, suppose you are designing a derived class that overrides a method inherited from the base class. The overridden method can call the method in the base class and modify the return value as shown in the following code fragment:

Class DerivedClass
    Inherits BaseClass
    Public Overrides Function CalculateShipping( _
        ByVal Dist As Double, _
        ByVal Rate As Double) _
        As Double 
        ' Call the method in the base class and modify the return value. 
        Return MyBase.CalculateShipping(Dist, Rate) * 2
    End Function 
End Class

The following list describes restrictions on using MyBase:

  • MyBase refers to the immediate base class and its inherited members. It cannot be used to access Private members in the class.

  • MyBase is a keyword, not a real object. MyBase cannot be assigned to a variable, passed to procedures, or used in an Is comparison.

  • The method that MyBase qualifies does not have to be defined in the immediate base class; it may instead be defined in an indirectly inherited base class. In order for a reference qualified by MyBase to compile correctly, some base class must contain a method matching the name and types of parameters that appear in the call.

  • You cannot use MyBase to call MustOverride base class methods.

  • MyBase cannot be used to qualify itself. Therefore, the following code is not valid:


  • MyBase cannot be used in modules.

  • MyBase cannot be used to access base class members that are marked as Friend if the base class is in a different assembly.

The MyClass Keyword

The MyClass keyword enables you to call an Overridable method implemented in your class and make sure that implementation of the method in this class is called instead of an overridden method in a derived class.

  • MyClass is a keyword, not a real object. MyClass cannot be assigned to a variable, passed to procedures, or used in an Is comparison.

  • MyClass refers to the containing class and its inherited members.

  • MyClass can be used as a qualifier for Shared members.

  • MyClass cannot be used in standard modules.

  • MyClass can be used to qualify a method that is defined in a base class and that has no implementation of the method provided in that class. Such a reference has the same meaning as MyBase.Method.

See Also


How to: Create Derived Classes


Overriding Properties and Methods


Inherits Statement

Other Resources

Language Changes for Visual Basic 6.0 Users