Member-only story

You may not need a sealed class

Patxi Bocos
2 min readDec 12, 2019

--

When it comes to decide between using an enum or a sealed class in Kotlin, I’ve seen several situations where developers where using them indistinctly.

Generally speaking, sealed classes are more powerful than enums. This is because a sealed class is just an abstract class which requires to define every child class in the same Kotlin source file.

Enum vs sealed class

So what are the main similarities and differences between them?

  • Both can define abstract members. In the case of the enum, each of the values will be required to implement them. For sealed classes, this is also true, unless a child class of the sealed class is another sealed class.
  • Only sealed classes can define deep hierarchies. This is by the nature of classes where inheritance is present.

--

--

Patxi Bocos
Patxi Bocos

Written by Patxi Bocos

Impure developer and functional programming enthusiast

No responses yet