20

Is it possible to make something like this? I know that implements cannot be in the <>, but I want to restrict the T to be Serializable somehow.

public class Clazz<T implements Serializable> {
    ...
}
user219882
  • 15,274
  • 23
  • 93
  • 138
  • 1
    I tried `extends` but I had an error so I thought it was wrong. Then I found there was another issue and `extends` really works. – user219882 Aug 16 '11 at 11:56
  • possible duplicate of [Java generics - why is “extends T” allowed but not “implements T”?](https://stackoverflow.com/questions/976441/java-generics-why-is-extends-t-allowed-but-not-implements-t) – Roeland Van Heddegem Oct 13 '17 at 13:33

3 Answers3

30
public class Clazz<T extends Serializable> {
    ...
}
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
6

Just use extends instead of implements.

Ingo
  • 36,037
  • 5
  • 53
  • 100
4

Yes, just use extends instead of implements.

Perception
  • 79,279
  • 19
  • 185
  • 195