Re: Extending an enum
Hi Roedy,
Roedy Green wrote:
You can't extend an enum to either
> Any thoughts on how to fake various types of extensions?
My ideas:
> (1) add more enum constants or
Use a simple class instead of an enum:
class Color {
static final Color red=new Color(255,0,0);
}
class Color2 {
static final Color pink=new Color(255,200,200);
}
BTW: I suppose you know the reason, *why* sun decided to make enums not
extendable!?
> (2) more static methods or
I think there is no necessity to do so. Of course, it might have been
"nicer" to write "String content=File.read(...);" instead of "String
content=MyUtilFileClass.read(...);", but I think it is not really that
important.
> (3) more instance methods on the enum constants
No possibility. You cannot even do that with classes, because you simply
cannot convert an Object of type "MySuperClass" to an Object of
"MySubClass". Of course, you may use static helper classes, but I think
you do not want to hear this! ;-)
> (4) more instance datafields on each enum constant.
Same as (3): Because Java has a very statical type system and an Object
(for clarification: in contrast to a reference) can never change its
type! Of course, you may use a (Weak)HashMap to "append" new attributes
to some Objects, but this is no clean solution, IMHO. Another
possibility is to "wrap" your Objects in new Objects which have the
required attributes.
Ciao,
Ingo