Reputation: 3098
I know many ask such problems, but I really do not get this one.
(If you need more code, please tell)
public class QuestionManager<Question extends Component & IQuestion<? extends IAnswerStorage>>
public class AccountingQuestionManager<Question extends Component & IQuestion<? extends IAnswerStorage>> extends QuestionManager<Question>
public interface IQuestion<DataStorage extends IAnswerStorage>
IAnswerStorage
is an empty interface
These are my 3 generic classes I need for explaining. In a QuestionManager
the questions need to be questions (IQuestions
) [with storage specified]
I now tried the following in the hope that it allows any questions, which are components as well.
QuestionManager<? extends Component & IQuestion<? extends IAnswerStorage>> manager = new AccountingQuestionManager<>(
"Test test", this);
I also have a function to add questions to a pool of questions:
manager.addQuestion(question);
But I get the 2 following errors:
Incorrect number of arguments for type QuestionManager<Question>; it cannot be parameterized with arguments <? extends Component, IQuestion<? extends IAnswerStorage>>
Syntax error on token "&", , expected
Thanks so much for your help. Let me know if you need more.
Upvotes: 0
Views: 159
Reputation: 3098
So that is the way I was able to solve my specific problem (which I would not be able to, if I would be less generic) QuestionManager:
public class QuestionManager<Question extends IQuestion<? extends AnswerStorage>>
extends ExtBorderLayout
And the "addQuestion" method looks like the following
public <QuestionComponent extends IQuestion<? extends AnswerStorage> & Component> void addQuestion(
QuestionComponent question)
So, making the method generic did the job for me.
Thanks for your help!
Upvotes: 0
Reputation: 2907
You may use the generic &
in a class declaration, but not in variable assignment. You can safely do
QuestionManager<?> manager = //...
because it is guaranteed in the specification of the type parameter Question
that it will already fit ? extends Component & IQuestion<? extends IAnswerStorage>
.
Upvotes: 1
Reputation: 1186
You cannot use '&' in the generic part when declaring a variable. It only works when declaring a generic class. When declaring a variable/field that uses a generic class, always provide concrete classes/interfaces for the generic parameters.
e.g., QuestionManager<Question>
Upvotes: -1
Reputation: 280168
In this expression
public class QuestionManager<Question extends Component & IQuestion<? extends IAnswerStorage>>
you are declaring a type parameter named Question
which is a subtype of Component
and of IQuestion<? extends IAnswerStorage>
. That is, the type parameter Question
has multiple bounds.
In this expression
QuestionManager<? extends Component & IQuestion<? extends IAnswerStorage>> manager = new AccountingQuestionManager<>("Test test", this);
what you are attempting to do is declare a type argument for the QuestionManager
type. A type argument cannot be declared to have multiple bounds. A type argument, by definition, already is a type with whatever bounds it has. You can't redefine it in the expression.
Here are the syntax rules for type arguments.
Upvotes: 1