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

VB.Net program to demonstrate the MustInherit class

Write a VB.Net program to demonstrate the MustInherit class

Here, we can build a MustInherit class Sample1 and then inherit the Sample1 class into Sample2 class. We cannot create the object of MustInherit class.

Program :

The source code to demonstrate the MustInherit class is given below. The program given is successfully compiled and executed.

'VB.net program to demonstrate the "MustInherit" class.



Module Module1
    MustInherit Class Sample1
        Sub Fun1()
            Console.WriteLine("Fun1() called")
        End Sub
    End Class

    Class Sample2
        Inherits Sample1
        Sub Fun2()
            Console.WriteLine("Fun2() called")
        End Sub

    End Class

    Sub Main()
        'We cannot create the object of mustinherit class, 
        'the below statement will generate an error
        'Dim S1 As New Sample1()

        Dim S2 As New Sample2()
        S2.Fun1()
        S2.Fun2()
    End Sub
End Module

	
Output:
Fun1() called
Fun2() called
Explanation:

We created a Module1 module in the program above. We built a class of Sample1 here, which is declared as MustInherit. The Fun1() method is used in Sample1 and the Sample1 class was then inherited into the Sample2 class. We can't build the MustInherit object in the VB.Net class.

Finally, we created a Main() function, this is the program entry point, here we created the Sample2 class object and named the methods Fun1(), Fun2() which will print a suitable message on the console screen.


No Sidebar ads