Home >>VB.Net Built-in Functions >VB.Net program to override a base class method into derived class
Here, we can create a Sample1 base class and then inherit the Sample1 class into the Sample2 class and override the Sample1 class method into the Sample2 class.
Program :Below is the source code for override a base class method into the derived class. The program given is compiled and successfully executed.
'VB.net program to override a base class method .
'in the derived class.
Module Module1
Class Sample1
Overridable Sub Fun()
Console.WriteLine("Sample1.Fun() called")
End Sub
End Class
Class Sample2
Inherits Sample1
Overrides Sub Fun()
Console.WriteLine("Sample2.Fun() called")
End Sub
End Class
Sub Main()
Dim S2 As New Sample2()
S2.Fun()
End Sub
End Module
We created a Module1 module in the program above that comprises two groups, Sample1 and Sample2. We created the method Fun() in the Sample1 class, which is declared as overridable. Then the Sample1 class was inherited into the Sample2 class. We use the Overrides keyword to override the Fun() method in the Sample2 class.
Finally, we created a Main() function, which is the program entry point, here we created the Sample2 class object and named the fun() method, here fun() Sample2 class method will be called and the console screen will print the relevant message.