Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic extending class AND implements interface [duplicate]

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)
like image 731
Sebastian Hoffmann Avatar asked Jul 26 '12 15:07

Sebastian Hoffmann


1 Answers

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) {
like image 96
assylias Avatar answered Sep 28 '22 02:09

assylias