Inheritance with VB.NET 2005

This is the beginning of a new series dedicated to explaining inheritance in VB.NET 2005. This article gives you an introduction to inheritance. The upcoming articles will cover some of the advanced aspects of Visual Basic.NET 2005.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 57
July 18, 2006
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

A downloadable file for this article is available here.

The entire source code for this article is available in the form of downloadable zip.  The solution was developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft Windows Server 2003 Enterprise Edition. Even though I believe that the source code available with this contribution can work with Microsoft Visual Studio.NET 2003/2002, I didn't really test it in any other environment.  I request that you  post in the discussion area if you have any problems with the code's execution.

Developing a simple class

My previous articles and series already covered a lot of ground concerning classes, members, fields, methods, properties and so on.  If you are very new to the concept of classes, I suggest you go through my previous series before proceeding with this one.

For the sake of simplicity in explanation, I developed a simple class as follows:

Public Class First

    Private m_x As Integer
    Private m_y As Integer

    Public Property X() As Integer
        Get
            Return m_x
        End Get
        Set(ByVal value As Integer)
            m_x = value
        End Set
    End Property

 

    Public Property Y() As Integer
        Get
            Return m_y
        End Get
        Set(ByVal value As Integer)
            m_y = value
        End Set
    End Property

End Class

The above class simply contains two fields (m_x and m_y) and two properties (X, Y).  As the fields (m_x and m_y) are defined as private, they are viewable/accessible only within the class "First."  The two properties are mainly used to provide a way to interface with the two fields.

To work with the above class, you may design a form with a single button and label. The code for the button would be as follows:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As New First
        With obj
            .X = 10
            .Y = 20
            Me.Label1.Text = .X + .Y
        End With
    End Sub

Within the code above, you can observe that I am creating an object named "obj" based on the class "First." I am assigning values to the properties and retrieving the values from the same, for calculation.

Basic inheritance

In the previous section, I simply added a class ("first") with four members.  Now, add one more class named "Second" as follows:

Public Class Second
    Inherits First

End Class

At this moment, visually there exist no members in the class "Second." Now, modify your "Button1_Click" as follows:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As New Second
        With obj
            .X = 10
            .Y = 20
            Me.Label1.Text = .X + .Y
        End With
    End Sub

Once you execute the above, you will not find any difference in output. Even though the members do not exist physically (or visually), they are available "virtually," as the class is "inherited" from its parent class "first." Let me discuss this point in more detail.

If a class is defined with the word "inherits," it automatically contains each and every member of its parent/super/base class "virtually." In this case, the class "First" is considered to be the parent/super/base class, whereas the class "Second" is considered to be the child/sub/derived class. All the members existing in the class "First" (m_x, m_y, X, Y) are virtually present in the class "Second," even though you don't define them.

From now on, I shall use the term "super" class for the "parent" class and "sub" class for the "child" class, so as not to confuse you. 

A sub class can also have its own members, apart from the members which are automatically available from its super class. This can be achieved by defining your own members within the sub class.  The next section will illustrate this point.

Adding your own members to the sub class during inheritance

In the previous section, we examined a very simple and basic inheritance. In this section, I shall extend the class "Second" further as follows:

Public Class Second
    Inherits First

    Private m_z As Integer

    Public Property Z() As Integer
        Get
            Return m_z
        End Get
        Set(ByVal value As Integer)
            m_z = value
        End Set
    End Property

    Public Function getSum() As Integer
        Dim s As Integer
        s = Me.X + Me.Y + Me.Z
        Return s
    End Function
End Class

If you observe carefully, the above class contains all the following members:

  • M_x (virtual and not accessible)
  • M_y (virtual and not accessible)
  • M_z (visual and accessible)
  • X (virtual and accessible)
  • Y (virtual and accessible)
  • Z (visual and accessible)
  • getSum (visual and accessible)

To work with the above class, I can code the button click as follows:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As New Second
        With obj
            .X = 10
            .Y = 20
            .Z = 30
            Me.Label1.Text = .getSum
        End With
    End Sub

Multi-level inheritance in Visual Basic 2005

Until now, in all of the previous sections, I introduced you only to single inheritance.  Now, I shall introduce you to multi-level inheritance. 

When you define more than two levels of inheritance (in the form of a chain of classes), it would be generally referred to as multi-level inheritance. In the case of multi-level inheritance, all the members of all super classes would be automatically available within the sub class.

