I tried reproducing the code below on eclipse. I get an error telling me that I have to implement all the inherited methods (because Comparator is an interface).
The type
new Comparator(){}
must implement the inherited abstract methodComparator.reversed()
.
There are many of these methods and the only one I want to overwrite is compare. Do I have to implement all the other methods or is there a way to specify I don't need to implement them? I understand that I will have to do it because of the contractual nature of an interface, but what if I just need to change one method?
static Map sortByValue(Map map) {
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
Map result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
EDIT Solved by changing the compliance level to java8 in eclipse luna. Thanks!
If you don't a compile time error will be generated for each unimplemented method saying “InterfaceExample is not abstract and does not override abstract method method_name in interface_name”.
Declare the missing methods abstract in your class. This forces you to declare your class abstract and, as a result, forces you to subclass the class (and implement the missing methods) before you can create any objects.
A class must implement all methods of the interface in its implementation part, with the following exceptions: Interface methods declared as optional using the addition DEFAULT.
What will happen if we provide concrete implementation of method in interface? Explanation: The methods of interfaces are always abstract. They provide only method definition.
The type
new Comparator(){}
must implement the inherited abstract methodComparator.reversed()
then if I apply the fix, I have many functions added
Comparator.reversed
was introduced in Java 1.8 and it's a default method i.e. a method that you don't have to override.
It seems like you have your compliance level set to pre Java 1.8 (since Eclipse asks you to override reversed
), while using Java 1.8 API (since Comparator
has a reversed
method).
Make sure you either change your API to 1.7 or change your compliance level to 1.8. (The latter option requires Eclipse Luna or better.)
More on Eclipse compliance level: What is "compiler compliance level" in Eclipse?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With