Say I create an enum but eventually someone wants to add items to that enum, what does one do? ex:
// blah.hpp
enum PizzaDressing {
DRESSING_OLIVES = 0,
DRESSING_CHEESE = 1
};
and in my FunkyPizza class, there might be pepper toppings.
So how could I add peppers without obviously modifying the original enum?
Thanks.
Enums can be extended in order to add more values to the enumeration list in which case the Extensible property must be set to true .
No, we cannot extend an enum in Java. Java enums can extend java. lang. Enum class implicitly, so enum types cannot extend another class.
All enumerations internally extend a class named Enum the base class of all the language enumeration types. Since Java doesn't support multiple inheritance you cannot extend another class with enumeration if you try to do so, a compile time error will be generated.
Since it's not a class, it cannot be extended and its defined values are limited to a small set of primitive types ( byte , sbyte , short , ushort , int , uint , long , ulong ). For instance, sometimes it is very convenient to get the list of enum values and display it in a combobox.
You can't dynamically modify an enum, because it only defines a new type resolved at compile time. They are mnemonics for the programmer, at compilation they are translated to numbers.
That said, you can use any number not used by the original enum to represent whatever you want:
PizzaDressing a;
a = (PizzaDressing)5;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With