Let me extend the classes "first" and "second" with "third" as follows:

Public Class Third
    Inherits Second

    Public Function getProduct() As Integer
        Dim s As Integer
        s = Me.X * Me.Y * Me.Z
        Return s
    End Function

End Class

Now, you can observe that the class "third" is inherited from "second." That means all the members available in the class "second" (all virtual and visual) would be available to the class "third."  In detail, you can observe the following members of the class "third:"

  • M_x (virtual and not accessible)
  • M_y (virtual and not accessible)
  • M_z (virtual and not accessible)
  • X (virtual and accessible)
  • Y (virtual and accessible)
  • Z (virtual and accessible)
  • getSum (virtual and accessible)
  • getProduct (visual and accessible)

To access the above class, we need to modify the button click event as follows:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As New Third
        With obj
            .X = 10
            .Y = 20
            .Z = 30
            Me.Label1.Text = "Sum = " & .getSum & ", Product = " & .getProduct
        End With
    End Sub

Dealing with constructors during inheritance

I already introduced constructors in my previous series of articles covering OOPS using Visual Basic 2005. If you are very new to constructors, I strongly suggest you go through my previous series before continuing this section.

During inheritance, the constructors are executed starting from the highest super class through the lowest sub class in the form of a hierarchy.  Now, let us modify our hierarchy of classes as follows:

Public Class First

    Private m_x As Integer
    Private m_y As Integer

    Public Sub New()
        m_x = 10
        m_y = 10
    End Sub

...

End Class

Public Class Second
    Inherits First

    Private m_z As Integer

    Public Sub New()
        m_z = 20
    End Sub

...

End
Class

Public Class Third
    Inherits Second

    Public Function getProduct() As Integer
        Dim s As Integer
        s = Me.X * Me.Y * Me.Z
        Return s
    End Function
...

End Class

Now that you have modified the hierarchy as above, let us try to modify the testing code as well:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As New Third
        With obj
            Me.Label1.Text = "Sum = " & .getSum & ", Product = " & .getProduct
        End With
    End Sub

Now, you can observe from the above code that I didn't assign any values to the properties during testing.  I just created the object and all the values, which are default values, have been assigned during the execution of constructors at various levels.

Dealing with constructors with parameters during inheritance

The previous section already introduced you to constructors in inheritance.  Now, I shall extend the discussion to constructors with parameters. Let us modify our hierarchy of classes as follows:

Public Class First

    Private m_x As Integer
    Private m_y As Integer

    Public Sub New(ByVal a As Integer, ByVal b As Integer)
        m_x = a
        m_y = b
    End Sub

...
End Class

Public Class Second
    Inherits First

    Private m_z As Integer

    Public Sub New(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)
        MyBase.New(a, b)
        m_z = c
    End Sub

...
End Class

Public Class Third
    Inherits Second

    Public Sub New()
        MyBase.New(0, 0, 0)
    End Sub

    Public Sub New(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)
        MyBase.New(10, 20, 30)
    End Sub

...

End Class

Once you modify the hierarchy as above, let us try to modify the testing code as well:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As New Third(10, 20, 30)
        With obj
            Me.Label1.Text = "Sum = " & .getSum & ", Product = " & .getProduct
        End With
    End Sub

You can observe that I am using a new keyword, "MyBase." It is mainly used to access any of the super class members. In this case, I am executing the constructor of the super class by using "MyBase."

In this article, I simply wanted to explain the topics of inheritance in OOPS. The sample codes given in this article are neither the best in performance nor the best in programming methodologies.

Any feedback, suggestions, bugs, errors, improvements etc., are highly appreciated at jag_chat@yahoo.com.

blog comments powered by Disqus
VISUAL BASIC.NET ARTICLES

- Basic Form Properties and Modality in VB.NET
- Multiple Document Interfaces in Visual Basic
- Visual Basic for Beginners
- ASP.NET Image to PDF with VB.Net
- MySQL in ASP.NET: Mono using VB.NET
- AsyncFileUpload File Type and File Size Vali...
- Visual Studio: Adding Functionality and Style
- Clocks and Countdowns
- User-defined Functions using Visual Basic Ap...
- Understanding Object Binding in VBA
- Mastering the Message Box
- Testing a Windows Forms Application
- Using Visual Basic.NET Features to Code a Wi...
- Correcting Code in a Windows Forms Applicati...
- Write Readable Code and Comments for Windows...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials