Alex
Alex

Reputation: 667

Java Queue of Interface type

I'm trying to make a queue of an interfaced type Player because I don't know what type of player will be in the queue, ie human, AI etc so I have an interface for what different players can do, ie makemove etc.

Queue<Player> players = new Queue<Player>();

However, queue cannot be instantiated because Player is an interface. How do I create a queue of an interfaced type?

Upvotes: 0

Views: 139

Answers (1)

khelwood
khelwood

Reputation: 59111

You can have a variable of type Queue<Player>, but Queue itself is just an interface. You need to instantiate a concrete implementation of Queue, such as LinkedList.

e.g.

Queue<Player> players = new LinkedList<Player>();

Upvotes: 2

Related Questions