Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not implementing all the methods of an interface

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 method Comparator.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!

like image 421
giulio Avatar asked Jan 28 '15 22:01

giulio


People also ask

What happens if we dont implement all methods of interface?

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”.

How can we avoid implementing all methods interface?

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.

Do we need to implement all the methods of interface in SAP?

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 happens if we provide the implementation of a method in an interface?

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.


1 Answers

The type new Comparator(){} must implement the inherited abstract method Comparator.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?

like image 126
aioobe Avatar answered Sep 27 '22 22:09

aioobe