Understanding Inheritance

Bookmarks

.netFramework class library (base class library - bcl)

Inheritance Basics (Visual Basic)

(Visual Basic) 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.)

Inherits Statement

Causes the current class or interface to inherit the attributes, variables, properties, procedures, and events from another class or set of interfaces.

Remarks

If used, the Inherits statement must be the first non-blank, non-comment line in a class or interface definition. It should immediately follow the Class or Interface statement.

You can use Inherits only in a class or interface. This means the declaration context for an inheritance cannot be a source file, namespace, structure, module, procedure, or block.

Rules

  • Class Inheritance. If a class uses the Inherits statement, you can specify only one base class.

A class cannot inherit from a class nested within it.

  • Interface Inheritance. If an interface uses the Inherits statement, you can specify one or more base interfaces. You can inherit from two interfaces even if they each define a member with the same name. If you do so, the implementing code must use name qualification to specify which member it is implementing.

An interface cannot inherit from another interface with a more restrictive access level. For example, a Public interface cannot inherit from a Friend interface.

An interface cannot inherit from an interface nested within it.

An example of class inheritance in the .NET Framework is the ArgumentException class, which inherits from the SystemException class. This provides to ArgumentException all the predefined properties and procedures required by system exceptions, such as the Message property and the ToString method.

An example of interface inheritance in the .NET Framework is the ICollection interface, which inherits from the IEnumerable interface. This causes ICollection to inherit the definition of the enumerator required to traverse a collection.

MustInherit (Visual Basic)

Specifies that a class can be used only as a base class and that you cannot create an object directly from it.

Remarks

The purpose of a base class (also known as an abstract class) is to define functionality that is common to all the classes derived from it. This saves the derived classes from having to redefine the common elements. In some cases, this common functionality is not complete enough to make a usable object, and each derived class defines the missing functionality. In such a case, you want the consuming code to create objects only from the derived classes. You use MustInherit on the base class to enforce this.

Another use of a MustInherit class is to restrict a variable to a set of related classes. You can define a base class and derive all these related classes from it. The base class does not need to provide any functionality common to all the derived classes, but it can serve as a filter for assigning values to variables. If your consuming code declares a variable as the base class, Visual Basic allows you to assign only an object from one of the derived classes to that variable.

The .NET Framework defines several MustInherit classes, among them Array, Enum, and ValueType. ValueType is an example of a base class that restricts a variable. All value types derive from ValueType. If you declare a variable as ValueType, you can assign only value types to that variable.

Rules

  • Declaration Context. You can use MustInherit only in a Class statement.
  • Combined Modifiers. You cannot specify MustInherit together with NotInheritable in the same declaration.

NotInheritable

Specifies that a class cannot be used as a base class.

Remarks

Alternate Terms. A class that cannot be inherited is sometimes called a sealed class.

The NotInheritable modifier can be used in this context:

FileStream Class

Exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations.

Remarks

Use the FileStream class to read from, write to, open, and close files on a file system, and to manipulate other file-related operating system handles, including pipes, standard input, and standard output. You can use the Read, Write, CopyTo, and Flush methods to perform synchronous operations, or the ReadAsync, WriteAsync, CopyToAsync, and FlushAsync methods to perform asynchronous operations. Use the asynchronous methods to perform resource-intensive file operations without blocking the main thread. This performance consideration is particularly important in a Windows Store app or desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working. FileStream buffers input and output for better performance.

Stream Class

Provides a generic view of a sequence of bytes. This is an abstract class.

With...End With Statement (Visual Basic)

Executes a series of statements that repeatedly refer to a single object or structure so that the statements can use a simplified syntax when accessing members of the object or structure. When using a structure, you can only read the values of members or invoke methods, and you get an error if you try to assign values to members of a structure used in a With...End With statement.

Overrides (Visual Basic)

Specifies that a property or procedure overrides an identically named property or procedure inherited from a base class.

Remarks

Rules

  • Declaration Context. You can use Overrides only in a property or procedure declaration statement.
  • Combined Modifiers. You cannot specify Overrides together with Shadows or Shared in the same declaration. Because an overriding element is implicitly overridable, you cannot combine Overridable with Overrides.
  • Matching Signatures. The signature of this declaration must exactly match the signature of the property or procedure that it overrides. This means the parameter lists must have the same number of parameters, in the same order, with the same data types.

In addition to the signature, the overriding declaration must also exactly match the following:

  • The access level
  • The return type, if any
  • Generic Signatures. For a generic procedure, the signature includes the number of type parameters. Therefore, the overriding declaration must match the base class version in that respect as well.
  • Additional Matching. In addition to matching the signature of the base class version, this declaration must also match it in the following respects:
  • Shadowing and Overriding. Both shadowing and overriding redefine an inherited element, but there are significant differences between the two approaches. For more information, see Shadowing in Visual Basic.

The Overrides modifier can be used in these contexts:

Function Statement

Property Statement

Sub Statement

MustOverride (Visual Basic)

Visual Studio 2013

Specifies that a property or procedure is not implemented in this class and must be overridden in a derived class before it can be used.

Remarks

You can use MustOverride only in a property or procedure declaration statement. The property or procedure that specifies MustOverride must be a member of a class, and the class must be marked MustInherit (Visual Basic).

Rules

  • Incomplete Declaration. When you specify MustOverride, you do not supply any additional lines of code for the property or procedure, not even the End Function, End Property, or End Sub statement.
  • Combined Modifiers. You cannot specify MustOverride together with NotOverridable, Overridable, or Shared in the same declaration.
  • Shadowing and Overriding. Both shadowing and overriding redefine an inherited element, but there are significant differences between the two approaches. For more information, see Shadowing in Visual Basic.
  • Alternate Terms. An element that cannot be used except in an override is sometimes called a pure virtual element.

The MustOverride modifier can be used in these contexts:

Function Statement (Visual Basic)

Property Statement

Sub Statement (Visual Basic)