Inheritance using VB.NET

In this article I will explain about Inheritance in VB.NET.
  • 2826

Inheritance is a relationship that defines one entity in terms of another. Class inheritance defines a new class in terms of one parent class or one or more interfaces. The new class inherits its interface and implementation from its parent class and method signatures. The new class is called a subclass or a derived class.

Class inheritance combines interface inheritance and implementation inheritance. Interface inheritance defines a new interface in terms of one or more existing interfaces. Implementation inheritance defines a new implementation in terms of one or more existing implementations.

Classes in VB.NET support only single inheritance, and Object is the ultimate base class for all classes. The classes shown in earlier examples all implicitly derive from Object.

Access modifiers in a classes members give different levels of access to derived classes. Below is a table that describes the access a child class has to the members of the inherited class depending upon the access modifier.

Parent class access modifier Access in child class public accessible protected accessible private not accessible

Table 5.6: Access-Specifier Members and inheritance scope

table5.6.gif

VB.NET only makes use of public inheritance, meaning that you cannot specify an inheritance modifier in VB.NET. Listing 5.50 shows an example of a class deriving inheritance with public access specified.

Listing 5.50: Inheritance Example 1


Module
Module1
    Class A
        ' derived from object behind the scenes
        Public Sub F()
            Console.WriteLine("A.F")
        End Sub
    End Class
 
    Class B
        Inherits A
        ' B inherits A
        Public Sub G()
            Console.WriteLine("B.G")
        End Sub
    End Class
 
    Class Test
        Shared Sub Main()
            Dim b As New B()
            b.F()
            ' Inherited from A
            b.G()
            ' Introduced in B
            Dim a As A = b
            ' Treat a B as an A, polymorphic access
            a.F()
            Console.ReadLine()
        End Sub
    End Class
End Module

Output Generated

5.gif

Listing 5.51: Inheritance Example 2


// example1 protected

Class
A
        Protected x As Integer = 123
    End Class
    Class B
        Inherits A
        Private Sub F()
            Dim a As New A()
            Dim b As New B()
            a.x = 10
            ' Error
            b.x = 10
            ' OK
        End Sub
    End Class

Listing 5.52 shows another example of private inheritance that enables direct access to protected members.

Listing 5.52: Protected.vb, Protected Modifier Example


// example2 protected

Module
Module1
    Class [MyClass]
        Protected x As Integer
        Protected y As Integer
    End Class
 
    Class MyDerivedC
        Inherits [MyClass]
        'private inheritance
        Public Shared Sub Main()
            Dim mC As New MyDerivedC()
            ' Direct access to protected members:
            mC.x = 10
            mC.y = 15
            Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y)
            Console.ReadLine()
        End Sub
    End Class
End Module

Output Generated

6.gif

Conclusion

Hope this article would have helped you in Inheritance in VB.NET.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.