0
0
Kotlinprogramming~10 mins

Enum with properties and methods in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Enum with properties and methods
Define Enum Class
Add Properties to Enum
Add Methods to Enum
Create Enum Instances
Access Properties and Call Methods
Use Enum in Code
This flow shows how to define an enum with properties and methods, create instances, and use them.
Execution Sample
Kotlin
enum class Color(val rgb: Int) {
    RED(0xFF0000) {
        override fun printInfo() = println("Red color with RGB: $rgb")
    },
    GREEN(0x00FF00) {
        override fun printInfo() = println("Green color with RGB: $rgb")
    },
    BLUE(0x0000FF) {
        override fun printInfo() = println("Blue color with RGB: $rgb")
    };

    abstract fun printInfo()
}

fun main() {
    val c = Color.RED
    c.printInfo()
}
Defines an enum Color with a property rgb and a method printInfo, then calls printInfo on RED.
Execution Table
StepActionEnum InstanceProperty rgbMethod CalledOutput
1Enum Color is defined with RED, GREEN, BLUE----
2Main function starts----
3Assign c = Color.REDRED0xFF0000--
4Call c.printInfo()RED0xFF0000printInfo()Red color with RGB: 16711680
5Program ends----
💡 Program ends after calling printInfo on Color.RED
Variable Tracker
VariableStartAfter Step 3After Step 4Final
cnullColor.RED (rgb=16711680)Color.RED (rgb=16711680)Color.RED (rgb=16711680)
Key Moments - 2 Insights
Why do we need to override printInfo() for each enum instance?
Each enum instance can have its own behavior. Overriding printInfo() lets each color print a custom message, as shown in execution_table step 4.
What does the property rgb represent and how is it used?
rgb holds the color code as an integer. It is passed when creating each enum instance and used inside printInfo() to show the color value (see step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of c.rgb after step 3?
A0xFF0000
B0x0000FF
C0x00FF00
Dnull
💡 Hint
Check the 'Property rgb' column at step 3 in the execution_table.
At which step is the method printInfo() called?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Method Called' column in the execution_table.
If we add a new enum instance YELLOW with rgb 0xFFFF00, what changes in the execution_table?
AA new row for YELLOW creation and method call would appear
BNo changes, because YELLOW is not used in main
CThe output at step 4 changes to Yellow color
DThe program will not compile
💡 Hint
Check that main only uses Color.RED, so new enum instances don't affect existing steps.
Concept Snapshot
enum class Name(val property: Type) {
  INSTANCE1(value) {
    override fun method() { ... }
  },
  INSTANCE2(value) { ... };

  abstract fun method()
}

- Enums can have properties and methods.
- Each instance can override methods.
- Use enum instances like objects with behavior.
Full Transcript
This example shows how to define an enum class in Kotlin with properties and methods. The enum Color has a property rgb representing the color code. Each enum instance (RED, GREEN, BLUE) overrides the abstract method printInfo() to print a message including its rgb value. In main, we create a variable c assigned to Color.RED and call c.printInfo(), which outputs the message for RED. The execution table traces these steps, showing variable values and method calls. Key moments clarify why overriding methods per instance is useful and how properties are used. The quiz tests understanding of property values, method calls, and adding new enum instances. The snapshot summarizes the syntax and behavior of enums with properties and methods in Kotlin.