Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the simple inheritance with destructors
By Nidhi Last Updated : November 13, 2024
Simple Inheritance with Destructors Example in VB.Net
Here, we will create a Sample1 class then create a new class Sample2 by extending the feature of Sample1 class using the Inherits keyword, both classes contain destructors, here we understand the flow of destructor calling in inheritance.
Program/Source Code:
The source code to demonstrate the simple inheritance with destructors is given below. The given program is compiled and executed successfully.
VB.Net code to implement the simple inheritance with destructors
'VB.net program to demonstrate the simple inheritance
'with destructors.
Module Module1
Class Sample1
Sub Fun1()
Console.WriteLine("Sample1.Fun1() called")
End Sub
Protected Overrides Sub Finalize()
Console.WriteLine("Sample1:Destructor called")
End Sub
End Class
Class Sample2
Inherits Sample1
Sub Fun2()
Console.WriteLine("Sample2.Fun2() called")
End Sub
Protected Overrides Sub Finalize()
Console.WriteLine("Sample2:Destructor 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
Sample2:Destructor called
Press any key to continue . . .
Explanation:
In the above program, we created a module Module1. Here, we created two classes Sampl1 and Sample2, Here, Sample1 is the parent class and Sample2 is the child class.
The Sample1 class contains a fun1() method and a destructor that will print text messages on the console screen. The Sample2 class inherits the feature of the Sample1 class, it also contains a fun2() method and a destructor.
At last, we created a Main() function, it is the entry point for the program, here we created the object of Sample2 class, and called methods fun1() and fun2(), and then the destructor is called when an object gets destroyed and print a message on the console screen.
VB.Net Basic Programs »