Home >>VB.Net Built-in Functions >VB.Net Program to demonstrate inheritance

VB.Net Program to demonstrate inheritance

Write a VB.Net Program to demonstrate inheritance

Here, we can build a Sample1 class then create a new class Sample2 by extending the feature of Sample1 class using the Inherits keyword.

Program :

The source code is given below to display the simple inheritance. The program given is compiled and successfully executed.

'VB.net program to demonstrate the simple inheritance.


Module Module1
    Class Sample1
        Sub Fun1()
            Console.WriteLine("Sample1.Fun1() called")
        End Sub
    End Class
    Class Sample2
        Inherits Sample1
        Sub Fun2()
            Console.WriteLine("Sample2.Fun2() called")
        End Sub
    End Class
    Sub Main()
        Dim obj As New Sample2()
        obj.Fun1()
        obj.Fun2()
    End Sub
End Module
	
Output:
Sample1.Fun1() called
Sample2.Fun2() called
Explanation:

We created a Module1 module in the program above. We built two classes here, Sampl1 and Sample2, where the parent class is Sample1 and the child class is Sample2.

A fun1() method is included in the Sample1 class that prints a text message on the console screen.

The Sample2 class inherits the Sample1 class features, which also includes the fun2() method.

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


No Sidebar ads