An inheritance relationship is one in which one type (the derived type) derives from another (the base type), such that the derived type's declaration space implicitly contains the accessible nonconstructor type members and nested types of its base type. In the following example, class
A is the base class of
B is derived from
Class A End Class Class B Inherits A End Class
A does not explicitly specify a base class, its base class is implicitly Object.
The following are important aspects of inheritance:
- Inheritance is transitive. If type C is derived from type B, and type B is derived from type A, type C inherits the type members declared in type B as well as the type members declared in type A.
- A derived type extends, but cannot narrow, its base type. A derived type can add new type members, and it can shadow inherited type members, but it cannot remove the definition of an inherited type member.
- Because an instance of a type contains all of the type members of its base type, a conversion always exists from a derived type to its base type.
- All types must have a base type, except for the type Object. Thus, Object is the ultimate base type of all types, and all types can be converted to it.
- Circularity in derivation is not permitted. That is, when a type
Bderives from a type
A, it is an error for type
Ato derive directly or indirectly from type
The following example produces a compile-time error because the classes circularly depend on each other.
Class A Inherits B End Class Class B Inherits C End Class Class C Inherits A End Class
It is valid for a class to derive from classes that are nested within it or within other classes. In the following example,
A derives from
B derives from
C does not derive from
Class A Inherits B.C End Class Class B Inherits A Public Class C End Class End Class
In the next example, class
B derives from class
A, but class
A does not derive from class
Class A Class B Inherits A End Class End Class
4.3.1 MustInherit and NotInheritable Classes | 4.3.2 Interfaces and Multiple Inheritance | 4.3.3 Shadowing | 4.1 Declarations | 4.4 Implementation | 4.5 Polymorphism | 4.6 Accessibility | 4.2 Scope | 4.7 Type and Namespace Names | 5. Attributes | Inheritance (Visual Basic Language Concepts)