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
 
Array integerArray contains:
1 2 3 4 5 6

Array doubleArray contains:
1.1 2.2 3.3 4.4 5.5 6.6 7.7

Array characterArray contains:
H E L L O
 

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

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net