Tomáš Zato
Tomáš Zato

Reputation: 53129

How to force generic argument superclass type when implementing Map?

I have all kinds of GameObjects. I want to make basic collection definition for them all:

//Wrong number of type arguments; required 2. > expected
public interface GameObjectMap<T> extends Map<String, T extends GameObject> {

}

The collection will allways be mapped by string (because the data are loaded from JSON). But the second generic type argument should be any instance of GameObject. I have no idea how to write the code above correctly.

Upvotes: 0

Views: 1012

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201439

The restriction comes on the first declaration of T, like

public interface GameObjectMap<T extends GameObject> extends Map<String, T> {

}

Upvotes: 2

Vincent
Vincent

Reputation: 484

You're almost right. Simply move the extends GameObject to the first generic definition:

public interface GameObjectMap<T extends GameObject> extends Map<String, T> {

}

Upvotes: 4

Related Questions