This documentation is archived and is not being maintained.

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:

  • All classes are inheritable by default 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 permit multiple inheritance, Visual Basic .NET permits 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 .NET 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 methods from its base class. If an inherited property or method needs 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. Public methods are NotOverridable by default.
  • 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 overriding 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 need 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 appearing 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 illegal:
    MyBase.MyBase.BtnOK_Click()   ' Syntax error.
  • 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 allows you to call an Overridable method implemented in your class and make sure that implementation of the method in this class is called rather than 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

Using Inherits to Create a Derived Class | Inherits Statement | Overriding Properties and Methods | Language Changes in Visual Basic