Home
VB.NET
MustInherit Class: Shadows and Overloads
This page was last reviewed on Mar 25, 2022.
Dot Net Perls
MustInherit. In VB.NET the MustInherit keyword changes a Class. It makes the Class only be used when Classes inherit from it. It is like an abstract class in C#.
Class details. A MustInherit Class cannot be instantiated directly. Instead it serves as a template. A separate class that uses the MustInherit class is always needed.
Class
Example. In this VB.NET program, we introduce a Class called Page that is a MustInherit class. We then show a Class called TextPage that inherits from Page.
Detail 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.
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
2 100
Intermediate language. In .NET IL 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 used directly.
.class private abstract auto ansi ConsoleApplication1.Page extends [mscorlib]System.Object { } // end of class ConsoleApplication1.Page
.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.
Detail The Overloads implementation is used. Similarly we can use a Shadows field on a derived class.
Tip 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.
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
2 100
NotInheritable. A NotInheritable class cannot be derived from by another class. This may impart some performance advantages. Common classes, like String, are NotInheritable.
NotInheritable Class Test End Class Class Test2 Inherits Test ' This does not compile. End Class Module Module1 Sub Main() End Sub End Module
Error 1 'Test2' cannot inherit from class 'Test' because 'Test' is declared 'NotInheritable'.
Discussion. The MustInherit keyword is useful in many programs. We can a MustInherit Class type as the value in a Dictionary. Then use derived types as the elements in the Dictionary.
Dictionary
Tip With this pattern, we can access any key and call a Function on the Value from the MustInherit Class.
And 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 25, 2022 (rewrite).
Home
Changes
© 2007-2024 Sam Allen.