Kotlin Enum Tutorial with Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video we will learn about enums in kotlin so whenever we want to represent a group of constants we can use enums and defining enum is very easy we can simply write enum class and let's say we have a enum class and we can name it anything let's say color or basically whatever you want so this is our enum class color and here we can define constants so for example let's say we have red we have green and we have blue now all these constants that we have inside this color enum are objects and we can get all the values using this color enum and we can simply do it like this we can write color dot values now this values function will give us an array of type color that means we will get all the values available inside enum color so we can simply loop like this you can see we are getting all the enums we can also pass or use enums with our when statement so we can write when let's say we have an enum val color equals to color dot let's say red and we can write here when color and here we can simply generate all the remaining branches like this so we have all the cases here red green and blue let's just write a print ellen statement you can see we are getting red because the color value is red so for these cases we can use enums you can also pass a constructor with your enum for example here i can pass val color name that is string and val color value that is end now when i pass these two values to the enum color constructor we have to define the constructors for all the enums or all the constants defined inside the enum so here we need to define the name and the value let's say for red we have value 10 so you can have these extra values with your enums and you can create enum constants like this now if you want to access an enum what you can do is you can simply write i will comment these things so i will write here println color dot red dot color name and the same way i can access the color value like this you can see we are getting red and 10. you can also implement or inherit interfaces with your enums for example i have an interface that is interface do color and inside this interface i have a function fun do color like this now i can inherit this interface like this we have do color now everything here inside this color enum is an object so we can simply define it like this and here we can write do color colored with red or you can write whatever you want the same way we can define the do color function for all other constants so here we have colored with green and colored with blue like this so here you can simply call color dot let's say green dot do color you can see it is working absolutely fine and one more thing that i would like to tell you is you cannot inherit abstract classes or open classes to your enum if you will try to do this you will get an error let's say we have an abstract class a and if i try to inherit a then i will get an error as you can see here enum class cannot inherit from classes so you can only use interfaces with your enums and enums are used to define constants a set of constants and the compiler knows at the compile time the all possible values and it is handy to generate all possible values with your when statement so that is all for this video friends i hope you found this video helpful and learned something thanks for watching
Info
Channel: Simplified Coding
Views: 3,101
Rating: 5 out of 5
Keywords: kotlin enum, enum class kotlin, kotlin enum tutorial, kotlin programming tutorial, kotlin object oriented programming, kotlin tutorial for beginners, kotlin tutorial, android kotlin tutorial
Id: -1C7Tm6eblA
Channel Id: undefined
Length: 5min 24sec (324 seconds)
Published: Sun Jan 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.