Kotlin Sealed Class Tutorial with Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi everyone in this video we will  learn about sealed classes in Kotlin   so whenever we want to define a limited set  of classes we can use sealed classes the most   common example is when we fetch some data from a  network so a network call may lead to a success   or a failure so for these kind of situations  we can use sealed classes and defining a sealed   class is very easy it is like an abstract class  so sealed class cannot be instantiated directly   but we will instantiate the other classes that  are extending or inheriting our sealed class   let's see with an example so here I will create a  sealed class let's say I have a sealed class data   now I will represent the data in two formats  either success or failure and we may have one   more case that is loading so what I can do is  I can create other classes to represent this   state for example one state is success so  I have a data class success and this class   will give us the data for example the data is  a string i need to write a val here because   inside data class I cannot have values without  val or var now to this class success I can inherit   the sealed class data now you can  only inherit this sealed class data   inside the classes that are available inside  this main file only so even if the class is   outside the sealed class let's say i have  data class error and error is also a string   so I can inherit the sealed class data here  because both these classes are in the same file   but if I try to extend or inherit this data  sealed class into an other file it will not   work and it will give us a compilation error  and for now I will put this data class inside my sealed data class so inside my sealed data class  I have two data classes one is success and another   one is failure or error and both these classes are  inheriting the sealed class data that means the   success and this error is data now we can define  one more thing here if required let's say we have   an object loading now here the compiler knows all  the possible conditions if you use this data class   with a when statement that means the compiler  knows all the possible conditions that are success   error and loading and that is why you do not need  an else branch because all the possible cases   are covered if you will use this thing with the  when statement so this is our data class and we   have three options inside success error and  loading and I will show you how you can use   it so let's create a function that will give  us some data so I have a function that will   return the data and here I will return data dot  success and I will just generate a random data so   what I can do is I can generate a random number  let's say I have a random number between 100 2000. And I have random dot to string like this  so this function will give us a success data   now we can use it here let's say i have data  equals to get data now I can write this data   inside my event statement and here I can check if  data is data dot success if it is data dot error   and if it is loading like this now an else  case is not required here because we have   covered all the possible situations and here  we are getting an error because I forget to   inherit the data class in my object so now  here we have covered all the possible cases   and that is why an else branch is not  required now here I can simply print   the data so I will write println data now  if I run this code then I will see some   output as you can see we are getting success and  the value is 816. that is a random value now we   can also pass error here and in this case it will  come inside error block and we will see error data   as you can see here and the same way it  may return loading if the state is loading   so that is all for the sealed class friends and  it is used to define a limited set of values   so if you have any question or confusion you can  leave your comments below and thanks for watching.
Info
Channel: Simplified Coding
Views: 7,509
Rating: 5 out of 5
Keywords: kotlin sealed class, sealed class kotlin, kotlin tutorial, kotlin programming tutorial, kotlin language tutorial, kotlin android tutorial, kotlin object oriented programming tutorial
Id: Gk2Z5zthdkw
Channel Id: undefined
Length: 5min 49sec (349 seconds)
Published: Tue Jan 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.