Sobes.tech
Junior — Middle

Is it possible to declare an interface as sealed in a programming language?

sobes.tech AI

Answer from AI

In Kotlin language, starting from version 1.5, it became possible to declare interfaces as sealed. This allows restricting the set of classes that can implement this interface, which is useful for type control and safety.

Example:

sealed interface Expr

class Const(val number: Double) : Expr
class Sum(val e1: Expr, val e2: Expr) : Expr

Thus, sealed interface restricts the hierarchy of implementations, similar to sealed class. In Java, such an option does not exist; sealed is applied only to classes and interfaces starting from Java 15, but with different syntax and restrictions.