I have a sealed class, DeliveryOption, where I override the toString method, but when I call it from Guy or Dispatcher it looks like it’s not overridden and returns the base implementation of toString.
Why is it not calling the override one?
sealed class DeliveryOption {
abstract val id: Int
abstract val name: String
override fun toString(): String = name
data class Guy(
override val id: Int,
override val name: String
) : DeliveryOption()
data class Dispatcher(
override val id: Int,
override val name: String
) : DeliveryOption()
}
To retain a toString() implementation from the base you can declare the method as final:
sealed class DeliveryOption {
abstract val id: Int
abstract val name: String
final override fun toString(): String = name
data class Guy(
override val id: Int,
override val name: String
) : DeliveryOption()
data class Dispatcher(
override val id: Int,
override val name: String
) : DeliveryOption()
}