public Class getFetchRoot(AdminUser
user,Class ser, List filter){
return MyProduct.class;//compile time error
In the above method the return type is Class<Serializable>
but you are returning MyProduct.class;
.
This is effectively equivalent to Class<Serializable> = MyProduct.class;
.
This doesn't work. The problem is Generics does not support sub-typing .
Quoting from the https://dzone.com/articles/5-things-you-should-know-about-java-generics
For ex: we cannot have List<Number> list = new ArrayList<Integer>()
.
Reason:
The piece of code shown above will not compile because if it compiles than type safety can't be achieved. To make this more clear, lets take the following piece of code shown below where at line 4 we are assigning a list of long to a list of numbers. This piece of code does not compile because if it could have compiled we could add a double value in a List of longs. This could have resulted in ClassCastException
at runtime and type safety could not be achieved.
List<Long> list = new ArrayList<Long>();
list.add(Long.valueOf(1));
list.add(Long.valueOf(2));
List<Number> numbers = list; // this will not compile
numbers.add(Double.valueOf(3.14));
To make it work you can either the method return type as Class<MyProduct>
or Class<? extends ProductImpl>
or Class<? extends Serializable>
.
Refer to the above link for more information and limitations.