I'm having problems trying to pass a parameter to the constructor of a parent generic class due to the parameter being static and therefore I must explicitly specify the type at compile-time.
Here are the two classes in question:
public class CardStack<T extends Card> extends ArrayList<T>
public class Deck<T extends Card> extends CardStack<T>
The reason I need "T extends Card" and not just Card is because I am sharing these classes between a client (which needs to render them) and a server (which only cares about values).
I am statically creating a single deck of cards, which will be shuffled after being put into each deck.
private static final CardStack<Card> NEW_DECK;
static {
NEW_DECK = new CardStack<>(DECK_SIZE);
for (int i = 0; i < DECK_SIZE; i++) {
NEW_DECK.push(new Card(Card.Suit.values()[i / Card.CARD_VALUE_MAX],
i % Card.CARD_VALUE_MAX + 1));
}
}
Constructor for Deck:
public Deck() {
super(NEW_DECK); // Error Here
Collections.shuffle(this);
}
Constructor for CardStack:
protected CardStack(final CardStack<? extends T> cardStack) {
super(cardStack);
}
I am having trouble figuring out what to put for "? extends T". I've tried a combination of things, but nothing seems to work, so it seems I don't fully understand what's going on. Is this design possible? if not what would be a better solution. Thanks!
=========================================================================
Edit: Better explain reasoning for using generics.
First of all, I want to share these classes between the client and server which I have stated above. The real problem is when I trying to extend these classes to render them. All of the Client classes contain info and methods for rendering (x/y coord, methods to draw and detect clicks).
This works fine for my class ClientCard extends Card
but the problem arises when I try to make ClientCardStack extends CardStack
since CardStack contains Cards, I am unable to render them since they do not contain the right information, and what I really need is ClientCardStack extends CardStack<ClientCard>
which causes all these problems.