Java Error - arrayqueue.ArrayQueue is not abstract and does not override abstract method dequeue()

StackOverflow https://stackoverflow.com/questions/19479721

Вопрос

I'm having trouble with a java error specifically, this one:

arrayqueue.ArrayQueue is not abstract and does not override abstract method dequeue() in arrayqueue.Queue at arrayqueue.ArrayQueue.(ArrayQueue.java:11)

Here's the code the error is occurring in:

public class ArrayQueue<E> implements Queue<E> {

E [] Q; 
int f,r;
int size;

static final int CAPACITY = 1000; 
int capacity; 

public ArrayQueue() {
    this(CAPACITY);
}

public ArrayQueue(int cap){
    capacity = cap;
    Q = (E []) new Object[capacity];
    f = 0;
    r = 0;
    size = 0;
}



public static void main(String[] args) {

}

}

Line 11 would be this line: public class ArrayQueue<E> implements Queue<E> { Specifically, I don't understand what the <E> in this line means. I'm trying to implement the queue ADT using a circular array. Obviously this implements the Queue interface, which I also don't understand the concept of an interface quite yet (Why can't Java be like Python!?)

For reference, I've also posted the Queue interface below:

public interface Queue<F> {


public int size();
public boolean isEmpty();
public F front() throws EmptyQueueException;
public void enqueue(F element);
public F dequeue() throws EmptyQueueException;

}

I know this is about 5 questions in a row, but conceptually, this is confusing to me. I appreciate any help.

Это было полезно?

Решение

The "E" is a placeholder for a type (String, Integer, etc). Something like E is used when you're defining a "template". Java has the ability to provide a "generic" template class, which can be used to create a class meant to handle a particular type. So if you had a template class Blah<E>, you could instantiate a class of type Blah<String>... and then, bam, you have an Blah that only handles String objects.

In this case, you have a generic interface. You're defining a generic class that uses the generic interface, which is handled the same way.

Now, an interface is basically just a "specification" that guarantees your class implements particular methods. So when you have a class Meatball that implements Woogie which specifies methods X, Y, and Z, that means the rest of the java world now knows Meatball is guaranteed to have the methods X, Y, and Z.

What you need to do, when implementing a generic interface such as ArrayQueue<E>, is continue to define the class with E instead of the type you actually might use ArrayQueue to handle. When you actually instantiate the class, if you want your ArrayQueue to store strings, you'd say

ArrayQueue<String> myQueue = new ArrayQueue<String>

... But then later you can use it again, and replace String with Integer when creating "theirQueue". Java will enforce that each ArrayQueue only contains a particular type of object.

In summary, you need to implement all the methods that Queue guarantees (which you listed), keeping it generic in your ArrayQueue<E> class, so Java won't whine about your usage of the Queue interface.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top