Motivation for Generic Methods
Overloaded methods are often used to perform similar operations on different types of data. To motivate generic methods, let's begin with an example (Fig. 18.1) that contains three overloaded printArray methods (lines 714, lines 1724 and lines 2734). These methods print the string representations of the elements of an Integer array, a Double array and a Character array, respectively. Note that we could have used arrays of primitive types int, double and char in this example. We chose to use arrays of type Integer, Double and Character to set up our generic method example, because only reference types can be used with generic methods and classes.
Figure 18.1. Printing array elements using overloaded methods.
(This item is displayed on pages 871 - 872 in the print version)
1 // Fig. 18.1: OverloadedMethods.java 2 // Using overloaded methods to print array of different types. 3 4 public class OverloadedMethods 5 { 6 // method printArray to print Integer array 7 public static void printArray( Integer[] inputArray ) 8 { 9 // display array elements 10 for ( Integer element : inputArray ) 11 System.out.printf( "%s ", element ); 12 13 System.out.println(); 14 } // end method printArray 15 16 // method printArray to print Double array 17 public static void printArray( Double[] inputArray ) 18 { 19 // display array elements 20 for ( Double element : inputArray ) 21 System.out.printf( "%s ", element ); 22 23 System.out.println(); 24 } // end method printArray 25 26 // method printArray to print Character array 27 public static void printArray( Character[] inputArray ) 28 { 29 // display array elements 30 for ( Character element : inputArray ) 31 System.out.printf( "%s ", element ); 32 33 System.out.println(); 34 } // end method printArray 35 36 public static void main( String args[] ) 37 { 38 // create arrays of Integer, Double and Character 39 Integer[] integerArray = { 1, 2, 3, 4, 5, 6 }; 40 Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; 41 Character[] characterArray = { 'H', 'E', 'L', 'L', 'O' }; 42 43 System.out.println( "Array integerArray contains:" ); 44 printArray( integerArray ); // pass an Integer array 45 System.out.println( " Array doubleArray contains:" ); 46 printArray( doubleArray ); // pass a Double array 47 System.out.println( " Array characterArray contains:" ); 48 printArray( characterArray ); // pass a Character array 49 } // end main 50 } // end class OverloadedMethods
|
The program begins by declaring and initializing three arrayssix-element Integer array integerArray (line 39), seven-element Double array doubleArray (line 40) and five-element Character array characterArray (line 41). Then, lines 4348 output the arrays.
When the compiler encounters a method call, it always attempts to locate a method declaration that has the same method name and parameters that match the argument types in the method call. In this example, each printArray call exactly matches one of the printArray method declarations. For example, line 44 calls printArray with integerArray as its argument. At compile time, the compiler determines argument integerArray's type (i.e., Integer[]) and attempts to locate a method named printArray that specifies a single Integer[] parameter (lines 714) and sets up a call to that method. Similarly, when the compiler encounters the printArray call at line 46, it determines argument doubleArray's type (i.e., Double[]), then attempts to locate a method named printArray that specifies a single Double[] parameter (lines 1724) and sets up a call to that method. Finally, when the compiler encounters the printArray call at line 48, it determines argument characterArray's type (i.e., Character[]), then attempts to locate a method named printArray that specifies a single Character[] parameter (lines 2734) and sets up a call to that method.
Study each printArray method. Note that the array element type appears in two locations in each methodthe method header (lines 7, 17 and 27) and the for statement header (lines 10, 20 and 30). If we replace the element types in each method with a generic nameby convention we'll use E to represent the "element" typethen all three methods would look like the one in Fig. 18.2. It appears that if we can replace the array element type in each of the three methods with a single generic type, then we should be able to declare one printArray method that can display the string representations of the elements of any array that contains objects. Note that the format specifier %s can be used to output any object's string representationthe object's toString method will be called implicitly. The method in Fig. 18.2 is similar to the generic printArray method declaration we discuss in Section 18.3.
Figure 18.2. printArray method in which actual type names are replaced by convention with the generic name E.
(This item is displayed on page 873 in the print version)
1 public static void printArray( E[] inputArray ) 2 { 3 // display array elements 4 for ( E element : inputArray ) 5 System.out.printf( "%s ", element ); 6 7 System.out.println(); 8 } // end method printArray |