wen I tried to explain the difference between `cla...
# announcements
n
wen I tried to explain the difference between
class
,
sealed class
and
open class
to a co-worker he asked: why is
sealed
not the default, given that developer has control on what goes into the same file anyway (i.e. why is
class
not the same as
sealed class
)? I did not have a good answer. can anyone help me out?
v
Because in many cases this is not you who extends your classes, but the users of your api.
Besides, making the default more like Java where possible is a good thing
l
A sealed class is open only in it's file, while class alone can't have any subclasses, even private ones
c
Also, sealed classes are a lot less common than regular classes in my experience
n
thanks for the answers. The argument that won me over was: "sealed classes are implicitly abstract". I of course do not want my classes be abstract by default, thus I see the necessity for another keyword to distinguish "cannot be instantiated but can be extended" from "cannot be instantiated but can be extended outside of the file where the class is defined".