I am trying to implement the Java 1.6 Queue interface, but I am getting the error:
MyBoundedQueue.java:27: MyBoundedQueue is not abstract and does not override abstract method offer(java.lang.Object) in java.util.Queue
What I really don’t understand is that there is no offer(Object) method in the Queue class. The Java 1.6 API for Queue says there is a method boolean offer(E e), where E is a parameterized type, and indeed, I have implemented that, as shown below.
Any help?
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.Queue;
public class MyBoundedQueue<ItemType> implements Queue
{
private int _maxSize;
private ArrayDeque<ItemType> _window;
public MyBoundedQueue(int maxSize)
{
_maxSize = maxSize;
_window = new ArrayDeque<ItemType>(_maxSize);
}
public boolean add(ItemType item)
{
if (_window.size() >= _maxSize)
{
_window.removeFirst();
}
_window.addLast(item);
}
public ItemType element()
{
return _window.element();
}
public boolean offer(ItemType item)
{
add(item);
return true;
}
public ItemType peek()
{
return _window.peek();
}
public ItemType poll()
{
return _window.poll();
}
public ItemType remove()
{
return _window.remove();
}
public void clear()
{
_window.clear();
}
public int size()
{
return _window.size();
}
public Iterator<ItemType> iterator()
{
return _window.iterator();
}
}
You need to change it to:
It’s telling you
offer(Object)because without the Generic typing that’s what it would be. You also don’t need to specify a generic type for your class … you’re not using generic types anywhere.If you wanted your class to use generics you’d want to do:
And everywhere you currently have
ItemTypeyou’d useTinstead.