aspose file tools
The moose likes Beginning Java and the fly likes Simple extending enum question... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Simple extending enum question..." Watch "Simple extending enum question..." New topic
Author

Simple extending enum question...

Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
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
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
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
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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

Joined: Jul 31, 2003
Posts: 294
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
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You're trying to use a Dog reference on an Animal.
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
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;
 
jQuery in Action, 2nd edition
 
subject: Simple extending enum question...
 
Similar Threads
extend class enum
Runtime
How to type cast the Object if im having class Name as string?
Iterating through unknown enum type variable
cast int to an enum