I have a question for Sealed classes vs Sealed int...
# general-advice
r
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
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
That boils down to contructor parameter difference only as a real world use case
k
An interface doesn't have any constructor at all, let alone constructor parameters.
c
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
Understood. basically sealed here just add restriction to heirarchies to regular ones and thats all
c
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
Ok understood