Questions tagged [sealed-class]
142 questions
66
votes
3 answers
sealed class vs enum when using associated type
I'd like to create a color object based on an Int. I can achieve the same result using sealed class and enum and was wondering if one is better than the other.
Using sealed class:
sealed class SealedColor(val value: Int) {
class Red :…

Benjamin
- 7,055
- 6
- 40
- 60
63
votes
1 answer
Extending data class from a sealed class in Kotlin
I have a set of data classes that share some common fields, So ideally I'd like to declare those in a supertype (Message in this example), and be able to write functions that operate on the supertype if they need access to these common fields…

f2prateek
- 2,034
- 2
- 20
- 26
54
votes
7 answers
What are sealed classes in Java 17?
Today, I updated my Java version from 16 to 17, and I found that sealed classes is a new feature in it. I think it can be declared like this:
public sealed class Main permits AClass, AnotherClass {
}
But, what is the use of sealed classes in…
user16411474
53
votes
2 answers
What is the difference between sealed class vs sealed interface in kotlin
With Kotlin 1.5 was introduce the sealed interface. Even that I know the difference between classes and interfaces, I'm not clear what are the best practices and beneficies of using sealed interface over sealed class
Should I always use interface…

Canato
- 3,598
- 5
- 33
- 57
53
votes
4 answers
What is the point of extending a sealed class with a non-sealed class?
I don't really understand why there is a non-sealed keyword in JEP 360/Java 15.
For me, an extension of a sealed class should only be final or a sealed class itself.
Providing the "non-sealed" keyword will invite the developer for hacks.
Why are we…

Ayox
- 691
- 5
- 15
29
votes
1 answer
Accessing Kotlin Sealed Class from Java
Up until now I have been using this Kotlin sealed class:
sealed class ScanAction {
class Continue: ScanAction()
class Stop: ScanAction()
... /* There's more but that's not super important */
}
Which has been working great in both my Kotlin…

ssawchenko
- 1,198
- 1
- 13
- 24
21
votes
7 answers
What is the point of a “sealed interface” in Java?
Sealed classes and sealed interfaces were a preview feature in Java 15, with a second preview in Java 16, and now proposed delivery in Java 17.
They have provided classic examples like Shape -> Circle, Rectangle, etc.
I understand sealed classes:…

Player_Neo
- 1,413
- 2
- 19
- 28
13
votes
3 answers
What is the difference between a final and a non-sealed class in Java 15's sealed-classes feature?
I have the following sealed interface (Java 15):
public sealed interface Animal permits Cat, Duck {
String makeSound();
}
This interface is implemented by 2 classes:
public final class Cat implements Animal {
@Override
public String…

Michael Kemmerzell
- 4,802
- 4
- 27
- 43
11
votes
2 answers
Why the compiler suggest convert sealed sub-class to object?
I don't understand why the compiler suggests me to convert a sealed class with the subclasses, to objects, let see an example:
sealed class CallState
class SendReceive : CallState()
class SendOnly:CallState()
to this
sealed class CallState
object…

dicarlomagnus
- 576
- 4
- 17
11
votes
1 answer
How can this code throw a NoWhenBranchMatchedException?
In our most recent app release we see a handful kotlin.NoWhenBranchMatchedExceptions reported to Fabric/Crashlytics.
This is the code snippet in question:
private lateinit var welcome: Welcome
// ...
welcome.welcomeStateLoginStatus.let {
val…

david.mihola
- 12,062
- 8
- 49
- 73
10
votes
1 answer
Sealed classes for classes in different packages
If I declare following sealed hierarchy
package a;
import b.B;
public sealed interface A permits B {
}
package b;
import a.A;
public record B() implements A {
}
without using modules (no module-info.java)
and try to compile it with Maven I…

Lovro Pandžić
- 5,920
- 4
- 43
- 51
10
votes
2 answers
How do i serialize a generic sealed class with kotlinx.serialization
Not sure if it is possible yet but for the life of me I cannot figure out how to serialize this.
sealed class ServiceResult {
data class Success(val data: T) : ServiceResult()
data class Error(val exception:…

Chris Legge
- 739
- 2
- 9
- 24
9
votes
1 answer
How to fix "Non exhaustive 'when' statements on sealed class/interface" in Kotlin
Non exhaustive when statements on sealed class/interface will be prohibited in Kotlin 1.7.
I have a sealed class State and it's children:
sealed class State {
object Initializing : State()
object Connecting : State()
object Disconnecting…

Sergio
- 27,326
- 8
- 128
- 149
8
votes
3 answers
How to pass data class as Parcelable in a bundle?
I have a sealed class like so:
sealed class SealedClass {
object Object1 : SealedClass()
object Object2 : SealedClass()
object Object3 : SealedClass()
data class DataClass(val sealedClass: SealedClass, val anotherDataType:…

waseefakhtar
- 1,373
- 2
- 24
- 47
7
votes
1 answer
R8 Compatibility With Sealed Class In Android Studio Flamingo
When I want to release my app get this error: com.android.tools.r8.internal.jb: Sealed classes are not supported as program classes when generating class files
Android Studio: Flamingo 2022.2.1 (also i try 2022.3.1 Giraffe)
Gradle Version: 8.0…

Ghasem
- 467
- 5
- 8