Reputation: 2188
I have an abstract class Person and and interface comparable, which is also used for some other part of the program. Currently I have a method compareTo() in Person. When I try to compile, I get :
The type Student must implement the inherited abstract method
Comparable<Person>.compareTo(Person, Person)
What exactly do I have to do? I don't wont to implement this method in any of the subclasses, because I need this method for all of them, Student, Tutor, Professor, etc... Is there a better way of doing this?
Interface:
interface Comparable<Element> {
public int compareTo(Element nodeA, Element nodeB);
}
Abstract class Person:
abstract class Person implements Comparable<Person> {
protected String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
public String toString() {
return name;
}
public int compareTo(Person personB) {
int comp = this.name.compareTo(personB.getName());
return comp;
}
}
And class Student
class Student extends Person implements Comparable<Person> {
private int id;
public Student(String name, int id) {
super(name);
this.id = id;
}
public int getID() {
return id;
}
public void setID(int newID) {
id = newID;
}
public String toString() {
return id + ", " + name;
}
}
Upvotes: 3
Views: 12947
Reputation: 8100
Your Comparable<Element>
class declares a method public int compareTo(Element nodeA, Element nodeB);
, but in your Person
class, you implement public int compareTo(Person personB)
, which is not the same method signature.
You need to either implement public int compareTo(Person personA, Person personB)
, or alter your Comparable<Element>
class's method definition to be public int compareTo(Element other);
to override the core Comparable
class's compareTo method.
Also, as @murat mentions below in the comment, using the @Override
annotation would help you out (assuming you're on Java version 1.5 or higher). If you add @Override
to a method that you're not actually overriding from a superclass (such as your two-argument compareTo
method), then it will be a compiler error.
Upvotes: 2
Reputation: 51030
interface Comparable<Element> {
public int compareTo(Element nodeA, Element nodeB);
}
It makes sense if it was either:
interface Comparator<Element> { //comparator compares two instances of same type
public int compare(Element nodeA, Element nodeB);
}
or (has to be something like this for your case):
interface Comparable<Element> { //comparable compares itself with another instance of same type
public int compareTo(Element that);
}
But for both cases, you should use the standard Java interfaces: Even though you are only using Comparable
also see Comparator
.
Upvotes: 0
Reputation: 1052
Change your interface from:
interface Comparable<Element>
{
public int compareTo(Element nodeA, Element nodeB);
}
to:
interface Comparable<Element>
{
public int compareTo(Element nodeA);
}
And make your Person class be defined as:
abstract class Person implements Comparable<? extends Person> { /* ... */ }
And make your Student (and other Person-subclasses be):
class Student extends Person { /* ... */ }
That is all.
Upvotes: 2
Reputation: 27233
You should implement the method as it appears in the interface, i.e. with two arguments
public int compareTo(Person nodeA, Person nodeB)
To avoid such problems in the future use the @Override
annotation:
@Override
public int compareTo(Person nodeA, Person nodeB)
This will cause a compilation error if you try to override a method, but make a mistake in its signature.
Also, consider using Java's standard Comparable.
Upvotes: 2
Reputation: 691625
Your Comparable interface has a method compareTo(Element nodeA, Element nodeB)
. This method is not defined in Student, and it's not defined in Person either. Person has the following method:
public int compareTo(Person personB)
, which doesn't override compareTo(Person nodeA, Person nodeB)
Why are you redefining the standard java.util.Comparable
interface?
Upvotes: 2
Reputation: 54780
You need to implement
public int compareTo(Person nodeA, Person nodeB)
In your Person
class. Currently you only have:
public int compareTo(Person personB)
Upvotes: 0