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 Efficiency! Queueing

edumorlom
edumorlom
4,073 Points

Why are Lists an interface?

I thought interfaces were just a contract that you had to implement their methods. Like if I implement Animal interface, I am agreeing to write their methods.

For example:

public class Mammal implements Animal {}

This implements the Animal interface.

List<String> listOfAnimals;

Why are lists referred to as interfaces?

Thanks!

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! As I understand it an Interface gives an object a way to interact with the "world" outside through a series of methods. Here's some documentation from Oracle about interfaces and here's some documentation specifically on the List interface. I feel like the big distinction here is that an interface is a type of object that only implements methods and no properties (at least not publicly exposed).

But someone please tell me if I've got this incorrect!

Hope this helps! :sparkles:

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

List is an interface that provides methods like add contains and delete. There are several concrete implementations that implement the List interface such as ArrayList, LinkedList etc.

Here is some more on the List interface.

What you are talking about here is a parameterized type or generic. List<Animal> can be read as a list of animals. And only Animals can be added, removed, etc.

List<Animal> animals = new ArrayList<Animal>();

See how we still choose the concrete implementation?

That help?

edumorlom
edumorlom
4,073 Points

I dont understand why Lists are considered interfaces. Arent they Classes? They have methods. It confuses me.

Classes - you can make object of class, you can inherit class, but only one. If you inherit class you can choose some of its methods you need (not all if you don't need). Interface - it is full abstract class - it means all its methods are abstract (have only signature). Only talk what to do but don't how to do. You are obligated to provide your own implementation for all methods from interface. But you cannot inherit interfaces - you can only implement them. You cannot create object from interface. But you can implement many interfaces.

I know that some thing called "composition" let to omit issue concerning one class inheritance.

P.S. Thanks to many developers we don't have to make our own implementation (but we can if you are so smart) of Collection interfaces. We can use already prepared good stuff like ArrayList<>() or map etc. There are many of them because there isn't one simple way to resolve many issues in specified short among of time. We have to consider their + and - and wisely choose one of them to achieve deliberate effect.