Bounded wildcard as a method parameter

265 Views Asked by At

I would like to create a method which returns type is a map object and parameter should be a class which extends A and implements I. So my code is as follows:

public Map<String,String> getIdea(Class < ? extends A & I) { .....}

But i am getting a compilation error saying that my syntax is wrong. It is expecting a comma right after A. It does not work even with comma. Do you have any idea?

2

There are 2 best solutions below

1
Makoto On

To put what Sotirios Delimanolis said in comments into code:

public <T extends A & I> Map<String, String> getIdea(Class<? extends T> clazz) { }

To be honest, I don't think that the wildcard gets you anything here since T is tightly bound, so you may be better off with the non-wildcard version.

0
Amit Verma On

For parameter to be a class which extends A and implements I, type parameter should be defined at method level as below:

public <T extends A & I> Map<String,String> getIdea(T t) { .....}