0

Queue is an interface that implements many different classes. I am confused about why my text book gives this sample text using Queue. I have also found this in an example on the Princeton website. Is this a customary way to provide code so it can be edited later to the programmers preferred type of queue?

This is code taken from an algorithm for a Binary Search Symbol Table.

public Iterable<Key> keys(Key lo, Key hi) {
    Queue<Key> q = new Queue<Key>();
    for (int i = rank(lo); i < rank(hi); i++) {
        q.enqueue(keys[i]);
    }
    if (contains(hi)) {
        q.enqueue(keys[rank(hi)]);
    }
    return q;
}
clenard
  • 188
  • 2
  • 16
  • What is your actual question? Which part is it related to? Do you mean Java Generics? Also, what do you mean that the interface "implements" many different classes, interfaces do not implement classes, it is the other way round. – SirRichie Jul 29 '14 at 13:49
  • The reason for using interfaces over concrete classes is discussed here: http://jdevelopment.nl/java-best-practices-5-code-to-interface-access-by-name-and-instance-data/ – Chris K Jul 29 '14 at 13:50
  • Right, `new Queue()` will not compile if we're dealing with `java.util.Queue`. – GriffeyDog Jul 29 '14 at 13:52
  • Looks like a typo. Variable should be declared as interface with call to concrete class constructor. – John B Jul 29 '14 at 13:56

3 Answers3

0

So first, there are many classes that implement Queue not Queue is an interface that implements many different classes.

Second, yes code convention is that you use the interface where ever possible to make code more flexible in case a different implementation is desired. This is especially true of method signatures but also good practice for variable and field declaration.

Third, looks like a typo. Line should be Queue<Key> q = new LinkedList<Key>();

See answer here: Why are variables declared with their interface name in Java?

Also, What does it mean to “program to an interface”?

Community
  • 1
  • 1
John B
  • 32,493
  • 6
  • 77
  • 98
  • I think the question is about `new Queue()`, which will not compile for `java.util.Queue`. – GriffeyDog Jul 29 '14 at 13:54
  • Good point. Looks like a typo. Variable should be declared as interface with call to concrete class constructor. – John B Jul 29 '14 at 13:57
0

I do this often for other interfaces where the specific implementation doesn't provide anything over the interface, like List.

List<String> strings = new ArrayList<>();

In that case I retain the most flexibility with the least amount of hassle.

Granted, you won't change implementations very often, but there are some specific ones where for example inserting takes O(n) time and lookup takes O(log n) time or vice versa and in those cases you have to do some research how you're using the interface.

Do you change the contents of the collection a lot, but read it sparingly? Or is it the other way around and do you insert just once and read often?

Davio
  • 4,609
  • 2
  • 31
  • 58
0

Your question is very broad. This technique is called programming to interface and it's an effect of years of gathering good practices in coding. It just makes things easier in so many ways. It revolutionised ways how we write software today. If you're an eager learner and want to know more, check this article as a starter.

Community
  • 1
  • 1
alobodzk
  • 1,284
  • 2
  • 15
  • 27