2
votes

I have a subclass that declares all of the methods in my abstract superclass, yet it still gives me an error stating that my class isn't abstract. I cannot figure out why this error is getting thrown.

The specific error I'm getting is

PhoneBookEntry.java:1: error: PhoneBookEntry is not abstract and does not override abstract method compareTo(Object) in Comparable

My code in question:

public abstract class PhoneNumber implements Comparable
{
   protected String firstName, lastName;
   protected int number;

   public PhoneNumber(String firstName, String lastName, int number)
   {
      this.firstName = firstName;
      this.lastName = lastName;
      this.number = number;
   }

   public abstract String getLastName();
   public abstract String getFirstName();
   public abstract int getNumber();

   public int compareTo(PhoneNumber other)
   {
      int last = other.lastName.compareTo(lastName);
      int first = other.firstName.compareTo(firstName);
      int num = other.number - number;
      if(last > 0)
         return 1;
      else if(last < 0)
         return -1;
      else
         if(first > 0)
            return 1;
         else if(first < 0)
            return -1;
         else
            if(num > 0)
               return 1;
            else if(num < 0)
               return -1;
            else 
               return 0;
   }
}

And my subclass:

public class PhoneBookEntry extends PhoneNumber
{
   public PhoneBookEntry(String firstName, String lastName, int number)
   {
      super(firstName, lastName, number);
   }

    public String getLastName()
   {
      return lastName;
   }
   public String getFirstName()
   {
      return firstName;
   }
   public int getNumber()
   {
      return number;
   }

   public int compareTo(PhoneNumber other)
   {
      super.compareTo(other);
   }

}
5

5 Answers

10
votes

This is the problem:

public int compareTo(PhoneNumber other)
{
   super.compareTo(other);
}

You've specified that you're just implementing the raw type Comparable, which has a method with a signature of:

int compareTo(Object)

The cleanest fix for this is to change the declaration of PhoneNumber to:

public abstract class PhoneNumber implements Comparable<PhoneNumber>

You could implement compareTo(Object) instead, but do you really want to be able to compare a phone number with any other object? It makes more sense (IMO) to just claim to be able to compare a phone number with other phone numbers.

3
votes

You are implementing the raw version of Comparable, whose compareTo method takes an Object.

Implement the generic version of Comparable instead:

public abstract class PhoneNumber implements Comparable<PhoneNumber>
1
votes

That is because

Comparable.compareTo(Object o); {} 

is not equal to

   public int compareTo(PhoneNumber other)
   {
      super.compareTo(other);
   }

Change

public abstract class PhoneNumber implements Comparable

to

public abstract class PhoneNumber implements Comparable<PhoneNumber>

and it will work the TypeSafe way you want it to.

1
votes

does not override abstract method compareTo(Object) in Comparable

Your sub class doesn't have a compareTo(Object) You could add such a method but the best solution is to change

public abstract class PhoneNumber implements Comparable<PhoneNumber> {

that way it expects a compareTo(PhoneNumber) which is what you have.

1
votes

Change:

   public int compareTo(PhoneNumber other)
   {
      super.compareTo(other);
   }

to

   @Override
   public int compareTo(Object other)
   {
      return super.compareTo((PhoneNumber)other);
   }