![](/kotlin-enum/cover_image.png)
Enumerations (or enum for short) is a named list of defined constants that you can use in your program to store an arbitrary type with certain values.
In Kotlin, you can create enum constants by creating a special enum class
that can have properties, implement interfaces, and create anonymous classes.
To create an enum in Kotlin, you need to define an enum class
as shown below:
enum class CarType {
TESLA,
FORD,
HONDA
}
And that’s all you need to do to create a basic enum. You can assign one of the enum values to a variable as follows:
val myCar = CarType.TESLA
Kotlin enum with properties
Next, you can also add properties for each enum value by adding a constructor to the enum class
that defines the properties you want the values to have.
The following example adds the color: String
property for the enum values:
enum class CarType(val color: String) {
TESLA("Black"),
FORD("Blue"),
HONDA("Grey")
}
println("My car color is ${CarType.FORD.color}")
// My car color is Blue
You can add as many properties to your enum values as you need.
Kotin enum as anonymous classes.
You can also create anonymous classes from the enum values.
To do so, you need to transform each enum into a class with its own methods.
You also need to provide the base method (either an abstract
or an open
function) for the class methods.
Take a look at the example below and notice how the start()
method is added to the enum class
body:
enum class CarType {
TESLA {
override fun start() {
println("Tesla ready to race..")
}
},
FORD {
override fun start() {
println("Ford revving the engine..")
}
},
HONDA;
open fun start() {
print("The car is starting..")
}
}
CarType.HONDA.start() // The car is starting..
CarType.FORD.start() // Ford revving the engine
The base method open fun start()
is defined inside the CarType
body, while each enum values are free to override
the method in their own anonymous class body.
Kotlin enum implementing interfaces
Finally, an enum class
can also implement an interface
like a normal class.
Each enum value you have would then be an anonymous class that implements the interface methods.
For example, let’s create a CarYear
interface with a getManufactureYear()
method that returns an Int
value:
interface CarYear {
fun getManufactureYear(): Int
}
Next, implement the CarYear
interface to the CarType
class.
You will then need to provide the override fun
for the getManufactureYear()
method in each enum value:
enum class CarType: CarYear {
TESLA {
override fun getManufactureYear() = 2020
},
FORD {
override fun getManufactureYear() = 2019
},
HONDA {
override fun getManufactureYear() = 2018
};
}
Alternatively, you can also provide a default implementation of the interface method as shown below:
enum class CarType: CarYear {
TESLA {
override fun getManufactureYear() = 2020
},
FORD {
override fun getManufactureYear() = 2019
},
HONDA;
override fun getManufactureYear() = 2018
}
CarType.HONDA.getManufactureYear() // 2018
And that’s how you can implement an interface
into an enum class
type in Kotlin.
To summarize, enums in Kotlin are a bit more sophisticated than enums in traditional languages.
Enums in Kotlin is defined inside a special enum class
. You can add properties, create anonymous classes, and implement interfaces.