0

I keep getting the error: Student is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable

Why is this? What this is trying to accomplish is taking a list of students and comparing them by GPA.

public class Student implements Comparable
    {
        private String name;
        private double gpa;    
        public Student(String name, double gpa)
        {
            this.name = name;
            this.gpa = gpa;
        }    
        public String getName()
        {
            return name;
        }
        public double getGpa()
        {
            return gpa;
        }    
        public String toString()
        {
            return "Name: " + name + "  GPA: " + gpa;
        }
        public double compareTo(Object other)
        {
            Student filler = (Student)other;
            if(this.getGpa() < filler.getGpa())
                return -1;
            else if(this.getGpa() == filler.getGpa())
                return 0;
            else
                return 1;
        }    
    }

3 Answers3

0
public double compareTo(Object other)

should be

@Override
public int compareTo(T other)

Take a look at the Comparable interface. It requires a method called compareTo, that takes an argument of type T (generic parameter), that returns an int. The method you have created doesn't implement the method specified in the interface, which is why the Java compiler is complaining.

Since the Comparable interface is genericized, you should take advantage of generics and make your class implement Comparable<Student>. When you do that, the signature of compareTo becomes:

@Override
public int compareTo(Student other)

Which is better than a raw Object, since you don't have to cast, and more importantly, you don't accidentally end up passing in something that is not a Student.

One more thing: use the @Override annotation when you implement methods from the interface. Assuming you're using a halfway-decent IDE, you would have seen an error if you had:

@Override
public double compareTo(Object other)

Since there is no method with that signature in the interface.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
0

To answer your question directly, you need to change the return type of compareTo() from double to int.

There are also several other modifications you should make to improve your code:

  1. implement Comparable<Student> instead of just Comparable. This makes it so you can write public int compareTo(Student other) and only allows calling compareTo() with other Student references.

  2. Add @Override annotations before both toString() and compareTo(). This annotation helps you avoid some common errors which the compiler cannot catch.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

compareTo method returns an int and not a double.

Also using an Override annotation helps to be sure that you are overriding the method correctly. So change this

public double compareTo(Object other)

to

@Override
public int compareTo(Object other)
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136