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
detailsA MustInherit
Class
cannot be instantiated directly. Instead it serves as a template. A separate class
that uses the MustInherit
class
is always needed.
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.
Main
Sub
, we create a new instance of TextPage
. We then call Number()
from the Page base Class
.MustInherit
class
is not used this way.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 Module2 100
In .NET IL the MustInherit
keyword is changed to the abstract
modifier. So in essence a MustInherit
class
is an abstract
class
.
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
A complex interaction exists between a MustInherit
Class
and its derived Class
. In the derived Class
(TextPage
), we can provide an Overloads Function.
TextPage
instance is used.class
.class
is used, the Shadows field is always used. No confusion exists.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 Module2 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 ModuleError 1 'Test2' cannot inherit from class 'Test' because 'Test' is declared 'NotInheritable'.
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
.
MustInherit
Class
.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.