Overloading Overloading inherited methods

76 Dim b As New BaseClass Dim d As New DerivedClass Dim d2 As New DerivedClass2 b.SomeMethod d.SomeMethod d2.SomeMethod This code results in the following output: BaseClass definition DerivedClass definition BaseClass definition The SomeMethod implementation in the DerivedClass class can itself be overridden by a class deriving from DerivedClass. This can be prevented, if desired, by specifying the NotOverridable keyword in the definition of the SomeMethod method of the DerivedClass class, as shown here: Class DerivedClass Inherits BaseClass Public NotOverridable Overrides Sub SomeMethod ... End Sub End Class DerivedClass

2.14.6.7 Overloading

When two or more different methods conceptually perform the same task on arguments of different types, it is convenient to give the methods the same name. This technique is called overloading and is supported by Visual Basic .NET. For example, the following code defines an overloaded SquareRoot method that can take either a Long or a Double as a parameter: Public Function SquareRoot _ ByVal Value As Long _ As Double ... End Function Public Function SquareRoot _ ByVal Value As Double _ As Double ... End Function When a call is made to the SquareRoot method, the version called is determined by the type of the parameter passed to it. For example, the following code calls the version of the method that takes a Long: Dim result As Double = SquareRoot10 And this code calls the version that takes a Double: Dim result As Double = SquareRoot10.1 Careful readers will note that in the first case the type of the argument is actually Integer, not Long. The Long version of the method is invoked because it is the closest match to the given argument. If there were also an Integer version of the method, that version would have been invoked, because it is a better match to the given argument. The .NET runtime discussed in Chapt er 3 always attempts to 77 invoke the most appropriate version of an overloaded function, given the arguments provided. If no suitable overload is found, a compiler error occurs if Option Strict is on or a runtime exception occurs if Option Strict is off. The name of a method together with the number and types of its arguments are called the signature of the method. The signature uniquely identifies a specific overloaded version of a specific method. Note that the return type is not part of the signature. Two versions of an overloaded method are not permitted to differ only by return type.

2.14.6.8 Overloading inherited methods

A method can also overload a method in a base class. Be careful to note the difference between overloading a base-class method and overriding a base-class method. Overriding means that the base-class method and the derived-class method have the same signature and that the derived-class method is replacing the base-class method. In addition, the base-class method must be marked with the Overridable keyword. Overloading means that they have different signatures and that both methods exist as overloads in the derived class. When overloading a method defined in a base class, the derived-class method declaration must include the Overloads keyword, but the base-class method doesnt have any special keyword attached to it. Heres an example: Public Class BaseClass Public Sub SomeMethod ... End Sub End Class Public Class DerivedClass Inherits BaseClass Public Overloads Sub SomeMethodByVal i As Integer ... End Sub End Class The requirement for the Overloads keyword helps to document the fact that a base-class method is being overloaded. There is no technical reason that the compiler requires it, but it is required nevertheless to help prevent human error.

2.14.6.9 Shadowing