Not implementing all the methods of an interface

旧街凉风 提交于 2019-12-03 11:43:35
aioobe

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?

This is sort of a wild guess: I think you are using Java 8 in a pre-Java 8-Eclipse (i.e. pre-Luna). This way, Eclipse does not know that all those new Comparator methods, like thenComparing, have a default implementation.

In Java 7, Comparator has just the method compare, while in Java 8 in has a whole lot more methods, all of which have a default implementation directly in the interface and need not to be implemented in a subclass.

I suggest you switch to the newest version of Eclipse, which should be Luna. Alternatively, you can also install a patch for Eclipse Kepler, but switching to Luna is certainly better.

The purpose of an interface is to obligate the implementer of said interface to have all of the methods listed there. Now there are a few ways to got about not implementing all of the methods.

  1. For every method you don't wish to implement (support) just throw UnsupportedOperationException. An example of this sort of implementation would be the Collections API.

To keep the number of core collection interfaces manageable, the Java platform doesn't provide separate interfaces for each variant of each collection type. (Such variants might include immutable, fixed-size, and append-only.) Instead, the modification operations in each interface are designated optional — a given implementation may elect not to support all operations. If an unsupported operation is invoked, a collection throws an UnsupportedOperationException. Implementations are responsible for documenting which of the optional operations they support. All of the Java platform's general-purpose implementations support all of the optional operations.

  1. Or you could follow what the Java developers did with some of the interfaces that declare a lot of methods eg: MouseAdapter. This way you'll implement every method, but they won't be doing anything useful.

An abstract class may be better suited for what you are doing. Although you could just .super() everything.

You have run into an Eclipse bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=390889

Basically, JAVA 1.8 introduced a brand new method Comparator.reversed(). And since you have a JAVA 1.7 or earlier code, JAVA 1.8 doesn't find the method and fails to compile.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!