VB.NET MustInherit Class: Shadows and Overloads

Use the MustInherit keyword to create a base class. Specify the Shadows and Overloads keywords.

MustInherit. The MustInherit keyword changes a Class. It makes the Class only be used when Classes inherit from it. A MustInherit Class cannot be instantiated directly. Instead it serves as a template for independent yet derived Classes.Class

Example. We introduce a Class called Page that is a MustInherit class. We then show a Class called TextPage that inherits from Page. In the Main Sub, we create a new instance of TextPage. We then call Number() from the Page base Class.

Note: You cannot create a new instance of Page with New Page. A MustInherit class is not used this way.

Tip: The MustInherit Page class is part of TextPage, but we do not instantiate it directly.

VB.NET program that uses MustInherit MustInherit Class Page Public Function Number() As Integer Return 1 End Function End Class Class TextPage Inherits Page End Class Module Module1 Sub Main() ' Create new TextPage. Dim t As TextPage = New TextPage ' Write result of Number Function. Console.WriteLine(t.Number()) End Sub End Module Output 2 100

Intermediate language. In the .NET Framework every program is implemented with intermediate language instructions. The MustInherit keyword is changed to the abstract modifier. So in essence a MustInherit class is an abstract class.

Tip: In the C# language, this terminology (the keyword abstract) is directly used.

IL for Page .class private abstract auto ansi ConsoleApplication1.Page extends [mscorlib]System.Object { } // end of class ConsoleApplication1.Page IL for TextPage .class private auto ansi ConsoleApplication1.TextPage extends ConsoleApplication1.Page { } // end of class ConsoleApplication1.TextPage

Overloads, Shadows. A complex interaction exists between a MustInherit Class and its derived Class. In the derived Class (TextPage), we can provide an Overloads Function. Then the base implementation (of Number) is ignored when a TextPage instance is used.

Instead, the Overloads implementation is used. Similarly we can use a Shadows field on a derived class. This means that when the derived class is used, the Shadows field is always used. No confusion exists.

Note: This program does not use the _value field on Page or the Number() Function on Page. Instead it uses only those members on TextPage.

VB.NET program that uses Shadows and Overloads MustInherit Class Page ''' <summary> ''' Field. ''' </summary> Public _value As Integer ''' <summary> ''' A base implementation. ''' </summary> Public Function Number() As Integer Return 1 End Function End Class Class TextPage Inherits Page ''' <summary> ''' Shadows the _value field. ''' </summary> Public Shadows _value As Integer ''' <summary> ''' Overloads the Base Function. ''' </summary> Public Overloads Function Number() As Integer Return 2 End Function End Class Module Module1 Sub Main() ' Overloads Number Function is used. Dim t As TextPage = New TextPage Console.WriteLine(t.Number()) ' Use Shadows field. t._value = 100 Console.WriteLine(t._value) End Sub End Module Output 2 100

NotInheritable. A NotInheritable class cannot be derived from by another class. This may impart some performance advantages—please see my testing on the "sealed" keyword in the C# language. Common classes, like String, are NotInheritable.Sealed
VB.NET program that uses NotInheritable, causes error NotInheritable Class Test End Class Class Test2 Inherits Test ' This does not compile. End Class Module Module1 Sub Main() End Sub End Module Output Error 1 'Test2' cannot inherit from class 'Test' because 'Test' is declared 'NotInheritable'.

Discussion. The MustInherit keyword is useful in many programs. In one common design, I use a MustInherit Class type as the value in a Dictionary. Then I use derived types as the elements in the Dictionary.Dictionary

With this pattern, we can access any key and call a Function on the Value from the MustInherit Class. And then all values can act differently but be used in the same way. This is a simple program design.

Summary. The MustInherit keyword specifies that a class is to be used only as a template. It cannot be used directly. We create new classes, with the Inherits keyword, that use this template.

And: Features can be shared. Code can be reused. And programs become simpler and faster.

Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to