👨🏼‍💻Kotlin Enum Class

Everything you need to know about Enum Classes!

Hüseyin Özkoç
Huawei Developers
3 min readJan 23, 2023

--

Kotlin Enum Class

Introduction

Hello Dear Kotlin lovers! Welcome to my new article. Today I’m going to explain Enum classes. First of all, I want to talk about why Enum classes are used and what they do.

Enum Class:

Enum classes allow the same datasets to be grouped. Thus, it allows us to easily see what the options are in these data sets that we have grouped. Likewise, Enum classes provide us with type safety. Let’s understand this better with an example.

more easily understand what the values related to the Car class

As we saw in the simple example above, we created a dataset related to Car and created this class to consist of 3 constants. In this way, we can now more easily understand what the values related to the car class are because it cannot take any other value other than these 3 types.

If we took a look at our simple example, I would like to talk about the features of Enum classes for better understanding.

1-Enum classes cannot be abstact, open, inner, sealed.

cannot be abstact, open, inner, sealed.

2-Enum classes are final. Therefore, using the final keyword gives a redudant warning.

Enum classes are final.

3-Enum classes can use a constructor and hold data in that constructor because they are essentially a class.

Can use a constructor and hold data in that constructor because they are essentially a class.

4-When we convert our Kotlin code to its Java equivalent with "show kotlin bytecode", we will see that in the background every enum constant is actually kept as static final classes. This way we don't have to create the object of enum constants when using it. Likewise, we see that each enum constant inherits its own class in the background.

In this example, we see that the days of the week are written in order.
When we look at the Java equivalent of our Kotlin code; each enum constant is kept in the background as a static final class and inherits its own class. For more detailed information; https://www.youtube.com/watch?v=iaKHAe9DKvk&list=PL5qUTObM_k-SThUEvdlwagNTD3zieAY7t&index=21

5-Objects of Enum classes cannot be created.

Enum classes cannot be created.

6- Every Enum constant has name and ordinal values by default. The value of Name gives the String of the Enum constant, the Ordinal gives the index of the enum constant and starts from 0.

7-Enum classes cannot inherit any class. But they can implement any interface.

Enum classes cannot inherit any class.
But they can implement any interface.

Likewise, we can override interface methods and values inside our class, except for enum constants. So we don’t need to override repeatedly inside each enum constant.

we can override interface methods and values inside our class, except for enum constants.

8-Enum classes can take abstract values and functions. Also, all enum constants must override these abstract structures. Additionally, open functions can be written inside enum classes. Overriding these open functions on enum constants is entirely optional.

Since enum constants are static classes that inherit our enum class in the background, that’s why it is necessity that we override abstract methods and values.

9-Even though enum constants appear as a static class in the background, they cannot implement an interface due to their special nature.

Conclusion

As a result, Enum classes allow grouping of same datasets for their intended use. Thus, we can easily see the available options with the data sets that we have grouped. Enum classes are used frequently because they make our work very easy in some scenarios. That’s why I took care to mention all the properties of enum classes in my article. I hope it was a useful article for you! See you in my other articles!

References

--

--