It is possible to have sealed interfaces?

Hi, as a developer I think that will be helpful to have in the language the concept of sealed interfaces.
It is something that you think can be added in the future?


Two years ago, @yole wrote the following on a reddit post about the same topic:

Kotlin does not support sealed interfaces because there is no way to prevent Java classes from implementing the same interface. Therefore, it’s not possible to obtain the exhaustiveness guarantee that sealed classes provide.


Thank you for the answer, so I guess that it could be possible in a Kotlin-only environment like Kotlin native for example, or am I wrong?

There were some discussions about it in some places. The sealed interfaces will be probably added to the language (either with safety feature like name mangling or with opt-in key which will allow to bypass some Java-safety) in the future, but it is not a first priority.


I don’t know what the actual problem is. Scala had sealed traits (inferfaces) for a long time already. Why not to take the similar approach.

Interoperability with Java isn’t a concern in Scala.


It seems like we could prevent Java classes from implementing the same interface… or is something wrong with this translation?


sealed interface Expr
object NotANumber : Expr()


public class Enclosing {
    private enum Private {}
    public interface Expr {
        void unimplementable(Private p);
    public static class NotANumber implements Expr {
        void unimplementable(Private p) {}

Basic idea: Use an auxillary private type to create a method on the interface that would-be implementors outside the enclosing class cannot implement, because they don’t have access to the parameter type.

You can also make this method uncallable by adding an overload that takes a different private type (prevents calls with null, by making them ambiguous).

1 Like

Java does not have type aliases, and you can’t alias Expr to Enclosing.Expr. So this still doesn’t solve the interopability issue.

That’s a cosmetic interoperability issue though. It’s not an issue that could potentially break the code like the other mentioned issues.

1 Like

Be patient, java 15 will integrate sealed classes & interfaces.

As it was announced on Kotlin 1.4 Online Event, Kotlin will support sealed interfaces. Moreover, sealed interfaces will be supported even when JVM target version is less than 15, and even when the platform is not JVM.
Here is the KEEP proposal with technical details. You’re welcome to review it and leave your comments.


Here’s an issue for sealed interfaces: KT-20423

Roman Elizarov: There’s no reason to limit [sealed interfaces] to Java 15.