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 Exploring the Java Collection Framework Using ArrayLists

Mehmet Arabaci
Mehmet Arabaci
4,432 Points

Why did we need to use the "implements" keyword in the class for Comparable but do not need it for Lists.

If both of these are interfaces should they not be implemented equally?

Armand J
Armand J
6,733 Points

I think it's because implementing is for the class and all Treet objects whereas we are just importing the Lists because we need to access them to use them for a particular method of the class. I see it as the implement keyword is to make Comparable part of the nature of an object whereas importing is just bringing in tools you might need for a particular job in a method of that object.

1 Answer

We use the keyword "implements" for the interface Comparable because our class file intends to use all the methods belonging to that interface (in the case of this interface, there is only one): compareTo(T). In order to use this method, we have to provide implementation, or code, that specifies exactly how two objects of our class can be compared.

We only need to import the interface List because we intend to refer to it by name in our class file; we don't care about implementing its methods. In other words, if you didn't include the import statement for List, you'd have to refer to it by its formal name (java.util.List) everywhere in the file. The import statement simply tells the compiler "Anytime you see 'List,' I'm talking about the one that belongs to the java.util package," that way you don't have to specify EVERY time you use List what you're talking about. Without the import statement, the line List<String> list = new ArrayList<String>(); would look like this: java.util.List<String> list = new ArrayList<String>();.

Again, the "implements" keyword means your class file provides implementation (or code) for that interface's methods. The import statement only tells the compiler to which class (and package) you are referring when it sees that name anywhere in the file.