Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0114 Generic Bounded Wildcards

Wildcard arguments can be bounded in the same way that a type parameter can be bounded. A bounded wildcard is important when creating a generic type that will operate on a class hierarchy. A bounded wildcard specifies either an upper bound or a lower bound for the type argument. class TwoD { int x, y; TwoD(int a, int b) { x = a; y = b; } } // Three-dimensional coordinates. class ThreeD extends TwoD { int z; ThreeD(int a, int b, int c) { super(a, b); z = c; } } class Map<T extends TwoD> { T[] coords; Map(T[] o) { coords = o; } } public class Main { static void showXY(Map<?> c) { for (int i = 0; i < c.coords.length; i++){ System.out.println(c.coords[i].x + " " + c.coords[i].y); } } static void showXYZ(Map<? extends ThreeD> c) { for (int i = 0; i < c.coords.length; i++){ System.out.println(c.coords[i].x + " " + c.coords[i].y + " " + c.coords[i].z); } } public static void main(String args[]) { TwoD td[] = { new TwoD(0, 0), new TwoD(-1, -2) }; Map<TwoD> map = new Map<TwoD>(td); System.out.println("Contents of tdlocs."); showXY(map); } } In general, to establish an upper bound for a wildcard, use the following type of wildcard expression: <? extends superclass> superclass is the name of the class that serves as the upper bound. This is an inclusive clause. You can specify a lower bound for a wildcard by adding a super clause to a wildcard declaration. <? super subclass> In this case, only classes that are superclasses of subclass are acceptable arguments. This is an exclusive clause, because it will not match the class specified by subclass