Inheritance with VB.NET 2005 - Basic inheritance
(Page 2 of 6 )
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.
Next: Adding your own members to the sub class during inheritance >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee