Sealed Classes VS. Enum Classes VS. Sealed Interfaces - When to Use Which?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to a new video in this video I will show you the differences between sealed classes enumclasses and sealed interfaces because all of these are so similar but also different in some way you will learn when you should actually use which in your Android projects so I want an empty Android Studio project and I first of all want to talk about what these types of clouds are actually used for these are used when you want to kind of group different related options together in a class so let me give you an example and I will create a class here called HTTP error as you will probably know we have different types of HTTP error messages we can get from an API for example 404 not found we have 401 that you're unauthorized we have 403 Forbidden and there are a bunch more like internal server there are tons of error messages and instead of just having the error code which is just a pure integer we could kind of group these errors together to have them in a more readable way and that's what we can use here at classes for here we have a sealed class HTTP error and a CA class is again just an abstract class where all the children like all the classes that inherit from this class are known at compile time that means there are no other modules that can extend this class so let's say you would get such a seal class from a library which is another module then you wouldn't be able to actually extend this class and actually write your own class that inherits from it that only works in that specific module where it's declared however on the other side if you have an abstract class you could do that if that would come from a library that you write your own version of that abstract class so let's see how that works we have our C-Class HTTP hour here and we could now Define different subclasses of that HTTP error class that kind of describe our different error messages we want to distinguish so for example we have an object unauthorized and that inherits from HTTP error or we might have an object not found which is an HTTP error and the cool thing about this is that all these subclasses of HTTP error are known as compile time that means if we now have such an HTTP or in our code let's say that is HTTP error and we set it to http error Dot not found and we then want to check what kind of error that is let's say you get that from another class then you could use a one expression and since all of these subclasses are known at compile time we have an exhaust of when block here so we can press Alt Enter here we can say add remaining branches and it adds all of these branches and our when block doesn't complain that there is no else option something we can also do with sealed classes is and that we can give them a Constructor just like with abstract classes we could say okay we actually want to attach an error code here because each HTTP error actually has a code that represent this error and now we of course need to pass this error message this error code for each single error class or object we actually have here so unauthorized would be for one and not found would be 404 and then here we could simply also just access the code using error.code that is probably nothing new to you let's now see how that works with enumclasses let's write an equivalent enumclass because enum in the end also just enumerates that's why it's called enum it enumerates a set of options um that kind of relate to the same thing or that are just different options for the same thing in our case our HTTP over here so let's go here and say enumclass HTTP error let's just call it enum and we can also give this a Constructor like valve code as an integer and we can open the class body and now the way we actually declare these child classes here is different from HTTP error so these are actually not child classes instead these are just kind of constants you can think of and we simply yeah put them in here separated by commas so we can say unauthorized pass in our 401 code then we put a comma and then we have not found with full four and if we then go how to main activity and actually just copy this block of code paste it here have our error enum is an HTTP or enum then we could also set this to not found HTTP or enum not found put this here and here we actually don't say HTTP error like this we say HTTP error in I'm not found and here HTTP error enum unauthorized and then we basically have the equivalent code here you can see the when block is also exhaustive so we don't need to specify an else option let me quickly get rid of this to do statements so we get rid of this error message like this so with enumclasses we also have an exhaustive window so till now everything's actually exactly the same when using a Seer class and when using an enumclass so what the heck is actually the difference actually we can also put in functions in both of these so function two something which you could call we could also make this abstract if we would want that and then of course both of our objects would need to implement that and we can also do this here with enums we would actually need to finish this up with a semicolon and then save function do something so that's also the same what's now the difference well the difference is that with enum classes we really deal with constants while with HTTP with C classes in this case with our HTTP error class we deal with individual instances so when we create such an HTTP error instance of like an unauthorized in this case it would always be the same since we use a Singleton here but we could also use data classes here on Armored classes so we could have a data class unauthorized and we could give this the reason for example so why are we unauthorized there could be multiple reasons maybe we didn't attach a token maybe the token is invalid or so so there could be two unauthorized errors and both could have a different reason so therefore there could also be two different instances of this specific data class and of this specific HTTP error however with enum classes we are not able to do that there is no no way to actually make um a child here of this enum class have its individual Behavior its individual kind of variables and parameters you actually pass to it no you actually need to declare a variable or you can declare a variable that you pass to all the children of that enum class like the code which all HTTP errors have in common but if some HTTP errors are actually different than others then you can do that with enum classes so in the end a seed class just offers you more customization possibilities if you actually need these so does that now mean you should always use sealed classes and never use Indian classes because c-classes are just like enum classes but offer more customization no that doesn't mean that I would always ask myself when you need to distinguish between different values is it fine if these are actually constants so if you only have variables in the Constructor and if none of your child classes like these single errors here needs individual behavior and form of functions and variables like individual Behavior would be having such a reason for unauthorized but not needing a reason for not found because when it's not found the reason is pretty clear if you don't need that I would use enum classes because let me show you something here um enumclass actually offers some things here classes don't offer and that is since all these values are known at compile time and are constant we can also go ahead and say HTTP error enum.values and that gives us an array of all possible values which we could simply Loop over for example so if we would like to print all possible error GP errors that we have we could simply use this enumclass say.values and say like for each and yeah just print each value and that would work however since with sealed classes each single insta each single child could be its own instance or actually is its own instance that doesn't work because I mean what should it print if every unauthorized error can have its own reason so if you would actually want to print all HTTP errors that you have in the seed class you would actually need to create your own list where you put in all these different types of classes and if you have a data class you would need to say okay that's actually the reason but if you only have objects then you would still need to create your very own list and then Loop over that so in that case I would really prefer having an enumclass if you're fine with every single child being a constant so now we compare two classes to enum classes but the title also says sealed interfaces so what the heck is that is that even another thing that I need to worry about no actually not sealed interfaces are super simple um a lot simpler than the kotlin documentation actually tells you or shows you um what is a sealed interface well in the end if you need to distinguish between options and you don't need a variable in the Constructor like here let's say we don't have this code we don't have it here we don't have it here then what you can do is you can simply make this a sealed interface as well and you simply get rid of calling the Constructor here because interfaces don't have a Constructor and that way you just save a little bit of code it's just a different way of writing this so I hope that clarified a little bit when you should actually use which if so please let me definitely know that down below and also let me know what you want me to make videos about in future maybe some kotlin features maybe something about Android just let me know that down below and if it gets some upvotes then I will definitely consider making a video about that topic thanks for watching have an amazing rest of the week and see you back in the next video bye bye
Info
Channel: Philipp Lackner
Views: 48,319
Rating: undefined out of 5
Keywords: android, tutorial, philip, philipp, filipp, filip, fillip, fillipp, phillipp, phillip, lackener, leckener, leckner, lackner, kotlin, mobile, sealed, enum, interface, class, which, what, better, when, use, android app, state, constant, instance
Id: kLJRZpRhX1o
Channel Id: undefined
Length: 10min 14sec (614 seconds)
Published: Wed Jul 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.