How do I properly extend / subclass EnumSet in Java?
I tried expanding EnumSet
to implement Comparable
in Eclipse. However, I am wrong from the beginning. Here I start with:
package sets;
import java.util.EnumSet;
enum Suits{
SPADE, DIAMOND, CLUB, HEART;
}
class ExtendedEnumSet extends EnumSet<Suits> implements Comparable<Suits> {
}
(problem 1) Defining an explicit constructor
He immediately tells me: Implicit super constructor EnumSet<Suits>() is undefined for default constructor. Must define an explicit constructor.
So, I follow the quick fix and add the following constructor:
ExtendedEnumSet(Class<Suits> finalArg0, Enum[] finalArg1) {
super(finalArg0, finalArg1);
// TODO Auto-generated constructor stub
}
... where he then tells me The constructor EnumSet<Suits>(Class<E>, Enum[]) is not visible
. I have tried changing the access modifier of both this class and this constructor to no avail.
(issue 2) Overriding Abstract Methods
The next problem is that when I decided to go ahead and fix this error reports Eclipse: The type ExtendedEnumSet must implement the inherited abstract method AbstractCollection<Suits>.iterator()
. Of course, this is just the tip of the iceberg. I use quick fix ( add unimplemented methods
) again and add the following:
@Override
public int compareTo(Suits finalO) {
// TODO Auto-generated method stub
return 0;
}
@Override
void addAll() {
// TODO Auto-generated method stub
}
@Override
void addRange(Suits finalArg0, Suits finalArg1) {
// TODO Auto-generated method stub
}
@Override
void complement() {
// TODO Auto-generated method stub
}
@Override
public Iterator<Suits> iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
Where he reports The method addAll() of type ExtendedEnumSet must override or implement a supertype method
. He repeats this error for addRange
and complement
. Can I copy the signature addAll
of Set
: public boolean addAll(Collection<? extends Suits> collection)
. However, when I try to copy other method signatures ( addRange
, complement
) from the API docs, they don't seem to exist. I'm at a loss.
I went with EnumMap
and it works great. EnumSet
seems impossible. Did I miss something?
source to share
Short answer: EnumSet
not intended to be extended outside of a package java.util
.
Long answer: EnumSet
has two implementations: MiniEnumSet
and HugeEnumSet
. The mini one is optimized to use one long
to represent values; huge uses several lengths. These classes are "hidden" inside the JRE, so the calling code doesn't know the difference. This is why EnumSet provides many static factory methods for creating new instances. This is a general design pattern for avoiding a brittle base class antipattern.
The compiler tells you that you cannot call the super-constructor of the class because the developers marked it as package-private. Thus, you will need to package the code under java.util
in order to call it. It's the same with other methods that you tried to undo.
source to share
I guess I EnumSet
never intended to be extended with custom classes. Look at the signature of this method:
abstract void addAll();
It is abstract, but package-private , so only classes in one package can appear (call or override) this method.
TL; DL: Unfortunately, you cannot extend EnumSet
with your own class, because that is not possible.
source to share