Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

4.3 Inheritance

Visual Studio .NET 2003

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, and B is derived from A.

Class A
End Class 

Class B
    Inherits A
End Class 

Since 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 B derives from a type A, it is an error for type A to derive directly or indirectly from type B.

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. C and B derives from A, but C does not derive from B.

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 B.

Class A
    Class B
        Inherits A
    End Class 
End Class 

See Also

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)

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.