Reputation: 33
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);
}
}
Upvotes: 2
Views: 1063
Reputation: 10707
Change:
public int compareTo(PhoneNumber other)
{
super.compareTo(other);
}
to
@Override
public int compareTo(Object other)
{
return super.compareTo((PhoneNumber)other);
}
Upvotes: 1
Reputation: 533442
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.
Upvotes: 1
Reputation: 1499790
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.
Upvotes: 10
Reputation: 178243
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>
Upvotes: 3
Reputation:
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.
Upvotes: 1