Extenable enum class



What is the best way to model one extendable enum, no code been extended just data.
The idea is to have one base enum with a set of possible discrete values and extend this enum in another place with new additional values.
What I am looking to model is kind of of a protocol of basic operations that are expected to be possible and be able to provide additional discover-able set of operations.

Thank you, kindly
Luis Oscar


It depends on what you want and at what semantic level. At base level you use an interface and have the enum implement this interface (and any other enum can do the same). You cannot close this though and loose some of the advantages of having an enum in the first place.

It depends really what you want. There are different goals:

  • Ensure uniqueness (no duplicate items)
  • The ability to enumerate operations (just put them in a set)
  • Fast switching on values

What you probably want is some sort of protocol manager object that is responsible for providing operation objects. It could be possible to register new objects in some way, managed by the manager class. Operation objects can be an inner class of the manager and have private constructors such that only the manager can manage them. The manager can even maintain sequence numbers for efficient when/switch statements. You will basically have to write your own almost-enum class that doesn’t come with the restrictions of enum. You will have to take care of the initialisation issues manually though.


Hi @pdvrieze:

This is what I end up making:

import org.apache.camel.builder.RouteBuilder
interface Operation {
    val name: String
    val description: String
data class DefaultOperation(override val name: String, override val description: String) : Operation
    object Availability : Operation by DefaultOperation("Availability", "Retrieve availabilities")
    object Booking : Operation by DefaultOperation("Booking", "Make a booking")
    object BookingInfo : Operation by DefaultOperation("BookingInfo", "Retrieve booking info")
    object Cancel : Operation by DefaultOperation("Cancel", "Cancel a booking")
    object Locations : Operation by DefaultOperation("Locations", "Retrieve list of locations")
abstract class OperationsRouteBuilder(val operations: Map<Operation, String>) : RouteBuilder()


I saw an other way to make extensive enum by combining enum and interfaces on this article : https://dzone.com/articles/java-extensible-enum-with-interface


Thank you. for the reference