Kotlin Data Class Tutorial with Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi everyone in this video we will learn about data  classes data classes is another cool feature of   Kotlin that can be used when we need to create a  class just for storing data in this video. I will   take example of java so these kind of classes that  we create in java just for storing data are called   pojo classes that is plain old java object and for  the sake of example I have already created a pojo   class in java so this class is called person and  I have to store three values for this person that   are Id name and country and just for storing these  three values I have to create a constructor and   getters for all these three values and sometimes  I also need to override the Tostring function   the equals function and the hash code function  so just for these three simple variables I have   created a class for more than 51 lines and these  all are just boilerplate code that I need to write   for every pojo class so these codes are useless  and every time I add a new variable in my pojo   class I need to generate all these codes again  because ide can generate all these boilerplate   codes but still it is not a good idea to have  this much amount of code inside a simple class   and Kotlin fix this problem using the data classes  so let's create the same class person and Kotlin   so what I will do is I will create a data  class this time and to create a data class   you need to use the data keyword and then  the class keyword and then your class name   so I will create a class name person and  here we need to have three values so we   will define three values as val Id string val  name string and val country it is also a string   and that's it this class is equivalent to this  class so in java you have to write 50 lines   but in Kotlin it is basically just one line I can  put it in one line because we just have the class   and the constructor so this is Kotlin and this  is java I cannot create two classes with the   same name and that is why I am getting an  error here so just for now I will comment   all the code inside this person. Java so we have a data class person   and when you are creating a data class  you cannot make it open or abstract   so when you are using a data keyword you  cannot use open abstract or inner keywords   with data classes and in data class  you cannot have a blank constructor   so if i try to create the constructor blank I will  get an error you can see we are getting an error   so you have to provide at least one value  inside the constructor you can have class   body and you can override the equals hash  code and tostring function if required   but in most of the cases it is not needed because  the basic thing is already done with data class so   if i create an instance of this data class let's  say we have a person so we have a person and if   I write println person I will get all the values  because the tostring function is already created   and you do not need to override it as you can see  we are getting all the values in a string format. So this was the two string function and you can  also compare two persons let's say person one   and let's say this time I will change the  name to anyone else let's say some name   and if I will print person equals to  equals to person 1 I will get false. You can see we have false but  if i will put the name Belal   then I will get true because the equals function  is also created by default you can see we are   getting true you can override the equals function  and the two string function if required but it's   not needed because the basic thing is already done  now one more thing with data class is you can have   body of your data class if required for example  you have let's say val F name and l name Then if required you can create a function   like this and this function can return F name  and l name like this so you can simply print person dot get name so you can have the  body for your data class if required  and this time we need to pass first  name and last name so let's pass it and let's run the code so you can see we are getting true and the full  name and one more thing with data class is you can   inherit another class to your data class you can  implement interface you can inherit another class   but you cannot inherit another data class to  your data class because as I said you cannot   use open keyword or abstract keyword with data  keyword so these classes are final and you cannot   extend these data classes so  this was one thing and another   function that Kotlin generates for you is the  component and function with all your data class so   this is another useful feature i will tell you  how first let's understand what is component   and function so with this instance that is the  person instance what I can do is I can print   dot component one so it will generate the  component and function for all your properties   so we have one two three four so it will generate  component one component 2 component 3 and   component 4 and that's why it is called component  n function where n is the number of properties   so it will start from 1 and go till the number n  and 1 means id 2 means F name 3 means last name   and 4 means country let's see if it is working  you can see we are getting all the values now   why it is useful so with the help of component  and function you can destructure the declaration   and what it means is you can simply get all these  values in vals so you can assign all these values   inside a val directly in a single line and  you can do it like this you will create val   id fname l name and country and equals to let's  say person so this is called destructuring   declaration and now you can  print let's say print ln Id   I will comment out these things because it will  print them again so let's see if it is working so you can see we are getting all the values  so this is how you can create data classes   and it is way better than java requires less  line of codes and it have all the basic things   by default so that is all for this video  friends i hope you found this video helpful   and learned something in case you have any  problem or question you can leave your comments   below so thanks for watching everyone  this is BELAL KHAN now signing off.
Info
Channel: Simplified Coding
Views: 3,788
Rating: 5 out of 5
Keywords: kotlin data class, kotlin data class tutorial, data class in kotlin, kotlin tutorial, android kotlin tutorial, kotlin object oriented programming, kotlin for beginners, kotlin android development
Id: L0VulZZPGbI
Channel Id: undefined
Length: 9min 16sec (556 seconds)
Published: Sat Jan 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.