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 Organizing Data Interfaces

Chris Rubio
Chris Rubio
1,643 Points

This is entire lesson was soooo confusing, i am completly lost. I dont even know what to ask

I dont know what to do , this entire lesson was sooo confusing.

5 Answers

andren
andren
28,558 Points

Interfaces are often considered quite confusing and hard to understand for beginners so you are certainly not alone in being confused. But interfaces are actually not as complex as they might seem at first. They are essentially just a set of rules that you promise that your class adheres to.

Let's say you where writing a method that required certain methods to exist on the parameter you where passed and those methods had to work in a specific way, how could you guarantee that those methods where present?

The sort method is a good example of such a method, in order to sort objects it depends on the class having a compareTo method, because without that it would have no way to actually compare the objects, which would make it impossible for it to sort them. And not only does the method need to exist, it needs to follow certain rules about how its return value is calculated. If it didn't then the sort method would not know what the returned value of compareTo actually meant.

The simplest way to guarantee those two things would be to just make the method work with one specific class, which you knew had those methods present and knew was coded a specific way. But having to make methods like sort class specific means that every single class would need a custom sort method, having one sort method that can work with every class is a lot more efficient.

And that's where interfaces comes in. Interfaces allow you to essentially create a contract that specifies exactly what methods and properties a class needs to have. When a class implements an interface it essentially just agrees to adhere to that contract by including the methods and properties that the interface requires.

The Comparable interface shown in this video actually only requires two things, which is that classes that implement it contain a method called "compareTo" and that the method returns a negative number if the passed in object is less than the object, 0 if it is equal and a positive number if it is greater.

By requiring that classes passed to it implements that interface the sort method can work with any class, as it can safely assume that any class passed to it will have the compareTo method and can safely assume that it works a specific way.

So to put it as simply as possible, an interface is just a template that describes certain methods and properties, and to implement that interface a class promises that those methods and properties exist in the class and follows the rules that the interface stipulated.

Brendan Hughes
Brendan Hughes
5,226 Points

Can you help me understand why interfaces are necessary?

My understanding from previous lessons was that you could just call compareTo on valid variables. What's the purpose of implementing an interface?

Chris Rubio
Chris Rubio
1,643 Points

Thank you for your response, i will continue to push forward !

Byron Farrow
Byron Farrow
13,267 Points

Thanks andren - that was really useful!

Leroy Hutchinson
Leroy Hutchinson
212 Points

really helpful andren thanks, im still a little iffy though but I think i understand it more. Are interfaces necessary because they promise to set the playground that certain methods need to work correctly?

andren Thanks for the comment. It makes more sense now.