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

kemishobowale
kemishobowale
3,627 Points

error: 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
Wout Ceulemans
17,603 Points

Little 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.

kemishobowale
kemishobowale
3,627 Points

Thank you...totally missed that part out!

I 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:

package com.teamtreehouse;
import java.util.Date;

public class Treet {
  private String mAuthor;
  private String mDescription;
  private Date mCreationDate;

  public Treet(String author, String description, Date creationDate) {
    mAuthor = author;
    mDescription = description;
    mCreationDate = creationDate;
  }

  @Override
  public String toString() {
    return String.format("Treat:  \"%s\" - @%s  on %s",
                         mDescription, mAuthor, mCreationDate);
  }

  @Override
  public int compareTo(Object obj) {
    Treet other = (Treet) obj;
    if (equals(other)) {
      return 0;
    }
    int dateCmp = mCreationDate.compareTo(other.mCreationDate);
    if (dateCmp == 0) {
      return mDescription.compareTo(other.mDescription);
    }
    return dateCmp;
  }

  public String getAuthor() {
    return mAuthor;
  }

  public String getDescription() {
    return mDescription;
  }

  public Date getCreationDate() {
    return mCreationDate;
  }

  public String[] getWords() {
    return mDescription.toLowerCase().split("[^\\w@#']+");
  }
}
Chufan Xiao
Chufan Xiao
18,955 Points

I got the same issue