Implementing Method sum With a Wildcard Type Argument in Its Parameter
Recall that the purpose of method sum
in Fig. 20.13 was to total any type of Number
s stored in an ArrayList
. We created an ArrayList
of Number
s that contained both Integer
and Double
objects. The output of Fig. 20.13 demonstrates that method sum
worked properly. Given that method sum
can total the elements of an ArrayList
of Number
s, you might expect that the method would also work for ArrayList
s that contain elements of only one numeric type, such as ArrayList<Integer>
. So we modified class TotalNumbers
to create an ArrayList
of Integer
s and pass it to method sum
. When we compile the program, the compiler issues the following error message:
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.