What is the difference between enum
(Enum Classes) and sealed
(Sealed Classes). And when it is advisable to use them.
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
sealed class Direction {
class NORTH
class SOUTH
class WEST
class EAST
}
Sealed Classes are used to represent restricted class hierarchies, where a value can have one of a limited set of types, but cannot have any other type.
Sealed Classes are, in a sense, an extension of enum classes : the set of values for an enum type is also restricted, but each enum constant exists only as a single instance, whereas a subclass of a sealed class can have multiple instances. instances that can contain state.
I present these examples so you can see the difference, as for your question, when is it advisable to use them?
Actually the Sealed Classes (Sealed Classes) could be said to be an "Enum on steroids".
Enum :
Sealed Class : A class with a specified number of subclasses