Is there a way to write an enumeration that can be extended. I have several methods that I would like to always have available for my enumerations. For example I use an enumeration for my database fields. I include the actual field name in the database.
public enum ORDERFIELDS { OrderID('Order_ID'); private String FieldName; private ORDERFIELDS(String fname) { this.FieldName = fname; } public String getFieldName() { return FieldName; } }
If I understand correctly, what you’d like to do is something like this:
Then define your enum to extend this class. However, unfortunately an enum cannot extend a class, but it can implement an interface, so the best you can do at the moment is define an interface which includes the getFieldName() method and have all your enums implement this interface.
However, this means that you’ll have to duplicate the implementation of this method (and any others) in all your enums. There are some suggestions in this question about ways to minimise this duplication.