https://kotlinlang.org logo
m

Mehdi Haghgoo

03/23/2021, 12:28 PM
What kind of inheritance is this? I see it in Compose docs, but can't find its usage on Kotlin docs. Can a subclass (object) be defined inside the parent class?
👌 1
Copy code
sealed class Screen(val route: String, @StringRes val resourceId: Int) {
    object Profile : Screen("profile", R.string.profile)
    object FriendsList : Screen("friendslist", R.string.friends_list)
}
m

Marc Knaup

03/23/2021, 1:16 PM
It’s normal inheritance. The only difference is that you can be sure that
Profile
and
FriendsList
are the only subclasses of
Screen
and there are no others. That’s because it’s
sealed
. https://kotlinlang.org/docs/sealed-classes.html
1
m

Mehdi Haghgoo

03/23/2021, 5:11 PM
I don't know why the linked doc does not have an example like the given snippet.
2 Views