Home >>VB.Net Built-in Functions >VB.Net Program of inheritance with the protected member

VB.Net Program of inheritance with the protected member

Write a VB.Net Program of inheritance with the protected member

Here, in the inheritance, we will show the protected member.

Program :

Below is the source code for explaining the inheritance with the protected member. The program given is compiled and successfully executed.

'VB.net program to demonstrate the inheritance

'with the protected member.


Module Module1
    Class Sample1
        Protected Sub Fun1()
            Console.WriteLine("Sample1.Fun1() called")
        End Sub
    End Class
    Class Sample2
        Inherits Sample1
        Sub Fun2()
            Fun1()
            Console.WriteLine("Sample2.Fun2() called")
        End Sub
    End Class
    Sub Main()
        Dim obj As New Sample2()
        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. Here, Sample1 is Sample2's parent class. But Sample1 includes a method that is protected and can be accessed within the Sample2 class. But we're unable to access the same method with the Sample2 class object.

Finally, we built a Main() function, it's the program's entry point, here we created the Sample2 object and called fun2() method with the Sample2 class object.


No Sidebar ads