What is to be done when you have a function restricted set of data as a statement? Today, you will get to know interestingly about enum Class and sealed class.
It will clear your concepts about both classes for sure, and you will be able to distinguish between both with no confusion. You will see, in an instant, how a particular class opens the door to functional ideas.
Sealed Class
It showcases a concrete group of values. Sealed classes are nothing but abstract classes with a definite number of subclasses in an identical file. The sealed modifier works as if it is impossible to define another subclass outside the file. You can get to know subclasses of a sealed class just by evaluating a single file.
Sealed class are ideal for presenting sum types. This can have a value of either True or False but not both. Therefore, sealed class subclasses can understand instance-specific Data.
For example, when you inform another section of the app, about the selected payment option, you can pass both the chosen payment category and payment specific data which is necessary for later processing.
A sealed class can manage all types of events and messages. It can determine what kind of event it is, and every event can keep data. Sealed class functions with various structures. For example, class, object, data class.
Sealed class subtypes are types, and those types can be further expanded based on usage.
class LocallyDispatched :
Dispatched() { } //OK
Enum Class
If you are looking to depict a constant set of probable functions, you should go for the enum class. Every value inside the enum is known as constant. Every constant inside enum is known as an object.
We use values() to get all the enum constants as an Array. EnumValues() and enumValuesOf
() are two generic addition function that recovers data from some particular enum.
ValueOf
() function recovered a constant rooted on the string value. For example, if your website provides a concrete set of payment choices, you can represent in the following manner:
enum class PaymentOption { CASH, CARD, TRANSFER}
It handles values that are always item-specific. Kotlin enums may comprise procedures. Their executions are always item-specific. The value of an enum is that items are specific and constant.
Henceforth, one can obtain all the items using the values() function or by type utilizing the enum Value Of function. You can also examine enum from String using valueOf(String)
or by type using enumValueOf
.
So, repeated use of enum values is straightforward. Also, their serialization and deserialization are straightforward and valuable, which is characterized by name. They are assisted by libraries for serialization. They also include ordinal and automatically executed toString, equals and hashCode.
In short, the enum class is ideal for presenting a concrete set of enduring values.
Conclusion
In the article, we have seen the type of class in Java that we can use based on different scenarios. We have seen their better usage and other insights to use them in the right place.
That is all for this article. Thanks for reading!