Overloading Generic Methods
A generic method may be overloaded. A class can provide two or more generic methods with the same name but different method parameters. For example, we could provide a second version of generic method PrintArray (Fig. 26.3) with the additional parameters lowIndex and highIndex that specify the portion of the array to output (see Exercise 26.8). A generic method can also be overloaded by another generic method with the same method name and a different number of type parameters, or by a generic method with different numbers of type parameters and method parameters.
A generic method can be overloaded by non-generic methods that have the same method name and number of parameters. When the compiler encounters a method call, it searches for the method declaration that most precisely matches the method name and the argument types specified in the call. For example, generic method PrintArray of Fig. 26.3 could be overloaded with a version specific to strings that outputs the strings in neat, tabular format (see Exercise 26.9). If the compiler cannot match a method call to either a non-generic method or a generic method, or if there is ambiguity due to multiple possible matches, the compiler generates an error. Generic methods can also be overloaded by non-generic methods that have the same method name but a different number of method parameters.