Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

abstract compareTo method not being overrided

When compiling the code below, I get the following error:

PersonalInformation is not abstract and does not override abstract method compareTo(Object) in Comparable

I assume that means I have a problem with my compareTo method. But everything seems to be all right. Anyone have a suggestion?

import java.util.*;
public class PersonalInformation implements Comparable
{
private String givenName;
private String middleInitial;
private String surname;
private String gender;
private String emailAddress;
private String nationalId;
private String telephoneNum;
private String birthday;

public PersonalInformation(String gN, String mI, 
        String sur, String gen, String eMa, String natId,
        String teleNum, String birthd)

{
        givenName = gN;
        middleInitial = mI;
        surname = sur;
        gender = gen;
        emailAddress = eMa;
        nationalId = natId;
        telephoneNum = teleNum;
        birthday = birthd;


}


public int compareTo(PersonalInformation pi)
{
 return (this.gender).compareTo(pi.gender);
}

}
like image 941
bitva Avatar asked Feb 20 '12 14:02

bitva


People also ask

Do you have to override compareTo?

So essentially you need to override compareTo() because you need to sort elements in ArrayList or any other Collection.

Why do we override compareTo method?

In order to change the sorting of the objects according to the need of operation first, we have to implement a Comparable interface in the class and override the compareTo() method.

How compareTo method works internally in Java?

Java String compareTo() MethodThe method returns 0 if the string is equal to the other string. A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters).

Is there a default compareTo method in Java?

The compareTo method defines the natural order; the default way for ordering objects of a class. It should return a negative integer(usually -1), if the current triggering object is less than the passed one, and positive integer (usually +1) if greater than, and 0 if equal.


3 Answers

Do this:

public int compareTo(Object pi) {
    return ((PersonalInformation )(this.gender)).compareTo(((PersonalInformation ) pi).gender);
}

or better

public class PersonalInformation implements Comparable<PersonalInformation>

If you implement the Comparable Interface you have to implement it either for all Objects using the first method or type your class the second way.

like image 186
juergen d Avatar answered Oct 03 '22 22:10

juergen d


You need to implement Comparable<PersonalInformation> rather than Comparable for your class to compile and work.

If you are implementing Comparable, the expected method signature is compareTo(Object o) which is missing in your class and hence the error.

like image 21
Scorpion Avatar answered Oct 03 '22 23:10

Scorpion


See juergen d's answer, much better.

You're overloading the method:

public int compareTo(PersonalInformation pi)
{
    return (this.gender).compareTo(pi.gender);
}

instead of overriding it:

public int compareTo(Object pi)

It could be something like:

public int compareTo(Object pi)
{
    if ( ! pi instanceof PersonalInformation )
        return false;
    return (this.gender).compareTo( (PersonalInformation)pi.gender );
}
like image 30
Luchian Grigore Avatar answered Oct 03 '22 23:10

Luchian Grigore