Java Generics: Multiple Bounds

When you instantiate like this:

GenericTest gt = new GenericTest() 

you use the raw version of GenericTest type. This means that T type will be replaced with it's first bound (in your case, Date). That's why the method contract of GenericTest#test() has a Date parameter, but not a List one.

Note that every bound, except the first one, must be an interface. Only the first bound can be a class. The reason for this is that it's not possible to have types, which inherit from more that one super-class.

So, since only the first parameter is a class, you won't be able to switch the type-parameters and the following class definition will be invalid:

class GenericTest<T extends List & Date> { }

Tags:

Java

Generics