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

Calling compareTo() within the new @Override compareTo()?

I can see that calling compareTo() within the new method of @Override compareTo() works, but don't really understand the scope on this. Do we have access to both compareTo() methods afterwards. Is there a case where this matters? Any thoughts?

2 Answers

I think the @Override is overwriting the compareTo method for the class it is overriding in so you can still call the compareTo method for Strings as usual. We're simply telling java that if we want to compare objects of this class we compare it our way (via the mDescription comparison which is a String comparison and the Date comparison which compares its long values). So we're basically setting up a custom comparison for our object according to our understanding.

Cool, guess that sort of makes a bit more sense! Cheeers!

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

My understanding is all @Override does is inform the compiler that you're changing the default behaviour of the compareTo() method. You can access it because you inherit the method from the interface. So unless I'm mistaken you don't have to use or access it more than the once.