Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the MustOverride keyword
By Nidhi Last Updated : November 13, 2024
MustOverride keyword in VB.Net
Here, we will create a base class Sample1 and then inherit Sample1 class into Sample2 class. In the Sample1 class method fun() will be declared using the MustOverride keyword.
Program/Source Code:
The source code to demonstrate the MustOverride keyword is given below. The given program is compiled and executed successfully.
VB.Net code to demonstrate the example of MustOverride keyword
'VB.net program to demonstrate the MustOverride keyword.
Module Module1
MustInherit Class Sample1
MustOverride Sub Fun()
End Class
Class Sample2
Inherits Sample1
'Here we need to override the Fun() method
'otherwise it will generate the error.
Overrides Sub Fun()
Console.WriteLine("Override method Fun() called")
End Sub
End Class
Sub Main()
Dim S As New Sample2()
S.Fun()
End Sub
End Module
Output:
Override method Fun() called
Press any key to continue . . .
Explanation:
In the above program, we created a module Module1 that contains two classes Sample1 and Sample2.
In the Sample1 class, we declared the Fun() method using MustOverride, if we declared a method using the MustOverride keyword then we need to declare a class using the MustInherit keyword. Then we override the Fun() method in the Sample2 class using the Overrides keyword.
At last, we created a Main() function, which is the entry point for the program, here we created the object of Sample2 class and called the Fun() method that will print the appropriate message on the console screen.
VB.Net Basic Programs »