Created
December 17, 2021 14:48
-
-
Save magdamiu/088be7d285e1fe2e84806829c2ad81d7 to your computer and use it in GitHub Desktop.
Sealed class in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class Fruit(val name: String) { | |
class Apple : Fruit("Apple") | |
class Mango : Fruit("Mango") | |
} | |
class Pomegranate : Fruit("Pomegranate") | |
fun display(fruit: Fruit) { | |
when (fruit) { | |
is Fruit.Apple -> println("${fruit.name} is good for iron") | |
is Fruit.Mango -> println("${fruit.name} is delicious") | |
is Pomegranate -> println("${fruit.name} contains vit d") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment