> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin Sealed Class

A sealed class in Kotlin is an implicitly abstract class that restricts its inheritance hierarchy to a strictly defined, bounded set of subclasses. By defining a class as `sealed`, you guarantee to the compiler that all direct subclasses are known at compile time, preventing any external or arbitrary extension of the class hierarchy.

```kotlin theme={"dark"}
sealed class NetworkResponse {
    data class Success(val payload: String) : NetworkResponse()
    data class Failure(val code: Int, val exception: Throwable) : NetworkResponse()
    object InProgress : NetworkResponse()
}
```

## Architectural Rules and Constraints

* **Implicitly Abstract:** A sealed class cannot be instantiated directly. It behaves as an `abstract` class and can contain abstract members, properties, and implemented methods.
* **Constructor Visibility:** The constructors of a sealed class are implicitly `protected`. You can explicitly declare them as `private`, but they can never be `public`.
* **Inheritance Scope (Kotlin 1.5+):** All direct subclasses of a sealed class must be declared within the **same package** and the **same compilation module** as the sealed class itself. They cannot be extended by unknown classes in client code or third-party libraries.
* **Subclass Flexibility:** Direct subclasses can be standard classes, `data class` declarations, singleton `object` declarations, or other `sealed` classes. Because a sealed class is a class, its subclasses cannot be interfaces.
* **Indirect Inheritance:** The restriction applies only to *direct* subclasses. If a direct subclass is not marked as `sealed` or `final` (classes are `final` by default in Kotlin unless marked `open`), it can be extended by other classes anywhere in the codebase.

## Exhaustive Type Checking

The primary mechanical advantage of a sealed class is its integration with Kotlin's `when` expression. Because the compiler possesses a complete registry of all direct subclasses, it performs exhaustive type checking.

If a `when` expression evaluates an instance of a sealed class and covers all possible subclass types, the compiler does not require an `else` branch.

```kotlin theme={"dark"}
fun evaluateResponse(response: NetworkResponse): String {
    return when (response) {
        is NetworkResponse.Success -> "Data: ${response.payload}"
        is NetworkResponse.Failure -> "Error: ${response.code}"
        NetworkResponse.InProgress -> "Loading..."
        // The compiler recognizes this is exhaustive. 
        // Adding an 'else' branch here is redundant and discouraged.
    }
}
```

If a new subclass is later added to the `NetworkResponse` sealed class, the compiler will immediately flag the `when` expression as an error until the new type is explicitly handled, ensuring structural safety across the codebase.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
