https://kotlinlang.org logo
#announcements
Title
# announcements
n

nkiesel

03/15/2017, 6:52 PM
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

voddan

03/15/2017, 6:53 PM
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

louiscad

03/15/2017, 7:12 PM
A sealed class is open only in it's file, while class alone can't have any subclasses, even private ones
c

cedric

03/15/2017, 7:25 PM
Also, sealed classes are a lot less common than regular classes in my experience
n

nkiesel

03/16/2017, 4:41 AM
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".
2 Views