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 trialSolomon Asiedu-Ofei Jnr
1,015 PointsCannot 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
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
Steve Hunter
57,712 PointsOK - 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.
Steve Hunter
57,712 PointsHi 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
1,015 PointsHey Steve...im really lost with your explanation how do I compare the equals to "this"
Steve Hunter
57,712 PointsYou'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
1,015 PointsFINALLY STEVE I made it through. This took me a whooping 4 days. feels so great. You are a life saver!!!!!
Steve Hunter
57,712 PointsHappy to help! Shout earlier in the Community pages - don't struggle for days!
Steve.
Solomon Asiedu-Ofei Jnr
1,015 PointsSolomon Asiedu-Ofei Jnr
1,015 PointsHey Steve, I clearly understand now. I took away "this" and mu code look like this
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
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsLet me check - give me a minute ...
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsLooking at the error and it is, for once, actually quite helpful.
It is saying you aren't
@Override
ing anything. So maybe try without the@Override
line? Your code passes the challenge, though.Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsThere's no superclass method called
compareTo()
to@Override
because your haven't implemented theComparable
interface which is wherecompareTo()
comes from. That's the error being thrown there. See other answer.