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 Comparable

A question about implementing and overriding interfaces

I've managed to get through this task, but I still don't quite understand what is going on. I feel that I need to understand it in its entirety to be able to apply this knowledge outside of this task.

From my understanding, the reason that I need to @Override is because this function already exists in the Comparable interface, which is part of one of the many Java libraries. The string data type implements this interface. With compareTo already being a function within the comparable library, we need to override it in order to make it better suit our needs in this class. (First question: Would compareTo, if used in other class, effectively be 'reset'? i.e. this variation of it that we are creating will only be available to this class?)

This line is required

          if (equals(passedBP)) {
return 0;
}

In my overridden 'method?' which in its entirety is

          @Override
         public int compareTo(Object obj){
            BlogPost passedBP = (BlogPost) obj;
                   if (equals(passedBP)) {
                     return 0;
                     }
               return 1;
          }

But my question is, what is equals(passedBP) comparing too?

Thanks in advance

1 Answer

Teja Tummalapalli
Teja Tummalapalli
5,592 Points

Question 1:

When a class implement an Interface , it need to implement all the methods of the Interfaces. In this case BlogPost is implementing Comparable. This interface has just one method compareTo(T o). So the class BlogPost is required to implement the method.

Note that the method compareTo(..) is used to compare this object with the object passed.

So , while overriding the method compareTo(..) you can add optional @Overriding annotation. Having this annotation avoid the possible compiler errors.

Yes, this compareTo(..) method is unique implementation to this class alone. Suppose you have a new class NewBlogPost and it implements Comparable to , then you are required to write another implementation of the method comapreTo(..)

Question 2: Method compareTo(..) is used to compare this object with the object passed. Its confusing , but what happening exactly is the object having this compareTo(...) method is being compared to the object passed though the method.

Thank you for explaining this to me :)