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 trialDaniel Peng
4,072 PointsConfusion about interfaces and implementation
During the video, Craig really tries to drive the point "interface on the left, implementation on the right."
He does this when writing the code: List<String> results = new ArrayList<String>();
From what I can understand, the list of strings is an interface, and the arraylist of strings is an implementation. However, I'm not sure what this means. Is it an implementation of the interface? Any help would be greatly appreciated!
1 Answer
Benjamin Barslev Nielsen
18,958 PointsYes, ArrayList<E> is an implementation of List<E>, so therefore we know that we can use all the methods declared in List<E> on the ArrayList object.
Hope this helps
Daniel Peng
4,072 PointsDaniel Peng
4,072 PointsThanks. So in a way, is the interface List<E> similar to a class?
Benjamin Barslev Nielsen
18,958 PointsBenjamin Barslev Nielsen
18,958 PointsThe big difference between interfaces and classes is that the interfaces define which methods an implementation should have, they do not provide an implementation themselves, like classes do. In this sense List<E> isn't similar to a class since List<E> does not implement the methods, it only defines them. The methods are implemented in the classes that implements List<E>, as for instance ArrayList<E> does.
Hope this clarifies the relation between interfaces and classes.
Daniel Peng
4,072 PointsDaniel Peng
4,072 PointsAlright, I think that clears things up a bit. Thanks for the help!