The general argument "extends" multiple classes

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

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

      

As you can see, you can specify as parameter bb an object that extends Box and Renderable. But eclipse gives the following information: "The type of the Box parameter hides the type of Box".

How can I fix this / work around it?

+3


source to share


2 answers


Here you define two type parameters:

  • T extends Renderable

  • Box

Box

is an alias for the second parameter type parameter, and if you have another with the same name (with class scope) the scoped method will hide it. This is why Eclipse issues a warning.

If you want to T

extend both Renderable

as well Box

, you need to do:



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

      

Also note that when your type parameter extends multiple types, you can use one class , which must 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)

      

+4


source


Here you have defined Box as a generic parameter that hides the Box

class / interface:

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

      

If Box is an interface that should be a T border:

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

      



If Box is a class that should be the border of T:

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

      

If both Box and Renderable are classes, they cannot be as constraints of type T. Only the first class can be a class.

+5


source







All Articles