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

Matthew Francis
Matthew Francis
6,967 Points

Newbie - Clarification on the Defitnion of Interfaces/When to use it

I am unsure of interfaces, and from what I've recall from this video, an interface is basically a class that has instantiated methods which revolves around sub-classes? Is my understanding correct?

PS: I know this can be solved through research, but I do not have the time to do so tomorrow. I would prefer if I wake up and the smart guys here have alread summarized what an interface is!

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

An interface lets you define what the implementor must do but not how it should be done. What this allows for is you can have methods accept the interface as a parameter, and since you know that all objects passed into the method can perform the necessary actions, you can make any number of implementations and they should all seemingly work.

The Java Collections Framework makes heavy use of this and it allows you to swap out implementations easily, as long as you always code to the interface, and not the implementation.

Hope it helps!

Great explanation. Thanks, Craig!

Matthew Francis
Matthew Francis
6,967 Points

Thanks Craig!

One other small question.

In this video/case, is implementing the interface "Comparable" essential? Would it be fine if you do not implement the interface and instead, just define the method compareTo();?

PS:Sorry! I'm in the middle of fixing my code (syntax errors) so I can't test it out!

Craig Dennis
Craig Dennis
Treehouse Teacher

Hey Matthew,

Great question! Actually this is a great example of what I was just talking about. The sort method requires that it's items implement the Comparable interface. The Comparable interface requires that you implement the compareTo method. So because sort knows that the method will exist on the object it receives, it can just call it.

Make sense?

Matthew Francis
Matthew Francis
6,967 Points

Thank's for the concise clarification Craig!

A Java Interface is essentially a collection of abstract methods that an inheriting class should implement. An interface can only contain method signatures and fields, but not any method implementation.

Access the following link of the official Java doc on Interface concept for more and an example:

https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

Ciaran McNally
Ciaran McNally
7,052 Points

Thanks for the link, this helped a lot.