• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple extending enum question...

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers

I am extending emum and trying to cast it to the extending enum:



I am getting Class cast because EVALUE is FooEnum and not BarEnum
What is the correct way/pattern for such case?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you're trying to accomplish. If you want to use an enum you should not create a class but an enum. The Oracle Java Tutorial section about enums can be found here.
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:I'm not sure what you're trying to accomplish. If you want to use an enum you should not create a class but an enum. The Oracle Java Tutorial section about enums can be found here.



I m trying to inherit legacy class implementing enum and adding functionality..

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then I would not use the name Enum because it will confuse people with the enum functionality of the java language.
But about your problem. I'll give you a hint by rename it:

 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately Animal cannot be changed because it is used by other APIs, adding values to Animal is impossible.


I didn't get it, you will still get ClassCastException on Dog b = Dog.EVALUE;
What is the difference?

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're trying to use a Dog reference on an Animal.
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found a solution I can live with:

Existing enum will stay intact:


Inheriting class will add new functionality as so:



//No ClassCastException
FooEnum b = BarEnum.NEW_VALUE;
 
reply
    Bookmark Topic Watch Topic
  • New Topic