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 trialLucas Santos
19,315 PointsHow does interfaces even play a part in this compareTo() video?
I am not understanding how interfaces is even shown in this video?? I saw him implement it on the class with "implements Comparable" but he never made a Comparable Interface?? Only thing I saw was him overriding a method named compareTo and compare objects by date. I did not understand the point of Interfaces in this video.
To define an interface wouldn't you need to do something like:
interface Bicycle {
//some interface stuff goes here
}
then on the class you want to implement that on you would do:
class Skateboard implements Bicycle{
//some stuff
}
Lucas Santos
19,315 PointsI see ok now that makes sense, thanks.
Grigorij Schleifer
10,365 PointsYou are very welcome !!!!
Grigorij
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsYou may be confusing implements with inheritance. In inheritance, the key word is extends example. On StackOverflow implements vs extends.
public class Bicycle {
// ...
}
public class MountainBike extends Bicycle {
// ...
}
The interface described by using implements Comparable
is establishing that the methods of Comparable are contained in the class. Comparable
contains a compareTo()
method. This interface could have been called "CompareTo", but "Comparable" was chosen instead.
Lucas Santos
19,315 PointsI am for sure not confusing implements with inheritance. I looked up what interfaces is at Oracle and looked nothing like what was on the video.
I am not understanding what you mean, he never made an interface named Comparable he just implemented it as if it was already created or something.
Check that Oracle Link and you will see what I mean. It says to create an interface you need to do:
interface Bicycle {
// wheel revolutions per minute
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
Which I did not see in the video.
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHi Lucas,
we dont need to create an interface. You are right, the are interfaces pre-created. Inside that pre-created Comparable interface, there is a compareTo method we nee to sort the Treets.