Explore Kotlin Sealed Classes

What are sealed classes and Why do we need them?

Satya Pavan Kantamani
Programming Geeks

--

Problem

To handle the states of an API request we might have used enum classes. Enum in Java is a data type that contains a fixed set of constants. It’s a special data type that enables a variable to be a set full of predefined constants. Its variable must be equal to one of the values that have been predefined for it.

Here we don't have any other details related to failure it just specifies whether the request is failed or successful. But if we want to know more about the state of failure whether it’s a remote failure, network failure, etc we don’t have a proper solution here.

Solution

So for this kind of problem, to handle the states along with types defined we can use sealed…

--

--

Satya Pavan Kantamani
Programming Geeks

Android Dev, Interested in Traveling, App development. Based in Hyderabad, India. Catch me at https://about.me/satyapavankumar