Kotlin Enum Class

Hamza Oban
Orion Innovation techClub
4 min readJul 27, 2023

What are Enums and how to use them with Kotlin.

An enum class is used to create types with a limited set of possible values.

We can create enum classes with this syntax.

The syntax

We can use this enum class we created as follows.

Usage

In the real world, for example, the game levels— BEGINNER, EASY, MEDIUM, HARD and IMPOSSIBLE — could be represented by an enum class

enum class Level {
BEGINNER,EASY,MEDIUM,HARD,IMPOSSIBLE
}

Each enum constant is an object. Enum constants are separated by commas.

Enum class can take parameters. It is mandatory to give this parameter to every enum constant when it takes a parameter.

For example:

enum class Country(val capital: String) {
Turkey("Ankara"),
Germany("Berlin"),
England("London")
}

Objects of Enum classes cannot be created.

Implementing interfaces in enum classes

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

Let’s create our interface

interface Attribute{
fun numberOfDoors(): Int
fun color(): String
fun brand(): String
fun maxSpeed(): Int
}

Let’s implement our interface in our car enum class.

enum class Car : Attribute {
FordFocus {
override fun numberOfDoors(): Int = 4

override fun color(): String = "Gray"

override fun brand(): String = "Ford"

override fun maxSpeed(): Int = 240

},
JeepWrangler {
override fun numberOfDoors(): Int = 4

override fun color(): String = "Black"

override fun brand(): String = "Jeep"

override fun maxSpeed(): Int = 280

},
NissanMicra {
override fun numberOfDoors(): Int = 4

override fun color(): String = "Red"

override fun brand(): String = "Nissan"

override fun maxSpeed(): Int = 200
};
}

Let’s look at the results in the main func.

fun main() {
Car.FordFocus.let {
println("${it.brand()} ${it.color()} ${it.numberOfDoors()} ${it.maxSpeed()}")
}
Car.JeepWrangler.let {
println("${it.brand()} ${it.color()} ${it.numberOfDoors()} ${it.maxSpeed()}")
}
Car.NissanMicra.let {
println("${it.brand()} ${it.color()} ${it.numberOfDoors()} ${it.maxSpeed()}")
}

}

Implementing abstract values and functions in enum classes

All enum constants must override these abstract structures.

enum class Car {
FordFocus{
override fun numberOfDoors(): Int = 4

override fun color(): String = "Gray"

override fun brand(): String = "Ford"

override fun maxSpeed(): Int = 240

},
JeepWrangler {
override fun numberOfDoors(): Int = 4

override fun color(): String = "Black"

override fun brand(): String = "Jeep"

override fun maxSpeed(): Int = 280

},
NissanMicra{
override fun numberOfDoors(): Int = 4

override fun color(): String = "Red"

override fun brand(): String = "Nissan"

override fun maxSpeed(): Int = 200
};
abstract fun numberOfDoors(): Int
abstract fun color(): String
abstract fun brand(): String
abstract fun maxSpeed(): Int
}

Additionally, open functions can be written inside enum classes.

enum class Car2 {
FordFocus{
override fun numberOfDoors(): Int = 4

override fun color(): String = "Gray"

override fun brand(): String = "Ford"

override fun maxSpeed(): Int = 240

},
JeepWrangler {
override fun numberOfDoors(): Int = 4

override fun color(): String = "Black"

override fun brand(): String = "Jeep"

override fun maxSpeed(): Int = 280

},
NissanMicra{
override fun numberOfDoors(): Int = 4

override fun color(): String = "Red"

override fun brand(): String = "Nissan"

override fun maxSpeed(): Int = 200

override fun modify() {
super.modify()
println("Change color")
}
};
abstract fun numberOfDoors(): Int
abstract fun color(): String
abstract fun brand(): String
abstract fun maxSpeed(): Int

open fun modify(){
println("Modify")
}
}

Overriding these explicit functions in enum constants is entirely optional.

Conclusion

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

As a result, Enum classes provide us convenience by allowing the same datasets to be grouped according to their intended use. Thus, we can easily see the available options with the data sets we have grouped. Enum classes are often used because they make our job so much easier. That’s why I took care to mention all the properties of enum classes. I hope it was a useful article for you! See you in my other articles!

Don’t forget to clap 👏

--

--