0

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.

Solsma Dev
  • 481
  • 6
  • 22
  • The message is telling you that you haven't implemented all the methods defined in the interface. – Oliver Charlesworth Oct 20 '13 at 16:20
  • `E` is the type of element in the queue, an interface is like a contract it defines methods required in the implementation, so you **have to** implements `size`, `isEmpty`, ... from the `Queue` interface –  Oct 20 '13 at 16:29
  • Ok, so I just have to define the methods in the Queue interface to eliminate the error? – Solsma Dev Oct 20 '13 at 16:30

1 Answers1

1

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.

sdanzig
  • 4,510
  • 1
  • 23
  • 27
  • OK, so I've tried to implement the method dequeue() within the ArrayQueue class as this: `@Override public E dequeue(){ if(isEmpty() == true){throw EmptyQueueException;} E temp = Q[f]; Q[f] = null; f = (f+1)%Q.length; return temp;}` Once I implement all the methods listed in the Queue interface I shouldn't see this error any more? – Solsma Dev Oct 20 '13 at 21:39
  • 1
    I must apologize... Stackoverflow hides the template parameters by default, unless you specify it in a code block..so I've updated my answer to show them. I hope you weren't thoroughly confused by that. You are correct when you say as long as you implement methods with the signatures defined by the Queue interface, you'll get rid of the error, however. One last thing... that "isEmpty()" method you're using to check... you should try to internally avoid using methods that are exposed in your public API. Read Josh Bloch's "Effective Java" for details on that. – sdanzig Oct 20 '13 at 21:46
  • Ok, thank you. These are concepts that are very foreign to a Python programmer. As for the isEmpty() method, it's used that way in the book I'm referencing. Sometime when I have more time, -a lot more time, I will check out "Effective Java", I've heard that referenced often. Thanks again. – Solsma Dev Oct 20 '13 at 22:12