Home >>VB.Net Built-in Functions >VB.Net Program to demonstrate the NotInheritable class

VB.Net Program to demonstrate the NotInheritable class

Write a VB.Net Program to demonstrate the NotInheritable class

We will create a NotInheritable Sample1 class here and then create a normal Sample2 class. In any class, a not-inheritable class can not be inherited.

Program :

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

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


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

    Class Sample2
        'We cannot inherit a NotInheritable class 
        'inside the VB.NET program.
        'Inherits Sample1
        Sub Fun2()
            Console.WriteLine("Fun2() called")
        End Sub

    End Class

    Sub Main()
        Dim S1 As New Sample1()
        Dim S2 As New Sample2()

        S1.Fun1()
        S2.Fun2()
    End Sub
End Module

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

We created a Module1 module in the program above. Here, we created Sample1 in a NotInheritable class. Within every class in the VB.Net program, we do not inherit a Not Inheritable class. Then we built a Sample2 class that includes the method Fun2().

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


No Sidebar ads