C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
Tip: In the C# language, this terminology (the keyword abstract) is directly used.
AbstractIL 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
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
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'.
And: Features can be shared. Code can be reused. And programs become simpler and faster.