June 2025
Intermediate to advanced
1129 pages
53h
English
For generic specifications, the types can be further restricted. This limitation is useful because an arbitrary type is often too general. Our declaration of random(...) didn’t impose any restrictions on the types:
public static <T> T random( T m, T n ) { return Math.random() > 0.5 ? m : n;}
Thus, the following is possible:
Object o1 = new Object();Object o2 = new Point();System.out.println( random( o1, o2 ) );
Since the type is arbitrary, objects can be passed that may make little sense, especially when they’re combined.
When you declare a generic type, you can specify that the later parameterized type extends a specific class or implements a concrete interface. ...
Read now
Unlock full access