简体   繁体   中英

Java 8 default method override (and eclipse)

I am working with Java 8.

I have the following interface declaration:

public interface IRequestBuilder {
    public String processParameters();
}

From this interface, I derived another interface:

public interface IMyRequestBuilder extends IRequestBuilder {

    @Override
    default String processParameters(){
        return createBody();
    }

    String createBody();

}

(I derived another interface from IRequestBuilder in which processParameters() is not set as default for other purposes).

Everything works fine, except that each class that I derive from IMyRequestBuilder asks me to implement processParameters() which I do not want to do because it is a default method.

I work on Eclipse - is it an Eclipse issue only? How can I avoid it?

What you're seeing here is only an Eclipse bug.

Let's consider the following code:

interface IRequestBuilder {
    public String processParameters();
}

interface IMyRequestBuilder extends IRequestBuilder {
    @Override
    default String processParameters() {
        return createBody();
    }
    String createBody();
}
class A implements IMyRequestBuilder {

}

Eclipse rightfully gives a compile error:

The type A must implement the inherited abstract method IMyRequestBuilder.createBody()

It suggests quick fixes, including the "Add unimplemented methods" quick fix. If you use this quick fix, you will find that Eclipse (Mars) generates stubs for both createBody and processParameters .

However, the code will compile just fine if you remove the processParameters stub (which is expected because the processParameters method is a default method).

I found two bug reports of this in Eclipse bug tracker ( bug 409520 and bug 476517 ). The latest (bug 476517) is "RESOLVED FIXED" for the next version of Eclipse (Neon).

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