简体   繁体   中英

Compiler not Recognizing that I've implemented the compareTo() method from the Interface it Implements

I'm implementing an interface 'Comparable' by the abstract class AbstractAffiliate, which is extended by the abstract class Abstract Faculty, which is extended by the regular class Assistant.

Upon implementing the compareTo method in the assistant class, which has been declared in all classes/interfaces above, the compiler gives this error.

Assistant.java:1: error: Assistant is not abstract and does not override abstract method compareTo() in Abstract Faculty public class Assistant extends AbstractFaculty{ ^ 1 error

I've tried adding as a generalizer to the implements Comparable.

Abstract Affiliate

public abstract class AbstractAffiliate implements Printable, Comparable<AbstractAffiliate>{

protected String name;
protected int age;
protected String address;
protected int phoneNumber;
protected int yearTheyCameToChapman;

    /**
    * Default empty AbstractAffiliate constructor
    */
public AbstractAffiliate() {
    super();
  age = 0;
  address = " ";
  phoneNumber = 0;
  yearTheyCameToChapman = 0;
}

public AbstractAffiliate(String name, int age, String address, int phoneNumber, int yearTheyCameToChapman){
    this.name = name;
    this.age = age;
    this.address = address;
    this.phoneNumber = phoneNumber;
    this.yearTheyCameToChapman = yearTheyCameToChapman;
}

public abstract String print();

public abstract int compareTo();

}

Abstract Faculty

public abstract class AbstractFaculty extends AbstractAffiliate{

protected int facultyId;
protected String department;
protected int yearlySalary;
protected int numberOfPapers;

    /**
    * Default empty AbstractFaculty constructor
    */
public AbstractFaculty() {
    super();
  facultyId = 0;
  department = " ";
  yearlySalary = 0;
  numberOfPapers = 0;
}

    /**
    * Default AbstractFaculty constructor
    */
public AbstractFaculty(String name, int age, String address, int phoneNumber, int yearTheyCameToChapman, int facultyId, String department, int yearlySalary, int numberOfPapers) {
    super(name, age, address, phoneNumber, yearTheyCameToChapman);
    this.facultyId = facultyId;
    this.department = department;
    this.yearlySalary = yearlySalary;
    this.numberOfPapers = numberOfPapers;
    }

public abstract String print();

public abstract int compareTo();



}

Assistant

public class Assistant extends AbstractFaculty{

private int yearsUntilTenure;

public Assistant(){
  super();
  yearsUntilTenure = 0;
}

public Assistant(String name, int age, String address, int phoneNumber, int yearTheyCameToChapman, int facultyId, String department, int yearlySalary, int numberOfPapers, int yearsUntilTenure){
  super(name, age, address, phoneNumber, yearTheyCameToChapman, facultyId, department, yearlySalary, numberOfPapers);
  this.yearsUntilTenure = yearsUntilTenure;
}

public String print(){
  return "yup";
}


public int compareTo(AbstractAffiliate affiliate){
  if (this.yearTheyCameToChapman < affiliate.yearTheyCameToChapman){
    return 1;
  }
  if (this.yearTheyCameToChapman > affiliate.yearTheyCameToChapman){
    return -1;
  }
  else{
    return 0;
  }
}



}
```[enter image description here][1]


  [1]: https://i.stack.imgur.com/Xdz2F.png

You haven't implemented the abstract method. The abstract .compareTo() method takes no parameters. By contrast, the version that you've implemented in your Assistant class takes an AbstractAffiliate as a parameter. Since they have different parameters, that makes them completely different methods.

At first glance, it looks like the version that takes a parameter is the one you want, and that should be taken care of by the fact that your base class implements Comparable<AbstractAffiliate> , so just remove your abstract compareTo() methods completely and you should be fine.

It's because you have declared to implement Comparable<AbstractAffiliate> and you're not implementing the method

int compareTo(AbstractAffiliate o)

Yours have a different signature (no parameter) Since AbstractAffiliate and AbstractFaculty are declared as abstracts, they do not have to implement the method from the Comparable interface. Assistant class needs to.

Refer to Comparable

The method signature of Comparable#compareTo is as below:

public int compareTo(T o);

But in Assistant and it's parent class you have added an abstract method

public abstract int compareTo();

Which is different from the method from Comparable#compareTo . Just remove the public abstract int compareTo(); method from its parent classes will solve the problem, there is no need to declare it again and again.

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