Extending an enum

Discussion in 'Java' started by Roedy Green, Jul 16, 2007.

  1. Roedy Green

    Roedy Green Guest

    You can't extend an enum to either add more enum constants or more
    static methods or more instance methods on the enum constants, or more
    instance datafields on each enum constant.

    Any thoughts on how to fake various types of extensions?
    ..

    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 16, 2007
    #1
    1. Advertising

  2. 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
     
    Ingo R. Homann, Jul 16, 2007
    #2
    1. Advertising

  3. Roedy Green

    Roedy Green Guest

    On Mon, 16 Jul 2007 11:54:41 GMT, Roedy Green
    <> wrote, quoted or indirectly quoted
    someone who said :

    >You can't extend an enum to either add more enum constants or more
    >static methods or more instance methods on the enum constants, or more
    >instance datafields on each enum constant.


    Why would I want to do such a thing? In this case I am working on the
    Replicator which has two halves the applet that runs at the client
    and the sender than runs on my machine. I wanted to keep the client
    code as small as possible.

    So wanted to have a core enum class, and extend it with extra methods
    on each enum constant only used by the receiver and ditto for the
    sender.

    I did not want to repeat code since it would not stay in sync. So
    what I did was a bit ugly but it works.

    I passed an enum constant of the common enum class to the
    corresponding enum constructor on the extending class. Then I wrote
    wrapper methods to let the extending class present the common methods.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 16, 2007
    #3
  4. Roedy Green

    Daniel Pitts Guest

    On Jul 16, 6:49 am, Roedy Green <>
    wrote:
    > On Mon, 16 Jul 2007 11:54:41 GMT, Roedy Green
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    > >You can't extend an enum to either add more enum constants or more
    > >static methods or more instance methods on the enum constants, or more
    > >instance datafields on each enum constant.

    >
    > Why would I want to do such a thing? In this case I am working on the
    > Replicator which has two halves the applet that runs at the client
    > and the sender than runs on my machine. I wanted to keep the client
    > code as small as possible.
    >
    > So wanted to have a core enum class, and extend it with extra methods
    > on each enum constant only used by the receiver and ditto for the
    > sender.
    >
    > I did not want to repeat code since it would not stay in sync. So
    > what I did was a bit ugly but it works.
    >
    > I passed an enum constant of the common enum class to the
    > corresponding enum constructor on the extending class. Then I wrote
    > wrapper methods to let the extending class present the common methods.
    > --
    > Roedy Green Canadian Mind Products
    > The Java Glossaryhttp://mindprod.com


    Perhaps "enum" isn't the right construct for you. Perhaps an
    extensible type token is what you're looking for?
     
    Daniel Pitts, Jul 16, 2007
    #4
  5. Roedy Green

    Roedy Green Guest

    On Mon, 16 Jul 2007 13:01:15 -0700, Daniel Pitts
    <> wrote, quoted or indirectly quoted
    someone who said :

    >Perhaps "enum" isn't the right construct for you. Perhaps an
    >extensible type token is what you're looking for?


    I think you are right. I started with an enum solution based on ints.
    By the time I was done I had no more switches, so that got rid of the
    #1 attraction of enum.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 16, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    extending enum

    Roedy Green, Jul 31, 2005, in forum: Java
    Replies:
    18
    Views:
    37,011
    chrismay
    Jun 25, 2011
  2. Simon Elliott

    extending enum's: what operators?

    Simon Elliott, Jan 9, 2006, in forum: C++
    Replies:
    7
    Views:
    495
    Pete Becker
    Jan 9, 2006
  3. Extending Enum

    , May 15, 2007, in forum: Java
    Replies:
    3
    Views:
    1,000
  4. Christopher

    deriving a class - extending enum

    Christopher, Dec 5, 2007, in forum: C++
    Replies:
    15
    Views:
    1,282
    Roland Pibinger
    Dec 6, 2007
  5. Laura Schmidt
    Replies:
    8
    Views:
    138
    Roedy Green
    Jun 16, 2014
Loading...

Share This Page