In Kotlin, sealed classes and sealed interfaces are powerful tools for representing restricted class hierarchies and They improve code readability and safety. The main purpose of sealed classes and interfaces is to handle API responses, manage UI update states and also we can define different types of functionality in our code. In this tutorial, we learn about Kotlin sealed classes & interfaces, their integration guide, and their benefits with easy examples.
Kotlin Sealed Classes & Interfaces Explained with Examples
Sealed classes in Kotlin:
A sealed class is a special class in Kotlin that allows us to define a restricted hierarchy of sub-classes. In a sealed class, you can define multiple types of subclasses, each representing a different type of data. Sealed classes are useful when you want to define a type with only a limited set of specific implementations and a sealed class cannot be instantiated outside the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
sealed class NetworkResponse { data class Success(val data: String) : NetworkResponse() data class Error(val errorMessage: String) : NetworkResponse() object Loading : NetworkResponse() } fun handleResponse(response: NetworkResponse) { when (response) { is NetworkResponse.Success -> println("Data: ${response.data}") is NetworkResponse.Error -> println("Error: ${response.errorMessage}") NetworkResponse.Loading -> println("Loading...") } } fun main() { // Creating different types of responses val loadingResponse = NetworkResponse.Loading val successResponse = NetworkResponse.Success("This is the response data") val errorResponse = NetworkResponse.Error("Something went wrong!") // Handling each response type handleResponse(loadingResponse) // Output: Loading... handleResponse(successResponse) // Output: Data: This is the response data handleResponse(errorResponse) // Output: Error: Something went wrong! } // Output Loading... Data: This is the response data Error: Something went wrong! // Output |
Sealed interfaces in Kotlin:
Sealed interfaces are similar to sealed classes. However, sealed interfaces allow more flexibility by allowing you to use interfaces as a base type in a restricted hierarchy. It was introduced in the Kotlin 1.5 version. The sealed interface restricts interfaces only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
sealed interface UserAction data class Login(val username: String) : UserAction data class Logout(val reason: String) : UserAction data class Signup(val email: String, val password: String) : UserAction fun handleUserAction(action: UserAction) { when (action) { is Login -> println("User logged in as ${action.username}") is Logout -> println("User logged out: ${action.reason}") is Signup -> println("User signed up with email: ${action.email}") } } fun main() { // Creating different user actions val loginAction = Login("Alice") val logoutAction = Logout("User requested logout") // Handling each action handleUserAction(loginAction) // Output: User logged in as Alice handleUserAction(logoutAction) // Output: User logged out: User requested logout } // Output Start // User logged in as Alice // User logged out: User requested logout // User signed up with email: [email protected] // Output End |
Happy coding.