1

I have a class as below,

public class Baseclass {
   private final EmployeeEnum empEnum;
   public Baseclass(EmployeeEnum e) {
       this.empEnum = e;
    }
}

Now I want to make the Baseclass generic and make it accept Enums of a certain type.

Since Enum cant extend a class I have created an interface IEnum and made EmployeeEnum and ManagerEnum(the new enum I created) implement the interface.

I have made changes to Baseclass as below,

public class Baseclass {
   private final Enum<?> empEnum;
   public Baseclass(Enum<?> e) {
       this.empEnum = e;
    }
}

Is there a better way to do this?

Cheers!!

2 Answers 2

8

If you merely want any enum then you can use E extends Enum<E>.

public class Baseclass<E extends Enum<E>> {

    private final E e;

    public Baseclass(E e) {
        this.e = e;
    }
}

All enums extend Enum<E> so that they can inherit the standard methods such as name and values. This is one of the reasons why enums cannot extend other classes because no class in Java is allowed to extend two classes.

Each sub-class must extend BaseClass with a specific enum like this:

enum MyEnum {

    I, Me, My, Mine;
}

class A extends BaseClass<MyEnum> {

    public A(MyEnum e) {
        super(e);
    }

}

If you want further restrictions - such as making subclasses only use enums of a special type (such as implementing an interface) then you can add the interface to the generic like this:

public interface SpecialEnum {

}

enum MyEnum implements SpecialEnum {

    I, Me, My, Mine;
}

enum NotSpecialEnum {

    Am, I, Special;
}

public class BaseClass<E extends Enum<E> & SpecialEnum> {

    private final E e;

    public BaseClass(E e) {
        this.e = e;
    }
}

class A extends BaseClass<MyEnum> {

    public A(MyEnum e) {
        super(e);
    }

}

// This is not allowed.
class B extends BaseClass<NotSpecialEnum> {

    public A(NotSpecialEnum e) {
        super(e);
    }

}

You can even put the enum inside the extending class:

class A extends BaseClass<A.AnotherEnum> {

    enum AnotherEnum implements SpecialEnum {

        Hello, All;
    }

    public A(AnotherEnum e) {
        super(e);
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

I want Enums of a type. Like an interface IEnum.
2

Is there a better way to do this?

Better way to do what? You could change to the code below which would limit you to enums that extend IEnum:

class Baseclass<T extends Enum<T> & IEnum> {
    private final T empEnum;
    public Baseclass(T e) {
        empEnum = e;
    }
}

So T extends Enum<T> - T must be an Enum & - and IEnum - T must extend IEnum.

4 Comments

Anyway other than modifying the class definition ? Just by changing the constructor signature?
Sorry but that makes no sense to me. Why do you not want to change the class definition?
The class has other constructors which takes parameters other than Enums.
Your answer helped. Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.