Why I can instantiate a stack like this:
Stack<Integer> stack = new Stack<>();
But can't instantiate a queue like this:
Queue<Integer> queue = new Queue<>();
Is that because queue is a interface while stack is a object
?
If so, why we say that everything in java is an object?
A Queue is an interface, meaning we cannot construct a Queue directly.
We can create object using implementing classes, implements the Queue interface, like one of the following: AbstractQueue
, ArrayBlockingQueue
, ConcurrentLinkedQueue
, DelayQueue
, LinkedBlockingQueue
, LinkedList
, PriorityBlockingQueue
, PriorityQueue
, or SynchronousQueue
.
Stack is the class and can be instantiated directly.
This is by design and with Queue
, lot more options are there using implementing classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With