https://kotlinlang.org logo
#general-advice
Title
# general-advice
r

Rohan Maity

10/10/2023, 12:37 PM
I have a question for Sealed classes vs Sealed interfaces. The official doc does not extend much what is the use case for Sealed interface. To me it appears, if you don't have constructor parameter then use interface. But I don't think thats the only use case. Please tell more if I am missing something
s

Stylianos Gakis

10/10/2023, 12:56 PM
I’d go with “Use interface, unless you can’t and you got to use abstract” The exact same principle with choosing an
abstract class
vs an
interface
1
r

Rohan Maity

10/10/2023, 12:57 PM
That boils down to contructor parameter difference only as a real world use case
k

Klitos Kyriacou

10/10/2023, 1:29 PM
An interface doesn't have any constructor at all, let alone constructor parameters.
c

CLOVIS

10/10/2023, 1:40 PM
The difference between
sealed interface
and
sealed class
is the same as between
interface
and
abstract class
. For example, a class can implement multiple
sealed interface
but only a single
sealed class
. Etc.
1
r

Rohan Maity

10/10/2023, 1:41 PM
Understood. basically sealed here just add restriction to heirarchies to regular ones and thats all
c

CLOVIS

10/10/2023, 1:43 PM
Yes,
sealed
just means that it cannot be implemented outside the current module. This allows the compiler to know for sure the entire list of implementations, so you don't have to add the
else
in a
when
. Other than that,
sealed interface
are normal interfaces, and
sealed class
are normal
abstract class
.
r

Rohan Maity

10/10/2023, 1:44 PM
Ok understood