Enums help group related values together. Adding properties and methods makes each value more useful and descriptive.
Enum with properties and methods in Kotlin
enum class EnumName(val propertyName: PropertyType) { VALUE1(propertyValue1) { override fun methodName() { // code for VALUE1 } }, VALUE2(propertyValue2) { override fun methodName() { // code for VALUE2 } }; abstract fun methodName() }
Each enum value can have its own property values and method implementations.
The abstract method must be implemented by all enum values.
enum class Direction(val degrees: Int) { NORTH(0) { override fun description() = "Upwards" }, EAST(90) { override fun description() = "Rightwards" }, SOUTH(180) { override fun description() = "Downwards" }, WEST(270) { override fun description() = "Leftwards" }; abstract fun description(): String }
enum class Light(val color: String) { RED("Red") { override fun action() = "Stop" }, YELLOW("Yellow") { override fun action() = "Get Ready" }, GREEN("Green") { override fun action() = "Go" }; abstract fun action(): String }
This program defines planets with mass and radius. Each planet has its own surface gravity method. The main function prints details for each planet.
enum class Planet(val mass: Double, val radius: Double) { EARTH(5.972e24, 6371.0) { override fun surfaceGravity() = 9.807 }, MARS(0.64171e24, 3389.5) { override fun surfaceGravity() = 3.721 }; abstract fun surfaceGravity(): Double } fun main() { for (planet in Planet.values()) { println("${planet.name} has mass ${planet.mass} kg and surface gravity ${planet.surfaceGravity()} m/s²") } }
Remember to put a semicolon (;) before defining methods if enum values have bodies.
Properties can be used like normal class properties.
Methods can be abstract or concrete inside the enum class.
Enums group fixed related values with extra data and behavior.
Each enum value can have its own properties and methods.
This helps write clear and organized code for fixed sets of options.
Practice
enum with properties and methods in Kotlin?Solution
Step 1: Understand enum basics
Enums group fixed related values, like days or colors.Step 2: Recognize added properties and methods
Adding properties and methods lets each enum value hold extra data and behavior.Final Answer:
To group related fixed values with additional data and behavior -> Option AQuick Check:
Enums = fixed values + extra data [OK]
- Thinking enums are for unrelated variables
- Confusing enums with regular classes
- Assuming enums can't have methods
color of type String in Kotlin?Solution
Step 1: Check enum class declaration with property
In Kotlin, enum classes can have constructor parameters likeval color: String.Step 2: Verify enum values initialization
Each enum value must pass the property value in parentheses, e.g.,APPLE("Red").Final Answer:
enum class Fruit(val color: String) { APPLE("Red"), BANANA("Yellow") } -> Option CQuick Check:
Enum with constructor and values = enum class Fruit(val color: String) { APPLE("Red"), BANANA("Yellow") } [OK]
- Using colon syntax instead of constructor parameters
- Trying to assign properties inside enum body incorrectly
- Omitting parentheses for enum values
enum class Size(val label: String) {
SMALL("S"), MEDIUM("M"), LARGE("L");
fun description() = "Size is $label"
}
fun main() {
println(Size.MEDIUM.description())
}Solution
Step 1: Understand enum property and method
Each enum value has alabelproperty: MEDIUM has "M".Step 2: Evaluate the
The method returns "Size is $label", so for MEDIUM it returns "Size is M".description()method callFinal Answer:
Size is M -> Option AQuick Check:
Method uses label property = "Size is M" [OK]
- Printing enum name instead of property
- Confusing property label with enum constant name
- Ignoring method call output
enum class Direction(val degrees: Int) {
NORTH(0),
EAST(90),
SOUTH(180),
WEST
}Solution
Step 1: Check enum constructor parameters
The enum class requires anIntparameterdegreesfor each constant.Step 2: Verify each enum constant initialization
All constants exceptWESTprovide a value;WESTis missing(value).Final Answer:
Missing property value for WEST enum constant -> Option DQuick Check:
All enum constants must pass constructor values [OK]
- Forgetting to pass constructor arguments for all constants
- Assuming semicolon is mandatory after constants
- Thinking enum constants must be lowercase
enum class TrafficLight(val duration: Int) {
RED(30) {
override fun signal() = "Stop"
},
YELLOW(5) {
override fun signal() = "Caution"
},
GREEN(25) {
override fun signal() = "Go"
};
abstract fun signal(): String
}
fun main() {
val lights = TrafficLight.values()
val signals = lights.map { it.signal() }
println(signals)
}What is the output when running
main()?Solution
Step 1: Understand enum with abstract method
Each enum constant overridessignal()to return a specific string.Step 2: Map enum values to their signals
Themapcallssignal()on each constant, producing ["Stop", "Caution", "Go"].Final Answer:
["Stop", "Caution", "Go"] -> Option BQuick Check:
Abstract method overridden per constant = correct signals [OK]
- Printing enum names instead of method results
- Confusing property values with method outputs
- Ignoring abstract method override requirement
Ready to prove it?
Pass the test and this pattern moves unknown
