description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | ||||
---|---|---|---|---|---|---|---|---|---|
Learn more about: Inherits Statement |
Inherits Statement |
07/20/2015 |
|
|
9e6fe042-9af3-4341-8093-fc3537770cf2 |
Causes the current class or interface to inherit the attributes, variables, properties, procedures, and events from another class or set of interfaces.
Inherits basetypenames
Term | Definition |
---|---|
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. |
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.
-
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 aFriend
interface.An interface cannot inherit from an interface nested within it.
An example of class inheritance in the .NET Framework is the xref:System.ArgumentException class, which inherits from the xref:System.SystemException class. This provides to xref:System.ArgumentException all the predefined properties and procedures required by system exceptions, such as the xref:System.Exception.Message%2A property and the xref:System.Exception.ToString%2A method.
An example of interface inheritance in the .NET Framework is the xref:System.Collections.ICollection interface, which inherits from the xref:System.Collections.IEnumerable interface. This causes xref:System.Collections.ICollection to inherit the definition of the enumerator required to traverse a collection.
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
.
[!code-vbVbVbalrStatements#37]
The following example shows inheritance of multiple interfaces.
[!code-vbVbVbalrStatements#38]
The interface named thisInterface
now includes all the definitions in the xref:System.IComparable, xref:System.IDisposable, and xref:System.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.