1 00:00:00,420 --> 00:00:01,080 Welcome back. 2 00:00:01,320 --> 00:00:06,150 And this video, I would like to talk about a specific type of a class, which is a data class. 3 00:00:06,480 --> 00:00:08,490 So let's create a data class real quick. 4 00:00:08,790 --> 00:00:13,350 In order to create a data class, you can just go ahead and use the keyword data. 5 00:00:13,770 --> 00:00:20,790 And then you'll see that the class so it's inside of the class here, Cortland basics, that's at least 6 00:00:20,790 --> 00:00:25,950 how it's going to be called directly, but I want to call this one user. 7 00:00:26,400 --> 00:00:34,290 And in here, I have to at least add one parameter and I'm going to add, for example, the ID, which 8 00:00:34,290 --> 00:00:41,220 is going to be a long ID and a name which is going to be a string. 9 00:00:42,860 --> 00:00:46,380 Now I can use to start a class in order to create objects of it. 10 00:00:46,400 --> 00:00:48,920 So let's do that real quick here, user. 11 00:00:49,310 --> 00:00:55,520 And it's going to be a user one or the idea is going to be one and the name is going to be Dennis. 12 00:00:55,910 --> 00:00:57,830 So that's already a user object. 13 00:00:57,830 --> 00:01:01,880 Of course, we can call this one user one in order to be more precise. 14 00:01:02,120 --> 00:01:05,209 So that's how we create an object of a data class. 15 00:01:05,450 --> 00:01:08,750 Now, when creating the other classes, there are things that you need to consider. 16 00:01:08,960 --> 00:01:14,540 So the primary constructor must have at least one parameter so you can see if I get rid of that. 17 00:01:14,840 --> 00:01:18,770 Then we get an error here because we don't have any parameter in there. 18 00:01:18,950 --> 00:01:21,260 But as I said, one is the minimum. 19 00:01:21,590 --> 00:01:25,970 Then you need to assign them either as VAR or volume. 20 00:01:26,660 --> 00:01:32,660 So both the swine and other classes cannot be abstract, open, sealed or in their classes. 21 00:01:33,140 --> 00:01:34,970 So none of these things work. 22 00:01:35,180 --> 00:01:39,180 So you cannot go ahead and use open here in front of it. 23 00:01:39,200 --> 00:01:41,000 So as you can see, it's not going to accept that. 24 00:01:42,810 --> 00:01:47,910 In order to, you know, acts as a property, we can simply go ahead and use it like this, we can just 25 00:01:47,910 --> 00:01:52,170 say user one dog name, for example, and that will give us the name. 26 00:01:52,170 --> 00:01:54,620 In this case, it will give us Dennis. 27 00:01:54,630 --> 00:01:58,470 So we can, of course, check that print line name. 28 00:01:59,960 --> 00:02:00,860 So let's check it. 29 00:02:03,000 --> 00:02:04,710 And there we are it says, Dennis. 30 00:02:05,400 --> 00:02:06,900 OK, so that's how you can do it. 31 00:02:07,200 --> 00:02:15,570 Now let's say you want to assign a new value, so instead of having the I.D. one, you want to assign 32 00:02:15,780 --> 00:02:16,470 the value to. 33 00:02:17,400 --> 00:02:20,070 Well, the problem is that won't work because it's a vowel. 34 00:02:20,100 --> 00:02:20,370 Right? 35 00:02:20,380 --> 00:02:25,530 So when you hover over it, wall cannot be reassigned and you shouldn't reassign an ID. 36 00:02:25,980 --> 00:02:31,950 But maybe you want to be able to reassign the name so you want to change the name of a specific user. 37 00:02:32,310 --> 00:02:37,140 Then you can, of course, do so and replace that with, for example, Michael. 38 00:02:38,010 --> 00:02:38,340 All right. 39 00:02:38,340 --> 00:02:40,230 So that's going to be now. 40 00:02:40,240 --> 00:02:43,650 Michael User1 is not going to be Dennis anymore, but it's going to be Michael now. 41 00:02:44,370 --> 00:02:48,510 And then, of course, you can create new users any time. 42 00:02:49,560 --> 00:02:52,740 And you can also compare them, so let's say we have user to. 43 00:02:54,050 --> 00:03:00,950 So it would create a new user here of all user to is going to be a user object, which I give the idea 44 00:03:00,950 --> 00:03:07,400 of one and also the name of Michael Wright, because what I want to show you is that you can compare 45 00:03:07,400 --> 00:03:13,700 them so you can go ahead and compare user one equals. 46 00:03:14,790 --> 00:03:15,450 User two. 47 00:03:16,200 --> 00:03:20,370 All right, so because every object is of type any. 48 00:03:20,850 --> 00:03:23,280 At one point, so inherits from any. 49 00:03:23,550 --> 00:03:26,250 That's why we can use the equals function here. 50 00:03:26,670 --> 00:03:30,270 And alternatively, you can replace it with a double equals sign. 51 00:03:30,510 --> 00:03:32,280 So both versions are fine. 52 00:03:32,640 --> 00:03:34,350 And this will print. 53 00:03:34,380 --> 00:03:34,770 True. 54 00:03:34,980 --> 00:03:37,830 We can, of course, test that and see if that's actually the case. 55 00:03:39,600 --> 00:03:40,370 And there you are. 56 00:03:40,400 --> 00:03:44,310 That's true, because both have the same content, right? 57 00:03:44,310 --> 00:03:47,910 So in both cases, the user ID is one and the name is Michael. 58 00:03:48,810 --> 00:03:54,630 OK, so of course, here we can follow the instruction and just use a double equal sign, which will 59 00:03:54,630 --> 00:03:55,830 result in true as well. 60 00:03:58,840 --> 00:04:07,790 Other classes also have a two string method, so the same as other classes as well, so user details 61 00:04:08,450 --> 00:04:09,890 colon and a user. 62 00:04:10,250 --> 00:04:19,370 So just using this as going to work totally fine because our data class has a two string method underlying 63 00:04:19,370 --> 00:04:26,660 it because every class is inheriting any and one point, and that's why it's available in there as well. 64 00:04:27,730 --> 00:04:30,010 Another cool function is the copy function. 65 00:04:30,130 --> 00:04:34,430 So if you want to copy one object to another, you can do that easily. 66 00:04:34,450 --> 00:04:40,030 So let's say we have this updated user object, which will be of type user. 67 00:04:40,810 --> 00:04:48,160 And here I'm just saying, OK, I want to copy user one so you can just go ahead and do like this or 68 00:04:48,490 --> 00:04:52,720 you can copy its contents except for the variables that you want to change. 69 00:04:52,750 --> 00:04:56,620 So let's say I want to change that name to Dennis Potuto. 70 00:04:56,920 --> 00:05:03,400 So user one will still be Michael, but now our updated user will be Dennis, one user, but will also 71 00:05:03,400 --> 00:05:06,280 have the idea of one, even though it usually should be unique. 72 00:05:06,520 --> 00:05:08,770 But I think in this case, it's going to be fine. 73 00:05:09,070 --> 00:05:14,590 So let's print our updated user and let's see what we get there. 74 00:05:15,890 --> 00:05:22,670 And by the way, we should maybe also print our user one just to see the difference between the two. 75 00:05:24,840 --> 00:05:25,860 All right, so there we are. 76 00:05:26,580 --> 00:05:29,120 It says then, is it from the top there than we had through this? 77 00:05:29,130 --> 00:05:35,090 Was this comparison that we had and then user details, that's this one here where it says user I.D. 78 00:05:35,100 --> 00:05:36,600 one name Michael. 79 00:05:36,840 --> 00:05:42,600 And then we have one user, which is our user, one which has the idea of one and the name of Michael 80 00:05:42,600 --> 00:05:47,790 and the other user, which is our updated user, which has the same ID and a different name. 81 00:05:48,090 --> 00:05:51,170 Now, in this case, it might be redundant. 82 00:05:51,180 --> 00:05:57,240 It might seem stupid to do that, but not all data classes are as basic as we have it here, with just 83 00:05:57,240 --> 00:05:59,620 two variables or two parameters. 84 00:05:59,640 --> 00:06:04,470 Imagine a data class with, let's say, seven different variables or parameters here. 85 00:06:04,950 --> 00:06:09,930 Then it's already a very different story because then it might really make sense to just change one 86 00:06:09,930 --> 00:06:11,940 thing and then copy the rest. 87 00:06:12,480 --> 00:06:15,660 OK, so let's look at our updated user. 88 00:06:16,470 --> 00:06:18,600 The updated user has an ID and a name. 89 00:06:18,870 --> 00:06:24,690 Now what we can do is we can get its components so we can go ahead and. 90 00:06:25,730 --> 00:06:26,990 Good component, number one. 91 00:06:27,530 --> 00:06:31,340 So, user one dot or in this case, it was updated user. 92 00:06:32,340 --> 00:06:42,180 That component one will give us the first component, so to speak, this one will print I.D. one and 93 00:06:42,180 --> 00:06:49,260 then print line updated user component two will print. 94 00:06:51,120 --> 00:06:51,870 Then this you to. 95 00:06:55,110 --> 00:06:56,550 OK, so this is the. 96 00:06:57,500 --> 00:07:02,900 Feature that you can use, another cool thing is that we can use deconstruction, so let's look at deconstruction, 97 00:07:02,900 --> 00:07:12,560 so I have this idea and the name variable, so I have these two variables that I want to make dependent 98 00:07:12,560 --> 00:07:14,240 on our updated user. 99 00:07:15,170 --> 00:07:21,710 So what will happen now is that is going to take the ID and the name from updated user and it's going 100 00:07:21,710 --> 00:07:23,950 to store them in separate variables. 101 00:07:23,970 --> 00:07:29,990 So it's the same thing as going ahead and saying it is equal to updated user dot ID. 102 00:07:31,470 --> 00:07:36,480 And while I know your name is updated user name. 103 00:07:37,110 --> 00:07:44,250 Now, of course, in our case, it's complaining because name already existed up here, so let's get 104 00:07:44,250 --> 00:07:44,700 rid of this. 105 00:07:45,800 --> 00:07:46,460 And now. 106 00:07:47,660 --> 00:07:54,680 We can use this functionality so now we can go ahead and create variables, all of the different attributes 107 00:07:54,680 --> 00:07:56,450 that are within an object. 108 00:07:56,780 --> 00:07:59,660 And I can, of course, go ahead and print those. 109 00:07:59,660 --> 00:08:05,780 So ID is going to be ID and name is going to be. 110 00:08:06,820 --> 00:08:07,210 Name. 111 00:08:10,660 --> 00:08:11,350 All right, so. 112 00:08:12,430 --> 00:08:16,720 This will print, Heidi, is one name is going to be Dennis Produto. 113 00:08:16,930 --> 00:08:17,890 So let's test it. 114 00:08:19,500 --> 00:08:22,950 And there we are, so it says IDs, one and name is then upon you, though. 115 00:08:23,490 --> 00:08:23,820 All right. 116 00:08:23,820 --> 00:08:29,790 So this was just a little introduction into beta classes, and we're going to use data classes because 117 00:08:29,790 --> 00:08:36,480 there's just very powerful, very basic classes, which are very limited in their functionality. 118 00:08:36,480 --> 00:08:37,860 But that is exactly what we need. 119 00:08:37,860 --> 00:08:44,520 Sometimes we just don't want to have all of this overhead and all of this additional code, and we just 120 00:08:44,520 --> 00:08:50,520 want to have a clean and simple object type and therefore data classes are amazing. 121 00:08:51,330 --> 00:08:52,860 All right, so see you in the next video.