In VB, you call the base class version using the MyBase
identifier, as shown:
Imports System
Public Class A
Public Overridable Sub SomeMethod()
Console.WriteLine("A.SomeMethod")
End Sub
End Class
Public Class B
Inherits A
Public Overrides Sub SomeMethod()
Console.WriteLine("B.SomeMethod")
MyBase.SomeMethod()
End Sub
End Class
Public Class EntryPoint
Shared Sub Main()
Dim objB As B = New B
Dim objA As A = objB
objA.SomeMethod()
End Sub
End Class
The output of the previous code prints the following:
CHAPTER 4 n METHODS, PROPERTIES, AND FIELDS 76
B.SomeMethod
A.SomeMethod
Inheritance with overridden methods can increase the amount of documentation that
you should provide the consumers of your class. This documentation should include both
public and protected methods, the overridable methods, and must clearly state whether the
base class should call them and when.
If you follow the Non-Virtual Interface (NVI) pattern described in Chapter 14, the
overridable method in question will be protected.
NotInheritable Methods
Reasons stated in the last chapter point out why you should seal your classes by default and
only make classes inheritable in well-thought-out circumstances. Inheritance, coupled with
overridable methods, can add a great deal of complexity to your classes.
Pages:
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151