Skip to main content
InterviewHubPRO
Section Overview
Q48

Question 48 of 50

Part 3: K2 Compiler, KMP, Advanced Metaprogramming & Optimization (25 - 35)

96%
Kotlin & Coroutines (50 Qs)•Part 3: K2 Compiler, KMP, Advanced Metaprogramming & Optimization (25 - 35)

Sealed Classes and Sealed Interfaces: Algebraic Data Types and pattern matching.

senior LevelCode ExamplePerformanceTypeScript

In Simple Words: Restricts inheritance to subclasses declared within the same package and compilation module:

kotlin
sealed interface NetworkState<out T> {
  data object Loading : NetworkState<Nothing>
  data class Success<T>(val data: T) : NetworkState<T>
  data class Error(val exception: Throwable) : NetworkState<Nothing>
}
  • Exhaustive when: The compiler verifies that all cases are covered. No else branch is needed; if a new subclass is added, the compiler flags all unhandled when statements as errors.