Home >>VB.Net Built-in Functions >VB.Net Program to demonstrate inheritance with constructors
Here, by extending the feature of Sample1 class using the Inherits keyword, we can create a Sample1 class then create a new class Sample2. Both classes contain constructors, here we understand the flow of inheritance calling constructor.
Program :Below is the source code to demonstrate the simple inheritance of constructors. The program given is compiled and successfully executed.
'VB.net program to demonstrate the simple
'inheritance with constructors.
Module Module1
Class Sample1
Sub New()
Console.WriteLine("Sample1:Constructor called")
End Sub
Sub Fun1()
Console.WriteLine("Sample1.Fun1() called")
End Sub
End Class
Class Sample2
Inherits Sample1
Sub New()
Console.WriteLine("Sample2:Constructor called")
End Sub
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
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 and a constructor are included in the Sample1 class that will print text messages 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, we created the Sample2 class object here, so it calls the creator, but we inherited the Sample1 class here. That's why it would first call the parent class constructor and then call the child class builder. After that, we called the fun1() and fun2() methods that will print the console screen with an appropriate message.