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

VB.Net Program to demonstrate inheritance with destructors

Write a VB.Net Program to demonstrate inheritance with destructors

Here, by extending the feature of Sample1 class using the Inherits keyword, we can build a Sample1 class then create a new class Sample2. All classes have destructors, here we understand the flow of inheritance calling destructor.

Program :

The source code is given below to show the basic inheritance with destructors. The program given is compiled and successfully executed.

'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
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 and a destructor are included in the Sample1 class that will print text messages on the console screen. The Sample2 class inherits the Sample1 class feature, which also includes a fun2() and a destructor method.

Finally, we created a Main() function, it's the program entry point, here we created the Sample2 class object, and called fun1() and fun2() methods, and then the destructor is called when an object gets destroyed and a message is written on the console screen.


No Sidebar ads