-1

It has been a few years since programming in Java, so I decided to consult an old, reliable source. In "Introduction to Java Programming" by Liang, pg. 798, an ArrayList is scheduled like so:

Collection<String> collection = new ArrayList<String>();

The author then goes about using (only) the add() method and creates an uses an Iterator of collection.

Now, I like using a generic class declaration whenever possible (coming from, now, a C and C++ background) because if I change my mind about the type I want to use for some solution, I can change just one line of code. Ideally, I would be able to declare a Collection<some_type> my_collection (whether a member or local variable), and later define it to be any type which extends the Collection class.

But, when I try to do the following,

Collection<String> my_collection;

and then later,

my_collection = ArrayList<String>();

I get compilation errors: namely, the compiler cannot find the get() and remove() symbols (or at least not the versions of the overloaded methods that I believe I am calling). For example,

[...].java:48: error: incompatible types: boolean cannot be converted to Process
        otherAlg.addJob(wait_queue.remove(0));
                                         ^

NOTE: the type Process is "fully-vetted" and is not the issue here.

Also,

[...].java:72: error: cannot find symbol
        return wait_queue.get(0);
                         ^

Sure: I can "fix" the issue by declaring whatever type I am going to initialize in the first place, but what is the deal? Why the inconsistency between what Liang states and what I would assume to be possible, and what is actually practical (at least as far as my compiler is concerned)?

JDQ
  • 443
  • 7
  • 11
  • 3
    What is the return type of `remove(int)`? What does this have to do with the original `Collection`? What is `wait_queue`? Your question is all over the place at the moment. Please clarify and provide a [MCVE]. – Sotirios Delimanolis Jun 09 '16 at 19:36
  • The compile-time type of my_collection is [Collection](http://docs.oracle.com/javase/8/docs/api/java/util/Collection.html), which has neither a get method nor a remove(int) method. The fact that you initialized it to an ArrayList is irrelevant; the type of the variable is remembered only as a Collection, as far as the compiler is concerned. When in doubt, read the official documentation; in Java, the docs are not just for assistance, they are law. – VGR Jun 09 '16 at 19:46
  • `my_collection = ArrayList;` should be `my_collection = new ArrayList();`. – shmosel Jun 09 '16 at 19:48
  • @shmosel Right: fixed that. – JDQ Jun 09 '16 at 19:54
  • @WatermarkBranding no you didn't. – shmosel Jun 09 '16 at 20:03

1 Answers1

1

As far as the rest of your program is aware your wait_queue is just a Collection, it doesn't have get or remove methods.

You're correct in that you don't want to couple your implementation to the variable's type, however you shouldn't use Collection unless you only want to iterate over the contents. Use List, Set, or other collections interfaces to describe collections. These expose the get and remove (in the case of the List) methods you want, and expose the behaviour of the object (but not the implementation - in this case ArrayList). e.g., a List allows duplicates whereas a Set does not.