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

Walker Bolger
Walker Bolger
16,798 Points

Why wasn't "List" implemented when we declared the class Treet?

When we declared the class Treet, we implemented the interfaces "Comparable" and "Serializable" in the class declaration when we said "public class Treet implements Comparable<Treet>, Serializable". Why then do we implement the interface "List<String>" next to the methods (i.e. "public List<String> getHashTags()") and not at the class declaration next to "Comparable," and "Serializable"?

1 Answer

Victor Learned
Victor Learned
6,976 Points

You are not implementing the List interface. You are telling the function that it will be returning a list of objects. Remember how methods are declared. Also the reason we are implementing "Comparable," and "Serializable" is so we can implement (aka code how the method should behave) the methods they contain whereas here we are merely using the List as is.

//Access modifer (public, private, protected), return value (in this case we are returning a List of strings), method name
public List<String> getHastTags()
{
  List<String> results = new ArrayList<String>();
}

Here's some more details on what implementing an interface means in Java: https://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html http://stackoverflow.com/questions/10839131/implements-vs-extends-when-to-use-whats-the-difference

But the instructor specifically said "interface on the left and implementation on the right." So according to him we are implementing the List interface. If so, it should appear in the implements clause of the class declaration. How can an interface be used without including it in the implements clause of the class?