Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding method with generics not working (no method found)

I am trying to @Override a method in a class that might look like having a complex inheritance structure, but it should actually be quite simple, however I cannot get it to work.

public interface Action { }

public interface Result { }

public interface Player<A extends Action, R extends Result> {
    default public <P extends Player<A, R>> void onPostAction(final P target, final A action, final R result) { }
}

abstract public class GesturePlayer<A extends Action, R extends Result> implements Player<A, R> { }

abstract public class RPSPlayer extends GesturePlayer<RPSGesture, RPSResult> { }

public class RPSHumanPlayer extends RPSPlayer {
    @Override
    public void onPostAction(final RPSPlayer target, final RPSGesture gesture, final RPSResult result) { }
}

I get the error on the @Override of the onPostAction, why can it not find the correct method to override?

This is for a Rock-Paper-Scissors implementation for people wondering where the name comes from.

The exact error message:

method does not override or implement a method from a supertype

My goal is to still use the current class, so for RPSHumanPlayer I would actually want to have this signature:

public void onPostAction(final RPSHumanPlayer target, final RPSGesture gesture, final RPSResult result) { }
like image 230
skiwi Avatar asked Mar 19 '14 20:03

skiwi


1 Answers

The method onPostAction in Player is itself generic. You have defined P there. Therefore, any method that overrides it must also be generic.

Try

public class RPSHumanPlayer extends RPSPlayer {
    @Override
    public <P extends Player<RPSGesture, RPSResult>> void onPostAction(final P target,
       final RPSGesture gesture, final RPSResult result) { }
}

A and R are already defined by GesturePlayer to be RPSGesture and RPSResult, but P still needs to be declared.

Addition

If it needs to be that exact signature, then you'll have to define P along with A and R on the Player interface:

public interface Player<A extends Action, R extends Result, P extends Player<A, R, P>> {
    default public void onPostAction(final P target, final A action, final R result) { }
}

Then GesturePlayer changes accordingly:

abstract public class GesturePlayer<A extends Action, R extends Result,
     P extends Player<A, R, P>> implements Player<A, R, P> { }

Then RPSPlayer defines itself to be P.

abstract public class RPSPlayer extends GesturePlayer<RPSGesture, RPSResult, RPSPlayer> { }

And RPSHumanPlayer can have the method as is:

public class RPSHumanPlayer extends RPSPlayer {
    @Override
    public void onPostAction(final RPSPlayer target, final RPSGesture gesture, final RPSResult result) { }
}
like image 73
rgettman Avatar answered Oct 18 '22 09:10

rgettman