简体   繁体   中英

Java, how to inherit methods from abstract class

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;
    }
}

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.

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?

You should implement the method as it appears in the interface, ie 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 .

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.

You need to implement

public int compareTo(Person nodeA, Person nodeB)

In your Person class. Currently you only have:

public int compareTo(Person personB)

您需要在Student类中创建compareTo方法。

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM