-1

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?

user1459497
  • 659
  • 2
  • 9
  • 18
  • 2
    Make the method generic, declare a type parameter with those bounds, then use that parameter as the type argument for the `Class` parameter. – Sotirios Delimanolis Jan 12 '21 at 16:52
  • answer to your question is here: [see it](https://stackoverflow.com/questions/6643241/why-cant-you-have-multiple-interfaces-in-a-bounded-wildcard-generic) – Dmitrii B Jan 12 '21 at 16:55

2 Answers2

2

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.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • I agree. Since `T extends`, and isn't used anywhere else, `? extends T` is redundant and should just be `T`, as in `getIdea(Class clazz)`. Still, +1 from me. – Andreas Jan 12 '21 at 17:15
0

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) { .....}
Amit Verma
  • 21
  • 2