Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Exploring the Java Collection Framework Using ArrayLists

Interface on the left, implementation on the right.

with the code: List<String> results = mew ArrayList<String>();

I understand that the interface is list of strings. And the implementation is ArrayList of strings, but what does that mean?

Craig says, Interface on the left, implementation on the right.

But what would happen if it was switched around?

What's the difference between a List of Strings and ArrayList? I thought they were the same? :/

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Tony!

By using the implementation, we are in fact saying and writing all code following it that, we don't really care what implementation is used, as long as it meets the following contract. The power behind this is subtle, and a little hard to grasp at first, until you see it in action. I do end up touching on it a bit in the example project, you'll see me change one line of code and fix the application, just by changing implementations.

Basically the power behind passing around implementations, in all code, but especially in the Java Collections Framework, is that you can at any point change the underlying algorithm with one fell swoop. It keeps your code portable and nimble, and it was how the framework has provided the abstraction of lower level code.

That clear things up?

Most definitely. Thx!

Look them up on the java documentation at oracle (a really invaluable resource, get used to using it: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html) and you'll see that ArrayList is a class object and List is a collections interface. What we're doing is taking the object that is limited in what it can do and placing inside a wrapper, like putting reeses into a wrapper at the factory. Before we use the interface, the array can only be one size and is limited in what it can do in terms of organization. Before we wrap the reeses we can't ship them overseas. When we wrap the ArrayList in the interface, the interface does the resizing and organizing work the ArrayList can't. But it's not made to work without an ArrayList, , just like you wouldn't want a reeses wrapper without a reeses inside. It's not made to hold peanut butter, that''s what the reeses is for. Again, if you have a question as to whether something is an object or a collections interface, check the oracle documentation. They're designed to do very different things.

Peter Taylor
Peter Taylor
Courses Plus Student 4,275 Points

Nicolas, Great explanation!!!!!!

Thanks. I still don't understand the 'subtleties' Craig was trying to explain but your version gets me a little more settled.