Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic argument 'extends' multiple classes

Tags:

java

generics

I have a question about generic arguments, this is the code I have:

public <T extends Renderable, Box> void setBackBox(T bb) {
    ...
}

As you see you can give as parameter bb an object that extends Box & Renderable. But eclipse gives the following waring: 'The type parameter Box is hiding the type Box'.

How can I fix this/work around it?

like image 982
HoldYourWaffle Avatar asked Dec 14 '22 13:12

HoldYourWaffle


1 Answers

Here, you're defining two type-parameters:

  • T extends Renderable
  • Box

Box is the alias of the second method-scoped type-parameter and if you have another one with the same name (class-scoped), the method-scoped one will hide it. That's why Eclipse throws a warning.

If you want T to extend both Renderable and Box, you have to do:

public <T extends Renderable & Box> void setBackBox(T bb)

Also note that when your type-parameter(s) extend multiple types, you're allowed to use one class, which has to be first in the list. For example, if Box is a class, the correct definition would be:

public <T extends Box & Renderable> void setBackBox(T bb)
like image 159
Konstantin Yovkov Avatar answered Dec 21 '22 09:12

Konstantin Yovkov