• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple extending enum question...  RSS feed

 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • 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?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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;
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!