Home » Java » Java inheritance in interface

Java inheritance in interface

Posted by: admin October 29, 2018 Leave a comment

Questions:

I have two interfaces:

public interface EventListener {

    <T extends Data> T modify(T data);

    public static class Data {

    }
}

and

public interface ServerInfoEventListener extends EventListener {

    // This works
    @Override ServerInfoData modify(Data data);

    // This doesn't work!
    @Override ServerInfoData modify(ServerInfoData data);

    public static class ServerInfoData extends Data {
        public ServerInfoData(String motd, int playerCount, int maxPlayers) {
            this.motd = motd;
            this.playerCount = playerCount;
            this.maxPlayers = maxPlayers;
        }
        public String motd;
        public int playerCount;
        public int maxPlayers;
    }
}

As you can see from the comments, modify(ServerInfoData data) does not compile. Why not, and how do I fix it? ServerInfoData extends Data so it should work, right? Thanks in advance.

Answers: