Inheritance with VB.NET 2005 - Multi-level inheritance in Visual Basic 2005
(Page 4 of 6 )
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
Next: Dealing with constructors during inheritance >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee