user3139545 user3139545 - 1 year ago 54
Java Question

Using higer order function to override abstract method in java

In an abstract class i have the following definitions:

protected abstract A expectedA(B b);

protected Function<A, B> createExpectedA(Long foo) {
return a -> { ... return b}}

And then I want to override the abstract function with the return function from
like so:

protected Function<A, B> expectedA = createExpectedA(fee);

However this gives me the following error:

The annotation @Override is disallowed for this location

How can I do what I want abowe in Java8?

Answer Source

The annotation Override is meant to be used on methods not on fields, that is why you get this error. As reminder, here is the Javadoc:

Indicates that a method declaration is intended to override a method declaration in a supertype. If a method is annotated with this annotation type compilers are required to generate an error message unless at least one of the following conditions hold:

  • The method does override or implement a method declared in a supertype.
  • The method has a signature that is override-equivalent to that of any public method declared in Object.

What you want to do seem to be something like this:

protected A expectedA(B b) {
    return createExpectedA(fee).apply(b);