How to use generic EnumMap as parameter in abstract methods

1.2k views Asked by At

I tried to use a generic EnumMap as paramter in an abstract method. My Problem is that when I implement the abstract method with an existing enum for the EnumMap the compiler tells me that I have to remove the Override Annotation and implement the super method.

Here is my abstract class:



    import java.util.EnumMap;
    import java.util.HashMap;

    public abstract class AbstractClazz {

        // The methode I tried to define
        public abstract <K extends Enum<K>> boolean isVisible(EnumMap<K, Object> visibleConditions);

        // second test
        public abstract <K> boolean isVisible2(HashMap<K, Object> visibleConditions);

        // third test
        public abstract boolean isVisible3(EnumMap<?, Object> visibleConditions);
    }

And the implementing class:



    import java.util.EnumMap;
    import java.util.HashMap;

    public class Clazz extends AbstractClazz {

        public enum Numbers {
            ONE, TWO, THREE
        }

        // Error: The method isVisible(EnumMap<Clazz.Numbers,Object>) of type Clazz must override or implement a supertype method
        @Override
        public boolean isVisible(EnumMap<Numbers, Object> visibleConditions) {
            return false;
        }

        // Error: The method isVisible2(HashMap<Clazz.Numbers,Object>) of type Clazz must override or implement a supertype method
        @Override
        public boolean isVisible2(HashMap<Numbers, Object> visibleConditions) {
            return false;
        }

        // Error: The method isVisible3(EnumMap<Numnbers,Object>) of type Clazz must override or implement a supertype method
        @Override
        public boolean isVisible3(EnumMap<Numnbers, Object> visibleConditions) {
            return false;
        }
    }

Maybe Iam too silly, but what am I doing wrong?

Can anyone help me?

1

There are 1 answers

2
David ten Hove On BEST ANSWER

You should type your classes, not just your methods. Try this:

import java.util.EnumMap;
import java.util.HashMap;

public abstract class AbstractClazz<K extends Enum<K>> {

    public abstract boolean isVisible(EnumMap<K, Object> visibleConditions);
}

And:

import java.util.EnumMap;

public class Clazz extends AbstractClazz<Numbers> {

    @Override
    public boolean isVisible(EnumMap<Numbers, Object> visibleConditions) {
        return false;
    }
}

EDIT: To make it work, put the Numbers enum in its own file:

public enum Numbers {
    ONE, TWO, THREE
}