Results 1 to 4 of 4
  1. #1
    Tabula Rasa is offline Member
    Join Date
    Apr 2011
    Location
    USA
    Posts
    15
    Rep Power
    0

    Default Error message when implementing Comparable

    I am working on a test project and I get this error message:

    Student is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable


    I'm trying to sort student names in alphabetical order according to last name, then first name. My main class sorts the names in an ArrayList.



    import java.util.*;

    public class Student implements Comparable
    {
    private String lastname;
    private String firstname;


    public Student(String last, String first)
    {
    lastname = last;
    firstname = first;
    }


    public int compareTo(Student n)
    {

    int num = lastname.compareTo(n.lastname);
    return (num != 0 ? num : firstname.compareTo(n.firstname));
    }


    public String toString()
    {
    return lastname + " " + firstname;
    }


    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You are using the non-generic version of Comparable (in other words, your class implements Comparable, not Comparable<Student>), and the compareTo method signature must match that of the non-generic version. If you check the API, you'll see that the method parameter should be Object, not Student:

    not:
    Java Code:
    public int compareTo(Student n)  {
      //...

    but rather:
    Java Code:
    public int compareTo(Object n)  {
      //... you'll need a cast to Student in here!

    A better solution is to use the generic version of Comparable<Student>, and then your compareTo method signature would be correct:

    not:
    Java Code:
    public class Student implements Comparable {


    but rather:
    Java Code:
    public class Student implements Comparable<Student> {
      
      //....
    
      public int compareTo(Student n)  {
         //...  here it's OK to use a Student parameter, and no cast is needed.
    Last edited by Fubarable; 04-16-2011 at 05:40 PM.

  3. #3
    Tabula Rasa is offline Member
    Join Date
    Apr 2011
    Location
    USA
    Posts
    15
    Rep Power
    0

    Default

    Thanks for the help. :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. error message
    By tri.yudhanto in forum JDBC
    Replies: 0
    Last Post: 01-13-2011, 01:20 PM
  2. Error Message ..
    By Hamodi18 in forum New To Java
    Replies: 15
    Last Post: 07-11-2010, 03:31 AM
  3. error message - non-static
    By newbie123 in forum New To Java
    Replies: 5
    Last Post: 03-15-2010, 02:25 PM
  4. Error Message????
    By Cubba27 in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:46 PM
  5. error message on jsp
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2007, 02:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •