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 trialAditya Puri
1,080 PointsQuestion about Comparable interface
So what I can understand is that the Comparable
interface contains a method compareTo
that compares 2 objects. Many classes have implemented this interface including String and Date. For String, it compares them according to the letters that they contain..for Date it compares them according to which date comes first. Am I right?
So then when we write int dateCmp = this.mCreationDate.compareTo(other.mCreationDate)
then is the compareTo
method of the class date
is being called here?
Also why do we need to implement the Comparable
interface to use the compareTo
object? We are making the compareTo
method from scratch anyway, then why do we need the interface? Can't we make the compareTo
method without implementing the interface?
1 Answer
Simon Coates
28,694 PointsInterfaces require you provide implementations, but the advantage is you can use the object in new ways. interfaces define types in addition to those of normal inheritance hierarchy. If implementing a interface, the object can be plugged into places (either methods or language features) that expect/rely on a Comparable type. According to the docs for Comparable: "Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator."
Aditya Puri
1,080 Pointsi don't understand what you said......
Simon Coates
28,694 PointsObjects have an inheritance hierarchy with single inheritance. A String object is thus, also an Object, and can be used where anywhere an Object or String is expected. Classes that implement interfaces get additional types - A string for example is of Object type, of String type, and by virture of interfaces of Comparable, Serilializable and CharSequence types. As such, a String can be used (predictably) in places that expect any of these types (as defined by inheritance or interface implementation). Being Comparable specifically means that an object can be used for sorted collections. (treemap for instance).
Aditya Puri
1,080 Pointsoh ok thanks!!
Simon Coates
28,694 PointsSimon Coates
28,694 PointsI assume you're right about how dates and strings are compared. The online documentation for a standard class should provide an explanation. Yes, that is using the compareTo method of the date class.