13

This might sound like an odd question but how can on define a generic which has to extend a class AND to implement an interface? Im currently having a generic function with the following prototype:

public static <E extends Enum<E>> List<E> buildEnumList(Class<E> enumClass)

This works just as intented. My problem now is that I want to further restrict the passable classes to those which are enums and implement a specific interface Readable (not that in java.lang). Since a generic uses the same keyword extends to indicate that it should implement an interface, I dont see any way to get the following pseudo behaviour:

public static <E extends Enum<E> implements Readable> List<E> buildLexicographicalEnumList(Class<E> enumClass)
Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77

1 Answers1

26

You can use & to indicate that E must also implement an interface:

public static <E extends Enum<E> & Readable> List<E> buildLexicographicalEnumList(Class<E> enumClass) {
assylias
  • 321,522
  • 82
  • 660
  • 783