1 00:00:00,240 --> 00:00:00,930 We'll come back. 2 00:00:01,200 --> 00:00:07,920 And this video, I would like to show you the functionality and called late in it and then also how 3 00:00:07,920 --> 00:00:09,150 setters and getters work. 4 00:00:09,600 --> 00:00:16,290 So let's create a variable called owner and it's going to be of type string. 5 00:00:16,940 --> 00:00:23,490 If you create such a variable, as you can see, you always need to initialize it and on alternative 6 00:00:23,490 --> 00:00:25,470 hereby is to use late in it. 7 00:00:26,100 --> 00:00:31,740 What do you say with this is that you will initialize it later on so you don't want to initialize it 8 00:00:31,740 --> 00:00:33,990 in here, but you will initialize it later. 9 00:00:34,530 --> 00:00:36,660 For example, in the init function. 10 00:00:37,290 --> 00:00:41,430 But there is something that you have to be very careful about when you do something like that. 11 00:00:41,820 --> 00:00:45,570 Because if I know, create an object of type car. 12 00:00:45,780 --> 00:00:53,400 So here bar my car is going to be of type car and now I want to access the owner. 13 00:00:55,960 --> 00:00:58,630 I'll get an error to be more precise. 14 00:00:58,690 --> 00:01:05,170 You can see I'm getting a initialise property access exception. 15 00:01:05,710 --> 00:01:11,050 So that's because of this late in a property owner has not been initialized. 16 00:01:11,620 --> 00:01:16,720 And it is, of course, because we use late in it and we're trying to access something that is empty, 17 00:01:17,050 --> 00:01:19,330 but it's not infallible, so we're not catching it. 18 00:01:19,750 --> 00:01:22,960 So what we will need to do is to use it in it. 19 00:01:24,220 --> 00:01:30,640 So the initialization of our car and in here we can set the owner through, for example, Frank. 20 00:01:31,720 --> 00:01:38,050 And now, to be more precise, you can even say this dot owner because we're talking about this property 21 00:01:38,050 --> 00:01:38,380 here. 22 00:01:38,980 --> 00:01:42,520 So if we run this again, then we will see there is no error. 23 00:01:42,730 --> 00:01:45,850 So it doesn't display anything because we don't have any print statements. 24 00:01:45,850 --> 00:01:49,140 But as you can see, it doesn't create an error anymore. 25 00:01:49,150 --> 00:01:50,320 So this is fine. 26 00:01:50,320 --> 00:01:56,440 This works only be careful that you don't use the variable before you have initialized it when you make 27 00:01:56,440 --> 00:01:57,400 it late in it. 28 00:01:58,740 --> 00:02:04,590 All right, the next thing I would like to show you is, let's say we have a brand for a car, so it's 29 00:02:04,590 --> 00:02:13,410 going to be a vault my brand and it's going to be of type string and let's say I assign BMW to it. 30 00:02:14,200 --> 00:02:20,190 Okay, now I'm going to create a custom getter, so I'm going to use get. 31 00:02:21,290 --> 00:02:28,700 And then I created Get Her Here, which will just return field dot to lower case. 32 00:02:30,650 --> 00:02:37,610 So what that will do is whenever somebody is trying to access my brand, so this variable here, what 33 00:02:37,790 --> 00:02:41,240 they will get is the lowercase version of BMW. 34 00:02:42,260 --> 00:02:43,340 So let's have a look at this. 35 00:02:43,820 --> 00:02:45,260 Let's actually use it here. 36 00:02:47,510 --> 00:02:57,200 So here, not the owner, but I want to have my brand, and that's in this case printed front line brand 37 00:02:57,200 --> 00:02:57,710 is. 38 00:02:58,250 --> 00:03:07,550 And then my car that brand now could see brand is BMW and BMW is written in a lowercase. 39 00:03:09,220 --> 00:03:10,450 Now, what did we do here? 40 00:03:11,050 --> 00:03:16,640 Well, what we created is a custom getter by default. 41 00:03:16,960 --> 00:03:20,350 Let's say we have a verbal called max speed. 42 00:03:21,070 --> 00:03:23,790 So this time it's not the ball, it's a VAR bar. 43 00:03:24,030 --> 00:03:32,230 VAR max speed is going to be of Typekit, and I'm going to say it's going to be 250 kilometres per hour. 44 00:03:32,500 --> 00:03:39,150 And by default, what happens is that Scotland creates the following code for us. 45 00:03:39,160 --> 00:03:44,920 So it says value field equals value. 46 00:03:45,850 --> 00:03:50,320 OK, so this is the default that is generated in the background. 47 00:03:50,320 --> 00:03:55,450 So when we create this variable here, this code here is automatically generated. 48 00:03:55,450 --> 00:04:03,040 We don't need to write it ourselves so we can just get rid of it because as you saw when we wanted to 49 00:04:03,040 --> 00:04:07,330 use something or as we can see here, so let me print a line. 50 00:04:07,540 --> 00:04:13,870 Max speed is and then we use my car max speed. 51 00:04:15,460 --> 00:04:17,709 As you can see, we can access it. 52 00:04:18,040 --> 00:04:20,589 So we are getting the value. 53 00:04:20,950 --> 00:04:24,070 We could even go ahead and set the value to 200. 54 00:04:26,200 --> 00:04:27,790 But we would need to do that before. 55 00:04:28,120 --> 00:04:31,150 So let me do that real quick. 56 00:04:32,020 --> 00:04:38,860 My car thud like speed is going to be 200, so now we set it. 57 00:04:39,010 --> 00:04:44,140 This is where we set the value and in this line we get to value. 58 00:04:44,200 --> 00:04:51,970 So we are retrieving it so that we are make speed is 200, even though it was 250 here by default. 59 00:04:53,170 --> 00:04:58,810 So bringing this back this jet and set what you can do is you can customize it. 60 00:04:59,110 --> 00:05:02,860 And we did that with this getter here for our BMW. 61 00:05:03,430 --> 00:05:05,080 So we did it here. 62 00:05:06,220 --> 00:05:07,750 We create this custom getter. 63 00:05:09,810 --> 00:05:13,200 And we can do the same thing with the senators. 64 00:05:13,710 --> 00:05:20,160 So if anyone tries to set our maximum speed, we can check if the maximum speed is at least positive, 65 00:05:20,670 --> 00:05:23,610 so we could even run an error here. 66 00:05:24,150 --> 00:05:31,620 So we can say field, if value is greater zero, then use value. 67 00:05:33,150 --> 00:05:38,250 Otherwise, throw an illegal argument exception. 68 00:05:39,090 --> 00:05:46,530 And we're just going to say maximum speed cannot be less than zero. 69 00:05:47,910 --> 00:05:48,300 All right. 70 00:05:50,020 --> 00:05:54,280 So what we have here is an if and else statement just in one line. 71 00:05:54,730 --> 00:05:56,890 But of course, you could do that in multiple lines. 72 00:05:56,890 --> 00:06:04,330 But basically what we're saying here is that if someone tries to set the maximum speed and they tried 73 00:06:04,330 --> 00:06:11,950 to set it to a lower value than zero, then throw an illegal argument exception, which is just going 74 00:06:11,950 --> 00:06:14,410 to say max speed cannot be less than zero. 75 00:06:15,760 --> 00:06:16,200 OK. 76 00:06:16,240 --> 00:06:18,220 As you can see here, this get field. 77 00:06:18,730 --> 00:06:24,360 This is not a custom field, so it's just this redundant getter because we're just using the default 78 00:06:24,370 --> 00:06:24,970 getter here. 79 00:06:25,300 --> 00:06:27,520 So of course, we could get rid of this whole line here. 80 00:06:28,540 --> 00:06:28,870 All right. 81 00:06:28,870 --> 00:06:29,590 So let's try it. 82 00:06:29,920 --> 00:06:35,860 Let's set the max speed to minus five just to get this error. 83 00:06:36,430 --> 00:06:37,300 So I'm running it. 84 00:06:37,300 --> 00:06:40,570 And as you can see, I'm getting an illegal argument exception. 85 00:06:40,990 --> 00:06:43,120 Max speed cannot be less than zero. 86 00:06:43,330 --> 00:06:48,370 So it's basically the text that I defined here for our illegal argument exception. 87 00:06:50,450 --> 00:06:55,160 What happens if we said the positive value here, let's say two hundred forty? 88 00:06:56,570 --> 00:06:58,760 Then we see the max speed is 240. 89 00:06:59,510 --> 00:06:59,810 All right. 90 00:06:59,810 --> 00:07:03,430 So this is what you need to know about setters and getters. 91 00:07:03,440 --> 00:07:10,220 There's one more thing that I would like to show you, though, and that is, for example, if you have 92 00:07:10,220 --> 00:07:10,820 a model. 93 00:07:10,820 --> 00:07:19,460 So I'm going to create another variable here for my model that's going to be of type string and I'm 94 00:07:19,460 --> 00:07:21,620 going to assign them five to it. 95 00:07:22,820 --> 00:07:23,300 OK. 96 00:07:23,420 --> 00:07:24,970 So of course, this would be redundant. 97 00:07:24,980 --> 00:07:27,020 We don't need that, but I'm just going to keep it there. 98 00:07:27,350 --> 00:07:31,040 So what I can do here is I can make the setter private. 99 00:07:31,700 --> 00:07:35,450 So we have seen this private keyboard before, but we have not used it. 100 00:07:35,840 --> 00:07:36,950 What does private do? 101 00:07:37,490 --> 00:07:45,560 Well, private says that the something is only available within the same class. 102 00:07:45,950 --> 00:07:49,820 OK, so in this case, it will only be available within the car. 103 00:07:50,210 --> 00:07:55,730 So if I try to access my model and try to set it somewhere else, it's not going to work. 104 00:07:56,300 --> 00:07:57,230 So let's just try it. 105 00:07:57,500 --> 00:08:07,790 Let's go up there in our main function and go to my car and then try to set my model to something like 106 00:08:07,910 --> 00:08:08,570 I'm three. 107 00:08:09,770 --> 00:08:13,310 So as you can see, we're getting an error here, Kenneth assigned to my model. 108 00:08:13,520 --> 00:08:15,680 The setter is private in car. 109 00:08:16,220 --> 00:08:18,020 To get her, however, is not private. 110 00:08:18,020 --> 00:08:31,190 So if we want to print the car model, print and model is and then we use my car model. 111 00:08:33,710 --> 00:08:38,150 And here you can see I don't get an error here because the getter is not private. 112 00:08:38,390 --> 00:08:39,799 Only the setter is private. 113 00:08:40,340 --> 00:08:46,370 So what I did here is I said, OK, you can only change the value of my model within the same class. 114 00:08:46,550 --> 00:08:48,350 So, for example, in our in it. 115 00:08:48,530 --> 00:08:54,890 So here I could say this my model is going to be and I'm three instead. 116 00:08:55,280 --> 00:08:56,240 So this will work. 117 00:08:56,600 --> 00:09:03,010 And if I run this, I will be able to get my model and it will now be an M3 instead of one. 118 00:09:03,230 --> 00:09:03,860 And I'm five. 119 00:09:05,550 --> 00:09:08,010 So there we are, modellers and free. 120 00:09:08,460 --> 00:09:14,730 All right, one little thing that you might have questions around is what this field and this value 121 00:09:14,730 --> 00:09:15,000 are. 122 00:09:15,930 --> 00:09:21,540 Well, field is a backing field, which helps you to refer to properties inside the getter and setter 123 00:09:21,540 --> 00:09:21,930 methods. 124 00:09:22,290 --> 00:09:27,780 This is required because if you used a property directly inside together or setter, then you run into 125 00:09:27,780 --> 00:09:31,260 a recursive call, which will generate a stack overflow. 126 00:09:31,680 --> 00:09:36,780 So this is just a syntax that you need to use when it comes to fields and value. 127 00:09:37,110 --> 00:09:42,990 Value is the value that is given to the variable when you set it. 128 00:09:43,140 --> 00:09:47,100 So here for max speed, for example, this 240 is the value. 129 00:09:47,100 --> 00:09:52,020 So whatever is after the equals sign is going to be the value that is set. 130 00:09:52,530 --> 00:10:00,000 So you can do something like double check what the value is that is given to you and then you can adjust 131 00:10:00,000 --> 00:10:00,570 accordingly. 132 00:10:01,410 --> 00:10:07,860 And then whatever is the result of this will be then set back to the field, which means just back to 133 00:10:08,160 --> 00:10:09,630 the variable itself. 134 00:10:09,900 --> 00:10:10,350 All right. 135 00:10:10,860 --> 00:10:14,400 So that's setters and getters and also late in it. 136 00:10:14,940 --> 00:10:19,100 And in the next video, we're going to see a little more about object oriented programming. 137 00:10:19,110 --> 00:10:19,890 So see you there.