Skip to content

sealed interface

Devrath edited this page Jun 17, 2024 · 1 revision

In Kotlin, an interface and a sealed interface serve different purposes and have distinct characteristics. Here’s an explanation of the differences between them:

Interface

  1. Definition:

    • An interface in Kotlin is a contract that classes can implement. It can contain abstract methods (which must be implemented by the implementing classes), default methods (with implementations), and properties.
  2. Purpose:

    • Interfaces are used to define a common protocol that classes can follow, allowing for polymorphism and code reusability.
  3. Characteristics:

    • They can be implemented by any class from anywhere in the codebase.
    • They do not enforce any restrictions on which classes can implement them.
  4. Example:

    interface Drawable {
        fun draw()
        val color: String
    }
    
    class Circle : Drawable {
        override fun draw() {
            println("Drawing a circle")
        }
        override val color: String = "Red"
    }
    
    class Square : Drawable {
        override fun draw() {
            println("Drawing a square")
        }
        override val color: String = "Blue"
    }

Sealed Interface

  1. Definition:

    • A sealed interface is a special type of interface in Kotlin that restricts which classes can implement it. All classes that implement a sealed interface must be defined in the same file as the sealed interface itself, or in the same module (if using Kotlin 1.5 or later).
  2. Purpose:

    • Sealed interfaces are used to represent restricted or hierarchical class structures, where you have a fixed set of subclasses. This is useful for modeling closed sets of possible types, enhancing type safety and exhaustiveness checking.
  3. Characteristics:

    • They ensure a controlled and limited set of implementations.
    • They allow for exhaustive when statements, making it easier to manage state and ensure all cases are handled.
  4. Example:

    sealed interface Shape {
        fun area(): Double
    }
    
    class Circle(val radius: Double) : Shape {
        override fun area(): Double = Math.PI * radius * radius
    }
    
    class Square(val side: Double) : Shape {
        override fun area(): Double = side * side
    }
    
    class Rectangle(val length: Double, val width: Double) : Shape {
        override fun area(): Double = length * width
    }

Key Differences

  1. Implementation Scope:

    • Interface: Can be implemented by any class anywhere.
    • Sealed Interface: Can only be implemented by classes in the same file or module (depending on the Kotlin version).
  2. Use Case:

    • Interface: Used for defining generic contracts for classes.
    • Sealed Interface: Used for defining a restricted set of implementations, providing more control and safety over type hierarchies.
  3. Exhaustiveness:

    • Interface: Does not guarantee that all implementations are known, so exhaustive checks (e.g., in when statements) are not enforced by the compiler.
    • Sealed Interface: Ensures that all possible implementations are known, allowing the compiler to enforce exhaustive checks.

In summary, while both interfaces and sealed interfaces define contracts for classes, sealed interfaces provide additional constraints on the implementations, making them suitable for scenarios where a fixed set of related types is required.

Clone this wiki locally