Bounds—Requiring a Type Parameter to Implement an Interface or Extend a Parent Class
You'll get more use out of generic types if there's a way to call additional methods on them, more than just the handful of methods implemented in java.lang.Object. You can call a specific method, such as compareTo()
, on a generic type within the body of a generic class, if you can be sure that the type has such a method. There are two ways to ensure that some class has a given method:
-
Make the class a child of some parent that has the method, or
-
Make the class implement an interface that has the method.
In general terms, we want a way to tell a generic that one of the parameter types must implement some interface or extend some parent. We express this ...
Get Just Java™ 2 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.