Home >>VB.Net Built-in Functions >VB.Net program to demonstrate the abstract class

VB.Net program to demonstrate the abstract class

Write a VB.Net program to demonstrate the abstract class

Here, using the MustInherit keyword, we can create an abstract class. An abstract class includes a method that is abstract. The MustOverride keyword is used to create abstract methods here.

Program :

Below is the source code to demonstrate the abstract class. The program given is compiled and successfully executed.

'VB.net program to demonstrate the abstract class.

Module Module1
    MustInherit Class Sample1
        MustOverride Sub Fun1()
        MustOverride Sub Fun2()
    End Class
    Class Sample2
        Inherits Sample1
        Overrides Sub Fun1()
            Console.WriteLine("Override method Fun1() called")
        End Sub
        Overrides Sub Fun2()
            Console.WriteLine("Override method Fun2() called")
        End Sub
    End Class
    Sub Main()
        Dim S As New Sample2()
    End Sub
End Module
Override method Fun1() called
Override method Fun2() called

We created a Module1 module in the program above that comprises two classes, Sample1 and Sample2.

Here, an abstract class comprising two abstract methods is Sample1. The object of an abstract class can not be created. Here, the Sample1 class methods are overridden in the Sample2 class.

Finally, we created a Main() function, which is the program entry point, where we created the Sample2 class object, and called both Fun1() and Fun2() methods, which will print the required message on the console screen.

No Sidebar ads