ARTICLE

Inheritance in VB.NET

Posted by VB Developer Articles | VB.NET Language August 16, 2009
This article discusses inheritance property of object oriented programming using Visual Basic .NET.
 
Reader Level:

Inheritance is a technique to derive a type from another type and the derived type inherits the functionality of the type it is derived from. The derived type is called a derived, child, or inherited type and the type it is derived from is called a base or parent type.

In Visual Basic .NET, the Inherits keyword is used to derive a class.  Syntax of Inherits keyword looks like this code snippet, where ClassB is the base class and ClassA is the derived class.

Public Class ClassB

    Inherits ClassA

End Class

Here is another example where Author class is derived from Person class.

Public Class Author

    Inherits Person

End Class

Once a class is inherited from a base class, it inherits all properties, attributes, and functions from the base class.

Let's see a complete example.

I create a Person class with two properties Name and Age as you can see in Listing 1.

 

''' <summary>

''' Person class with Name and Age properties

''' </summary>

''' <remarks></remarks>

Public Class Person

    Protected pName As String

    Protected pAge As Integer

 

    ''' <summary>

    ''' Name of the person

    ''' </summary>

    ''' <value></value>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Public Property Name() As String

        Get

            Return Me.pName

        End Get

        Set(ByVal value As String)

            Me.pName = value

        End Set

    End Property

 

    ''' <summary>

    ''' Age of the person

    ''' </summary>

    ''' <value></value>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Public Property Age() As Integer

        Get

            Return Me.pAge

        End Get

        Set(ByVal value As Integer)

            Me.pAge = value

        End Set

    End Property

End Class

 

Listing 1

 

Now, I inherit a class called Author from Person class. Class Author has three properties BookTitle, PublishedYear, and BookPublisher as you can see in Listing 2.

''' <summary>

''' Author class: Inherited from Person

''' </summary>

''' <remarks></remarks>

Public Class Author

    Inherits Person

 

    Protected title As String

    Protected pubYear As Integer

    Protected publisher As String

 

    Public Property BookTitle() As String

        Get

            Return Me.title

        End Get

        Set(ByVal value As String)

            Me.title = value

        End Set

    End Property

 

    Public Property PublishedYear() As Integer

        Get

            Return Me.pubYear

        End Get

        Set(ByVal value As Integer)

            Me.pubYear = value

        End Set

    End Property

 

    Public Property BookPublisher() As String

        Get

            Return Me.publisher

        End Get

        Set(ByVal value As String)

            Me.publisher = value

        End Set

    End Property

 

 

End Class

Listing 2

 

So now I have Author class inherited from Person class. That means, the Author class must have all properties defined in the Person class.

 

So in my code that uses the Author class, I write code, I see the Name and Age properties defined in the Person class are available when I create an instance of the Author class and use its properties.

 

As you can see from code sample in Listing 3, I set and display Name and Age properties of Author class instance.

 

Module Module1

 

    Sub Main()

 

        ' Create a new author

        Dim auth As New Author

 

        ' Set author properties

        auth.Name = "Mahesh Chand"

        auth.Age = 30

        auth.BookTitle = "Graphics Programming with GDI+"

        auth.BookPublisher = "Addison Wesley"

        auth.PublishedYear = 2003

 

        ' Display author details

        Console.WriteLine("Author Name: " + auth.Name)

        Console.WriteLine("Author Age: " + auth.Age.ToString())

        Console.WriteLine("Book Title: " + auth.BookTitle)

        Console.WriteLine("Book Publisher: " + auth.BookPublisher)

        Console.WriteLine("Published Year: " + auth.PublishedYear.ToString())

 

        Console.ReadLine()

 

    End Sub

 

End Module

Listing 3

 

 

Important Rules of Inheritance

  1. .NET Framework, the Object type is mother of all. All types are inherited from the Object type by default. In our code above, the Person class is derived from the Object type.
  2. Inheritance is transitive. If type C is derived from type B, and type B is derived from type A, type C inherits the type members declared in type B as well as the type members declared in type A.
  3. A derived type extends, but cannot narrow, its base type. A derived type can add new type members, and it can shadow inherited type members, but it cannot remove the definition of an inherited type member.
  4. Because an instance of a type contains all of the type members of its base type, a conversion always exists from a derived type to its base type.
  5. All types must have a base type, except for the type Object. Thus, Object is the ultimate base type of all types, and all types can be converted to it.
  6. Circularity in derivation is not permitted. That is, when a type B derives from a type A, it is an error for type A to derive directly or indirectly from type B.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor