Issue
With Kotlin 1.5 was introduce the sealed interface. Even that I know the difference between classes and interfaces, I'm not clear what are the best practices and beneficies of using sealed interface over sealed class
Should I always use interface now even when is a simple case? Or will be a case by case?
Thanks
Obs: Didn't found similar questions, only about sealed classes
Solution
A major reason to choose to use a sealed class instead of interface would be if there is common property/function that you don't want to be public or open. All members of an interface are always public and open.
Regarding public, you can work around it on an interface using extension functions/properties, but only if it doesn't involve storing state non-publicly.
Regarding open, even though implementations are restricted to your module, you might have a complex hierarchy and appreciate the ability to prevent yourself from making a mistake overriding something you shouldn't.
Otherwise, sealed interfaces are more flexible because they allow a subtype to be a subclass of something else, an enum class, or a subtype of multiple sealed interface/class hierarchies.
Answered By - Tenfour04
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.