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.
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 AsInteger Private m_y AsInteger
PublicProperty X() AsInteger Get Return m_x EndGet Set(ByVal value AsInteger) m_x = value EndSet EndProperty
PublicProperty Y() AsInteger Get Return m_y EndGet Set(ByVal value AsInteger) m_y = value EndSet EndProperty
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 AsNew First With obj .X = 10 .Y = 20 Me.Label1.Text = .X + .Y EndWith EndSub
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.
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 AsNew Second With obj .X = 10 .Y = 20 Me.Label1.Text = .X + .Y EndWith EndSub
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.
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 AsInteger PublicProperty Z() AsInteger Get Return m_z EndGet Set(ByVal value AsInteger) m_z = value EndSet EndProperty
PublicFunction getSum() AsInteger Dim s AsInteger s = Me.X + Me.Y + Me.Z Return s EndFunction 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 AsNew Second With obj .X = 10 .Y = 20 .Z = 30 Me.Label1.Text = .getSum EndWith EndSub
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
PublicFunction getProduct() AsInteger Dim s AsInteger s = Me.X * Me.Y * Me.Z Return s EndFunction
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 AsNew Third With obj .X = 10 .Y = 20 .Z = 30 Me.Label1.Text = "Sum = " & .getSum & ", Product = " & .getProduct EndWith EndSub
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 AsInteger Private m_y AsInteger
PublicSubNew() m_x = 10 m_y = 10 EndSub
...
End Class
Public Class Second Inherits First Private m_z AsInteger PublicSubNew() m_z = 20 EndSub ... End Class
Public Class Third Inherits Second
PublicFunction getProduct() AsInteger Dim s AsInteger s = Me.X * Me.Y * Me.Z Return s EndFunction ...
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 AsNew Third With obj Me.Label1.Text = "Sum = " & .getSum & ", Product = " & .getProduct EndWith EndSub
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.
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 AsInteger Private m_y AsInteger
PublicSubNew(ByVal a AsInteger, ByVal b AsInteger) m_x = a m_y = b EndSub
... End Class
Public Class Second Inherits First
Private m_z AsInteger PublicSubNew(ByVal a AsInteger, ByVal b AsInteger, ByVal c AsInteger) MyBase.New(a, b) m_z = c EndSub
... End Class
Public Class Third Inherits Second
PublicSubNew() MyBase.New(0, 0, 0) EndSub
PublicSubNew(ByVal a AsInteger, ByVal b AsInteger, ByVal c AsInteger) MyBase.New(10, 20, 30) EndSub
...
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 AsNew Third(10, 20, 30) With obj Me.Label1.Text = "Sum = " & .getSum & ", Product = " & .getProduct EndWith EndSub
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.