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

VB.Net Program to demonstrate hierarchical inheritance

Write a VB.Net Program to demonstrate hierarchical inheritance

Here, by demonstrate three classes, Sample1, Sample2, Sample3, we can show the hierarchical inheritance. Here, for Sample2, Sample3 would be the parent class of Sample1.

Program :

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

'VB.net program to demonstrate the

'Tree or Hierarchical 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
    Class Sample3
        Inherits Sample1
        Sub Fun3()
            Console.WriteLine("Sample3.Fun3() called")
        End Sub
    End Class
    Sub Main()
        Dim S2 As New Sample2()
        Dim S3 As New Sample3()
        S2.Fun1()
        S2.Fun2()
        S3.Fun1()
        S3.Fun3()
    End Sub
End Module
	
Output:
Sample1.Fun1() called
Sample2.Fun2() called
Sample1.Fun1() called
Sample3.Fun3() called
Explanation:

We created a Module1 module in the program above. Three classes, Sample1, Sample2, and Sample3, were developed here. Sample1 here is the parent class of Sample2, the class of Sample3. Any class includes a method.

Finally, we created a Main() function, it's the program entry point, here we created the Sample2, Sample3 class object, and named fun1() methods for both objects.


No Sidebar ads