What if your fixed categories could carry their own secrets and actions, making your code smarter instantly?
Why Enum with properties and methods in Kotlin? - Purpose & Use Cases
Imagine you have a list of fixed categories like days of the week or traffic light colors, and you want to store extra details for each, like a description or a number. Doing this manually means creating separate variables or classes for each item.
Manually managing each category with its details is slow and messy. You might repeat code, forget to update all places, or mix data and behavior, making your program hard to read and error-prone.
Using enums with properties and methods lets you group each fixed item with its own data and behavior neatly. This keeps your code clean, easy to update, and lets you treat each item as a smart object.
val red = "Red"; val redCode = "#FF0000"; fun printRed() { println("Color: $red, Code: $redCode") }
enum class Color(val code: String) { RED("#FF0000"); fun printInfo() { println("Color: $name, Code: $code") } }
You can create clear, organized sets of related constants that carry their own data and actions, making your programs smarter and easier to maintain.
Think of a traffic light system where each light (red, yellow, green) knows its color code and how long to stay on, all bundled in one place.
Enums group fixed items with their own data and functions.
This avoids repetitive code and keeps related info together.
It makes your code cleaner, safer, and easier to understand.
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
