Skip to content

Instantly share code, notes, and snippets.

@magdamiu
Created December 17, 2021 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magdamiu/088be7d285e1fe2e84806829c2ad81d7 to your computer and use it in GitHub Desktop.
Save magdamiu/088be7d285e1fe2e84806829c2ad81d7 to your computer and use it in GitHub Desktop.
Sealed class in Kotlin
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