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 trialkemishobowale
3,627 Pointserror: method does not override or implement a method from a supertype
Hello I'm getting this message but i'm not sure as to why, please help?
error: method does not override or implement a method from a supertype @Override
package com.teamtreehouse; import java.util.Date; import java.lang.Object;
public class Treet { //m to show its the member (or class?) variable private String mAuthor; private String mDescription; private Date mCreationDate;
public Treet(String Author, String Description, Date CreationDate){ this.mAuthor = Author; this.mDescription = Description; this.mCreationDate = CreationDate; }
public String getAuthor(){ return mAuthor; }
@Override public String toString(){ return String.format("Treet: \"%s\" by %s on %s", mDescription, mAuthor, mCreationDate); }
@Override public int compareTo(Object obj){ Treet other = (Treet) obj; //cast to treet if (equals(other)){ //treet compared to treet is 0 return 0; } int dateCmp = mCreationDate.compareTo(other.mCreationDate); //compare the dates of //the treet if it is equal (0) run a check on descritption to make sure the treet //are not identical if(dateCmp == 0){ return mDescription.compareTo(other.mDescription); } return dateCmp; }
public String getDescription(){ return mDescription; }
public Date getCreationDate(){ return mCreationDate; }
public String[] getWords(){
return mDescription.toLowerCase().split("[^\w]+");
}
}
3 Answers
Wout Ceulemans
17,603 PointsLittle bit formatted for reference: https://hastebin.com/urumolujen.java
You can override the toString() method because its a method inside the Object-class, which you automatically inherit from if you don't extend any class. https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()
However, the compareTo method is not defined in that Object-class. It is defined in the Comparable interface: https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
In order to be able to override the compareTo method, you have to implement the Comparable interface.
Nicolás Lavín
1,922 Pointsshame on me
Chufan Xiao
18,955 PointsI got the same issue
kemishobowale
3,627 Pointskemishobowale
3,627 PointsThank you...totally missed that part out!
ohadyohay
867 Pointsohadyohay
867 PointsI have the exact same error and I don't understand what do I need to change in my code in order to make it work. my code is:
Chufan Xiao
18,955 PointsChufan Xiao
18,955 Pointsthx I solve it