Can you explain why my compareTo method is not showing to be Overrided? I get er
ID: 3849633 • Letter: C
Question
Can you explain why my compareTo method is not showing to be Overrided? I get errors from my compliler that my class needs to be abstract because I am not correctly overriding the compareTo method.
What simple thing am I missing here to get compareTo to be overrided?
Here is my code:
public class Vehicle implements Comparable {
private String make;
public Vehicle(String inputMake) {
make = inputMake;
}
@Override
public String toString() {
return "working on this method";
}
@Override
public int compareTo(Vehicle vehicle) {
int difference = make.compareToIgnoreCase(vehicle.getMake(vehicle));
return difference;
}
public String getMake(Vehicle vehicle) {
return make;
}
}
Explanation / Answer
You need to implement Comparable<Vehicle> rather than Comparable for your class to compile and work.
If you are implementing Comparable, the expected method signature is compareTo(Object o)which is missing in your class and hence the error.
class Vehicle implements Comparable<Vehicle> {
private String make;
public Vehicle(String inputMake) {
make = inputMake;
}
@Override
public String toString() {
return "working on this method";
}
@Override
public int compareTo(Vehicle vehicle) {
int difference = make.compareToIgnoreCase(vehicle.getMake(vehicle));
return difference;
}
public String getMake(Vehicle vehicle) {
return make;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.