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

Solomon Asiedu-Ofei Jnr
Solomon Asiedu-Ofei Jnr
1,015 Points

Cannot get past this challenge

Im confused about the compare to... I keep getting this error

./com/example/BlogPost.java:20: error: method does not override or implement a method from a supertype @Override ^ 1 error

com/example/BlogPost.java
package com.example;

import java.util.Date;

public class BlogPost {
  private String mAuthor;
  private String mTitle;
  private String mBody;
  private String mCategory;
  private Date mCreationDate;

  public BlogPost(String author, String title, String body, String category, Date creationDate) {
    mAuthor = author;
    mTitle = title;
    mBody = body;
    mCategory = category;
    mCreationDate = creationDate;
  }

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

3 Answers

OK - sorry to have confused you.

Here, you are comparing obj to post.

if(obj.equals(post))

In the video, the this keyword was omitted because it can be implied. You used the following code:

public int compareTo(Object obj) {
  Treet other = (Treet) obj;
  if (equals(other)){
    // do stuff
  }
}

What are you comparing to other here? What does equals(other) mean? Don't we need to compare two things to test if they are equal? Yes, and we are. The thing you are comparing is the instance upon which you are calling the compareTo() method. That is this instance. The Treet code could also look like:

public int compareTo(Object obj) {
  Treet other = (Treet) obj;
  if (this.equals(other)){
    // do stuff
  }
}

So, in your challenge, you want to create a BlogPost instance inside the method and assign the obj cast to BlogPost to it. You've done that. Then you want to compare equality between that new object and the current instance you are using the method from. You can either use this or just leave it silent like in the video:

  public int compareTo(Object obj){
    BlogPost bp = (BlogPost) obj;
    if(equals(bp)){ // implied 'this'
      return 0;
    }
    return 1;
  }

You could use:

if(this.equals(bp))

// or

if(bp.equals(this))

I hope that makes sense.

Steve.

Solomon Asiedu-Ofei Jnr
Solomon Asiedu-Ofei Jnr
1,015 Points

Hey Steve, I clearly understand now. I took away "this" and mu code look like this

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

but I still get the error

./com/example/BlogPost.java:21: error: method does not override or implement a method from a supertype @Override ^ 1 error

Let me check - give me a minute ...

Looking at the error and it is, for once, actually quite helpful.

It is saying you aren't @Overrideing anything. So maybe try without the @Override line? Your code passes the challenge, though. :wink:

There's no superclass method called compareTo() to @Override because your haven't implemented the Comparable interface which is where compareTo() comes from. That's the error being thrown there. See other answer.

Hi there,

You're looking at comparing the Object passed in to the current instance you're calling the method within. That instance is referenced with the keyword this. So, cast obj to BlogPost and compare with equals() to this.

Make sense?

Steve.

Solomon Asiedu-Ofei Jnr
Solomon Asiedu-Ofei Jnr
1,015 Points

Hey Steve...im really lost with your explanation how do I compare the equals to "this"

You've not implemented Comparable - that's the issue here.

Add implements Comparable after the class:

public class BlogPost implements Comparable{

Sorry I missed that!

Steve.

Solomon Asiedu-Ofei Jnr
Solomon Asiedu-Ofei Jnr
1,015 Points

FINALLY STEVE I made it through. This took me a whooping 4 days. feels so great. You are a life saver!!!!!

Happy to help! Shout earlier in the Community pages - don't struggle for days! :+1: :smile:

Steve.