Udostępnij za pośrednictwem


Inherits Statement

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

Inherits basetypenames

Parts

  • basetypenames
    Required. The name of the class from which this class derives.

    -or-

    The names of the interfaces from which this interface derives. Use commas to separate multiple names.

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.

Example

The following example uses the Inherits statement to show how a class named thisClass can inherit all the members of a base class named anotherClass.

Public Class thisClass
    Inherits anotherClass
    ' Add code to override, overload, or extend members  
    ' inherited from the base class. 
    ' Add new variable, property, procedure, and event declarations. 
End Class

The following example shows inheritance of multiple interfaces.

Public Interface thisInterface
    Inherits IComparable, IDisposable, IFormattable
    ' Add new property, procedure, and event definitions. 
End Interface

The interface named thisInterface now includes all the definitions in the IComparable, IDisposable, and IFormattable interfaces The inherited members provide respectively for type-specific comparison of two objects, releasing allocated resources, and expressing the value of an object as a String. A class that implements thisInterface must implement every member of every base interface.

See Also

Concepts

Interface Implementation Examples in Visual Basic

Reference

MustInherit

NotInheritable

MyBase

MyClass

Other Resources

Inheritance in Visual Basic