1 00:00:00,180 --> 00:00:00,990 ‫Welcome back. 2 00:00:01,020 --> 00:00:05,700 ‫In this video, I would like to show you properties and teach you how properties work. 3 00:00:05,700 --> 00:00:11,280 ‫And because one video would be too long and I created a video in the past about properties and it was 4 00:00:11,280 --> 00:00:12,720 ‫a little too confusing as it seemed. 5 00:00:12,720 --> 00:00:17,940 ‫I decided to break it down and rerecord it because it just wasn't good enough as it seems. 6 00:00:17,940 --> 00:00:21,870 ‫So if you find another video which is totally confusing, it doesn't help you at all. 7 00:00:21,870 --> 00:00:26,460 ‫Then please let me know because I want this course to be the best course out there. 8 00:00:26,730 --> 00:00:27,120 ‫All right. 9 00:00:27,120 --> 00:00:30,600 ‫So what we need to do is, first of all, create a new class. 10 00:00:30,600 --> 00:00:37,500 ‫So let's go to project and add a new class, because I want to teach you the fundamentals of properties 11 00:00:37,500 --> 00:00:43,230 ‫because they are an important aspect or an important concept that we're going to use a lot. 12 00:00:43,230 --> 00:00:47,190 ‫That's why it's really important that you understand the whole breadth of it. 13 00:00:47,700 --> 00:00:51,750 ‫So we start by creating a new class and I'm going to call this class box. 14 00:00:51,750 --> 00:00:52,110 ‫All right? 15 00:00:52,110 --> 00:00:54,720 ‫So just be very simple class. 16 00:00:54,720 --> 00:01:03,360 ‫So this box class is going to just be a class that has a certain amount of variables. 17 00:01:03,360 --> 00:01:03,720 ‫All right. 18 00:01:03,720 --> 00:01:09,720 ‫So, for example, just want to calculate the volume of the box based on its length, its height and 19 00:01:09,720 --> 00:01:10,380 ‫its width. 20 00:01:10,770 --> 00:01:11,100 ‫All right. 21 00:01:11,100 --> 00:01:14,670 ‫So let's just create those variables, and I'm going to make them public for now. 22 00:01:15,150 --> 00:01:15,540 ‫All right? 23 00:01:15,540 --> 00:01:23,670 ‫I'm going to make this one public length, then another, and this one will be the height, and then 24 00:01:23,670 --> 00:01:25,380 ‫we need the width. 25 00:01:25,950 --> 00:01:29,520 ‫And finally, I want to also have the volume. 26 00:01:29,520 --> 00:01:33,180 ‫So the volume will be the end result of this box. 27 00:01:33,180 --> 00:01:33,750 ‫All right. 28 00:01:33,750 --> 00:01:36,540 ‫So now I have a bunch of variables. 29 00:01:36,540 --> 00:01:39,030 ‫Now, those variables are not simple variables. 30 00:01:39,030 --> 00:01:47,310 ‫They are, in fact, member variables, which means that they are the variables on the level of the 31 00:01:47,310 --> 00:01:50,370 ‫class, in this case of the class box. 32 00:01:50,580 --> 00:01:58,230 ‫So they have a direct impact on the whole appearance of the box or the whole structure of a box, so 33 00:01:58,230 --> 00:01:58,590 ‫to say. 34 00:01:58,590 --> 00:01:59,160 ‫All right. 35 00:01:59,250 --> 00:02:05,220 ‫And we're going to see how we can use member variables in a different way or even a different structure, 36 00:02:05,220 --> 00:02:06,570 ‫which is even safer. 37 00:02:06,570 --> 00:02:09,840 ‫Because the problem here by is that they are public, so they're not safe. 38 00:02:09,840 --> 00:02:11,550 ‫We will see how we can fix this. 39 00:02:11,550 --> 00:02:15,390 ‫So first of all, let's go ahead and create a new method here. 40 00:02:15,390 --> 00:02:21,810 ‫And I'm just going to call this one display info, because the idea behind it is that it will just display 41 00:02:21,840 --> 00:02:28,830 ‫the length, the width, the height, and then it will calculate the volume and tell us how big the 42 00:02:28,830 --> 00:02:29,550 ‫volume is. 43 00:02:29,880 --> 00:02:30,240 ‫All right. 44 00:02:30,240 --> 00:02:33,690 ‫I'm just going to write a short right line statement. 45 00:02:33,690 --> 00:02:38,640 ‫So I enter CW double tab, which creates this console right line statement here for me. 46 00:02:38,730 --> 00:02:48,780 ‫And in here I'm just going to say something like length is going to be the first entry and height is 47 00:02:48,780 --> 00:02:51,510 ‫going to be the second entry. 48 00:02:51,510 --> 00:02:59,250 ‫So I use it like this and with is the third entry. 49 00:02:59,670 --> 00:03:05,580 ‫So the volume is and then I have the fourth entry. 50 00:03:06,180 --> 00:03:13,590 ‫All right, so this is my whole right line statement and then the variables that I want to use is length, 51 00:03:14,550 --> 00:03:15,420 ‫height. 52 00:03:16,690 --> 00:03:17,500 ‫With. 53 00:03:19,170 --> 00:03:20,940 ‫And volume. 54 00:03:21,810 --> 00:03:26,890 ‫Now, volume is something that I need to calculate based on length, height and width. 55 00:03:26,910 --> 00:03:28,180 ‫So I'm just going to do that. 56 00:03:28,200 --> 00:03:37,680 ‫I'm just going to say the volume is the length multiplied with the height multiplied with the width. 57 00:03:38,760 --> 00:03:46,050 ‫Doing so creates this volume or calculates the volume because the volume is the result of this calculation 58 00:03:46,050 --> 00:03:46,330 ‫here. 59 00:03:46,350 --> 00:03:49,440 ‫Length, time, side times, width, it's going to be the volume. 60 00:03:49,740 --> 00:03:50,220 ‫All right. 61 00:03:50,220 --> 00:03:54,600 ‫So now let me split it up into lines so it's a little more readable. 62 00:03:54,750 --> 00:03:55,140 ‫All right. 63 00:03:55,140 --> 00:03:57,310 ‫So now we have this method display info. 64 00:03:57,330 --> 00:04:00,870 ‫Now let's go ahead and save this file and go to our program keys. 65 00:04:00,900 --> 00:04:04,800 ‫Here we are going to use this class. 66 00:04:04,800 --> 00:04:06,780 ‫We're going to create an object of this class. 67 00:04:06,780 --> 00:04:08,270 ‫How do we create objects? 68 00:04:08,280 --> 00:04:09,000 ‫Well. 69 00:04:10,410 --> 00:04:14,520 ‫So I create a new object of it. 70 00:04:14,520 --> 00:04:17,730 ‫I'm going to call this one box and it's going to be a new box. 71 00:04:17,730 --> 00:04:22,620 ‫So this is how you create an object of a class or an instance of a class. 72 00:04:22,620 --> 00:04:25,620 ‫So I'm creating an instance of this class box. 73 00:04:25,620 --> 00:04:30,390 ‫So pretty much a variable of type box and box is what we created ourselves. 74 00:04:30,720 --> 00:04:36,000 ‫Now what I can do is I can assign the values to it so I can go ahead and say, okay, the length is 75 00:04:36,000 --> 00:04:46,260 ‫going to be three, then the height is going to be four and the width is going to be five, something 76 00:04:46,260 --> 00:04:46,680 ‫like that. 77 00:04:46,710 --> 00:04:52,110 ‫Of course, I need an equal sign here as well in order for this to be correct code. 78 00:04:52,200 --> 00:04:56,640 ‫Now, what I can do is I can just go ahead and call box dot. 79 00:04:57,940 --> 00:04:59,590 ‫Display info. 80 00:05:00,160 --> 00:05:01,300 ‫So that's pretty cool. 81 00:05:01,540 --> 00:05:03,790 ‫I don't need this bright line statement here anymore. 82 00:05:03,790 --> 00:05:06,640 ‫I can now save this file and execute it. 83 00:05:06,640 --> 00:05:12,580 ‫So let's run this file and it will display us the info in the console. 84 00:05:13,780 --> 00:05:20,590 ‫So that we are length is free and height is four and width is five, so the volume is 60. 85 00:05:21,190 --> 00:05:21,460 ‫All right. 86 00:05:21,460 --> 00:05:22,630 ‫So that worked out. 87 00:05:22,630 --> 00:05:23,200 ‫That's great. 88 00:05:23,200 --> 00:05:28,540 ‫So this display info worked out and it just displaying the details that we wanted. 89 00:05:28,660 --> 00:05:37,570 ‫Now what we could do is we could change the values or what we can change the values of the member variables 90 00:05:37,600 --> 00:05:39,370 ‫of our box class. 91 00:05:39,670 --> 00:05:41,620 ‫And we can do so because they are public. 92 00:05:41,620 --> 00:05:41,950 ‫Right? 93 00:05:41,950 --> 00:05:43,110 ‫So this one is public. 94 00:05:43,120 --> 00:05:43,810 ‫This one is public. 95 00:05:43,810 --> 00:05:44,710 ‫This one is also public. 96 00:05:44,710 --> 00:05:46,030 ‫So all of them are public. 97 00:05:46,720 --> 00:05:54,100 ‫Now, this is very insecure code because at this point, if you write member labels which are totally 98 00:05:54,100 --> 00:05:56,230 ‫public, everyone can change them at any point. 99 00:05:56,230 --> 00:06:01,630 ‫So everyone who can create an object or every class that can create an object of box, we'll be able 100 00:06:01,630 --> 00:06:06,910 ‫to change all of those values of that instance of the box. 101 00:06:06,910 --> 00:06:10,270 ‫So everyone was access to this box can change it now. 102 00:06:10,810 --> 00:06:14,770 ‫And that is something that can cause a lot of problems. 103 00:06:15,160 --> 00:06:22,420 ‫Go further down the road, right when you program and a team and stuff, then it can really become cumbersome 104 00:06:22,420 --> 00:06:23,920 ‫to do it like this. 105 00:06:23,920 --> 00:06:25,240 ‫So there are better ways. 106 00:06:25,240 --> 00:06:31,420 ‫There are cleaner ways to implement it because maybe you don't want the length to be adjustable, maybe 107 00:06:31,420 --> 00:06:33,460 ‫you want the length to have a certain value. 108 00:06:33,490 --> 00:06:34,660 ‫No one can adjust it. 109 00:06:35,410 --> 00:06:38,410 ‫Then what you could do is you could make this. 110 00:06:39,550 --> 00:06:40,720 ‫Verbal privates. 111 00:06:40,750 --> 00:06:46,120 ‫So my member variable length is going to be private and I'm just going to assign a default value of 112 00:06:46,120 --> 00:06:46,870 ‫three to it. 113 00:06:47,110 --> 00:06:52,400 ‫So now every box that I create will have the value for the length of three. 114 00:06:52,420 --> 00:06:59,880 ‫So every box will have a length of three inches or meters or whatever the metrics are that you're using. 115 00:06:59,890 --> 00:07:02,040 ‫So now I cannot assign length anymore. 116 00:07:02,050 --> 00:07:03,240 ‫So you see, I get an error here. 117 00:07:03,250 --> 00:07:06,100 ‫Box length is inaccessible due to its protection level. 118 00:07:06,100 --> 00:07:08,410 ‫So we added the protection level by making it private. 119 00:07:08,410 --> 00:07:10,270 ‫So now we cannot adjust it anymore. 120 00:07:10,420 --> 00:07:13,600 ‫So how can we still change it? 121 00:07:14,110 --> 00:07:16,450 ‫Well, we can't. 122 00:07:16,480 --> 00:07:17,740 ‫Not that we're this way. 123 00:07:17,740 --> 00:07:19,660 ‫So we need to implement something. 124 00:07:19,660 --> 00:07:25,240 ‫We need to implement an alternative way to change the length of the box. 125 00:07:26,050 --> 00:07:26,350 ‫All right. 126 00:07:26,350 --> 00:07:31,600 ‫In order to do so, what we can do is we can implement something called setters and getters. 127 00:07:31,960 --> 00:07:37,160 ‫So in this case, we cannot even access the length of the box anymore. 128 00:07:37,180 --> 00:07:41,680 ‫So let's say I want to write the length. 129 00:07:41,680 --> 00:07:53,740 ‫So I want to console right line box length is and then just going to access box dot length. 130 00:07:54,010 --> 00:07:57,660 ‫As you can see, my ID doesn't even show me the variable. 131 00:07:57,670 --> 00:08:00,820 ‫I can't access box dot length so I cannot change it. 132 00:08:01,060 --> 00:08:03,130 ‫But I can also not read it. 133 00:08:03,490 --> 00:08:03,820 ‫All right. 134 00:08:03,820 --> 00:08:06,480 ‫So I can neither get it nor set it. 135 00:08:06,490 --> 00:08:09,880 ‫So this is setting and this is getting the variable. 136 00:08:09,970 --> 00:08:16,870 ‫These are very important terms setting and getting, because now what we can do is we can create methods 137 00:08:16,870 --> 00:08:22,870 ‫in our box class which will allow us to set or get the variable length. 138 00:08:23,230 --> 00:08:24,430 ‫So let's just do that. 139 00:08:24,460 --> 00:08:26,380 ‫Let's create a. 140 00:08:27,360 --> 00:08:30,210 ‫Sutter so public. 141 00:08:32,290 --> 00:08:35,230 ‫Voit set length. 142 00:08:35,320 --> 00:08:37,830 ‫So this variable will set the length. 143 00:08:37,840 --> 00:08:39,700 ‫Now, what will it set it to? 144 00:08:39,730 --> 00:08:43,480 ‫Well, it needs a variable or a parameter here. 145 00:08:43,510 --> 00:08:45,940 ‫It needs a value here that it can set it to. 146 00:08:45,970 --> 00:08:48,280 ‫So it's just going to. 147 00:08:49,150 --> 00:08:53,560 ‫Need an integer here, and I'm just going to call this one length. 148 00:08:54,970 --> 00:08:57,490 ‫So it will set the length. 149 00:08:57,520 --> 00:09:01,780 ‫Now I will set length, equal length. 150 00:09:01,900 --> 00:09:05,110 ‫Now, this is confusing and it's also incorrect. 151 00:09:05,110 --> 00:09:06,550 ‫So I get a warning here. 152 00:09:06,550 --> 00:09:10,510 ‫Don't even get an error here, because as you can see, the colors are different, right? 153 00:09:10,510 --> 00:09:13,150 ‫So this color and that color are slightly different. 154 00:09:13,150 --> 00:09:17,500 ‫This one is this very light color and this one is a darker blue color. 155 00:09:17,620 --> 00:09:19,180 ‫So what is going on here? 156 00:09:19,180 --> 00:09:21,430 ‫Because we are using the same word here. 157 00:09:21,430 --> 00:09:22,900 ‫We're using length here. 158 00:09:22,900 --> 00:09:25,600 ‫We're using length here and here, here as well. 159 00:09:25,750 --> 00:09:31,240 ‫So this length here is actually this or should be this length here at the top. 160 00:09:31,240 --> 00:09:35,800 ‫So if I want to do it correctly, I can use this this keyword to it. 161 00:09:35,980 --> 00:09:41,590 ‫By doing so, I say, okay, I want to change the length, which is. 162 00:09:42,310 --> 00:09:43,150 ‫Parts. 163 00:09:44,100 --> 00:09:46,050 ‫Of this class called Box. 164 00:09:46,050 --> 00:09:50,460 ‫So I want to change the length of this object. 165 00:09:51,900 --> 00:09:55,830 ‫So in this case, of the object that is created of the class box. 166 00:09:56,940 --> 00:10:00,450 ‫All right, so whatever length I'm getting assigned. 167 00:10:00,630 --> 00:10:04,240 ‫So whatever length is passed when set length is called. 168 00:10:04,260 --> 00:10:06,270 ‫This method will be. 169 00:10:06,940 --> 00:10:14,320 ‫This variable here, and it will be set to the length, which is my private member variable of my box. 170 00:10:15,490 --> 00:10:22,750 ‫This is very important because what I can do now is I can now go ahead and change the length of the 171 00:10:22,750 --> 00:10:24,520 ‫box by doing so. 172 00:10:24,520 --> 00:10:26,110 ‫Set length. 173 00:10:26,680 --> 00:10:32,410 ‫I can also set the length to, for example, three and now run the code again and it will work perfectly 174 00:10:32,410 --> 00:10:32,890 ‫fine. 175 00:10:34,360 --> 00:10:34,970 ‫And let's see. 176 00:10:34,990 --> 00:10:35,260 ‫All right. 177 00:10:35,260 --> 00:10:35,700 ‫There we are. 178 00:10:35,710 --> 00:10:36,430 ‫Length is free. 179 00:10:36,460 --> 00:10:38,020 ‫Height is four and so forth. 180 00:10:38,050 --> 00:10:38,410 ‫All right. 181 00:10:38,410 --> 00:10:41,380 ‫So now let's say I don't want to have three because it was the default value. 182 00:10:41,410 --> 00:10:47,740 ‫Now, let's say I want to have the length as four, and now we can see it's the working and the volume 183 00:10:47,740 --> 00:10:48,640 ‫also has adjusted. 184 00:10:48,640 --> 00:10:51,240 ‫So it even worked for the calculation. 185 00:10:51,280 --> 00:10:52,470 ‫So that's great. 186 00:10:52,480 --> 00:10:57,790 ‫So now we can set a variable which is a member variable of a class. 187 00:10:57,790 --> 00:10:58,660 ‫So that's important. 188 00:10:58,660 --> 00:11:00,310 ‫We're talking about the different class. 189 00:11:00,910 --> 00:11:02,200 ‫It's not in the same class. 190 00:11:02,380 --> 00:11:08,830 ‫Now what we can do is we can try to access this length because it's private, right? 191 00:11:08,830 --> 00:11:10,360 ‫So how could I access it? 192 00:11:10,570 --> 00:11:14,770 ‫Because maybe I really need this variable here somewhere. 193 00:11:14,770 --> 00:11:17,580 ‫I want to access the length, but I can't do it. 194 00:11:17,590 --> 00:11:21,130 ‫So what I can implement is something called a getter. 195 00:11:21,340 --> 00:11:24,700 ‫So a get length public. 196 00:11:25,150 --> 00:11:30,190 ‫And in this case the type has to be the same type as the variable that I want to return. 197 00:11:30,190 --> 00:11:34,870 ‫And in this case, I want to return the value of this length or I want to return the length itself, 198 00:11:34,870 --> 00:11:35,260 ‫right? 199 00:11:35,260 --> 00:11:36,640 ‫So it's of type int. 200 00:11:36,640 --> 00:11:40,300 ‫So I need to return an int type for this method. 201 00:11:40,300 --> 00:11:49,360 ‫So this one will be get length and this is also the convention to use the name, get with the capital 202 00:11:49,360 --> 00:11:52,210 ‫G and then the name of the variable that you want to get. 203 00:11:52,210 --> 00:11:57,940 ‫And in this case I want to get the variable length and now you just return the length. 204 00:11:58,690 --> 00:12:04,540 ‫Now, in order to be more precise, to return this dot length, of course you could just write the length, 205 00:12:04,540 --> 00:12:09,970 ‫but when you add this, then it's perfectly clear that we're talking about the member variable here. 206 00:12:09,970 --> 00:12:15,430 ‫So we're talking about the variable that is on the highest level of this class. 207 00:12:15,430 --> 00:12:18,910 ‫So it's not within any method or so you couldn't even access it in this way. 208 00:12:18,910 --> 00:12:22,960 ‫But here it's this particular length that we're talking about. 209 00:12:22,960 --> 00:12:27,490 ‫So if I click on it, you can see my ID automatically marks it. 210 00:12:27,490 --> 00:12:31,570 ‫And it's also marking this length here at the bottom. 211 00:12:33,120 --> 00:12:38,850 ‫So now I can get the length by using get length by using this public method. 212 00:12:38,850 --> 00:12:42,780 ‫And it's important that it's public because if it wasn't public, then I couldn't access it. 213 00:12:43,260 --> 00:12:50,100 ‫So in this case, I'm saving the box again, and now I'm getting the length by calling this method get 214 00:12:50,100 --> 00:12:52,500 ‫length, and now it suddenly works again. 215 00:12:52,530 --> 00:12:59,340 ‫So now I can go ahead and of course write it correctly here as well length and let's run it again. 216 00:12:59,340 --> 00:13:01,620 ‫And it's going to say box length is. 217 00:13:02,010 --> 00:13:04,200 ‫And in this case, it's four. 218 00:13:05,070 --> 00:13:05,460 ‫All right. 219 00:13:05,460 --> 00:13:09,610 ‫So this is just the preparation for properties, but it's an important concept. 220 00:13:09,630 --> 00:13:13,440 ‫Setters and getters, this is something that you are going to see quite a bit as well. 221 00:13:13,440 --> 00:13:19,140 ‫So it's a totally fine approach, but there are a lot easier and faster approaches. 222 00:13:19,140 --> 00:13:22,290 ‫So this is just one way to do it. 223 00:13:22,290 --> 00:13:28,980 ‫But I'm going to show you two or even three other ways to make it even quicker and to have the same 224 00:13:28,980 --> 00:13:30,600 ‫type of protection. 225 00:13:30,600 --> 00:13:33,570 ‫Because what we're doing here is called encapsulation. 226 00:13:33,570 --> 00:13:38,700 ‫So we are encapsulating the axis of our length. 227 00:13:40,180 --> 00:13:46,600 ‫The definition of Wikipedia is that encapsulation refers to the building of data with the methods that 228 00:13:46,600 --> 00:13:53,880 ‫operate on that data, or the restricting of direct access to some of an object's components. 229 00:13:53,890 --> 00:13:55,300 ‫So that's exactly what we're doing. 230 00:13:55,300 --> 00:14:02,230 ‫We are restricting the access to this component called length, which is part of an object or our class 231 00:14:02,230 --> 00:14:04,450 ‫box of which we can create objects. 232 00:14:04,720 --> 00:14:05,350 ‫All right. 233 00:14:05,680 --> 00:14:11,740 ‫And we create these setters and getters in order to still adjust them. 234 00:14:12,160 --> 00:14:18,550 ‫Now, the beauty is that you can define how you want to encapsulate. 235 00:14:18,550 --> 00:14:20,710 ‫In this case, I made both of them public. 236 00:14:20,860 --> 00:14:22,720 ‫But what if I don't want them to be public? 237 00:14:22,720 --> 00:14:24,520 ‫What if I want this to be? 238 00:14:25,770 --> 00:14:26,690 ‫Not suitable. 239 00:14:26,700 --> 00:14:31,800 ‫So let's say I don't want anyone to be able to set the length of my box. 240 00:14:31,800 --> 00:14:35,100 ‫So what I could do is I could make this one private. 241 00:14:35,250 --> 00:14:36,870 ‫So now only. 242 00:14:38,050 --> 00:14:45,280 ‫Classes that inherit from our box or the box itself will be able to set the length from outside. 243 00:14:45,280 --> 00:14:47,230 ‫We will not be able to set the length anymore. 244 00:14:47,230 --> 00:14:53,980 ‫So setting length here, this line of code is not working anymore because I decided, okay, if anyone 245 00:14:53,980 --> 00:15:02,530 ‫wants to set the length, I wanted to only be the class itself or inheriting classes, but not classes 246 00:15:03,040 --> 00:15:04,060 ‫outside of me. 247 00:15:04,840 --> 00:15:08,170 ‫This is something that you can directly modify from here. 248 00:15:08,170 --> 00:15:13,300 ‫You could also say, okay, even getting the length would not be allowed outside of here. 249 00:15:13,840 --> 00:15:14,440 ‫All right. 250 00:15:14,530 --> 00:15:20,110 ‫But now the cool thing is you can go ahead and adjust those. 251 00:15:20,260 --> 00:15:23,140 ‫So let's say somebody wants to get the volume. 252 00:15:23,140 --> 00:15:26,020 ‫And now at this point, we haven't set the volume yet. 253 00:15:26,170 --> 00:15:34,510 ‫So what we can do instead, we can go ahead and say public int, get volume. 254 00:15:34,510 --> 00:15:40,120 ‫And now if anyone wants to get the volume, we're not just going to return volume, but what we will 255 00:15:40,120 --> 00:15:42,100 ‫do is we will return. 256 00:15:43,440 --> 00:15:52,350 ‫This dot length multiplied with this dot height multiplied with this dot width. 257 00:15:52,650 --> 00:15:58,020 ‫So if anyone wants to get the volume, they will not get the volume directly. 258 00:15:58,020 --> 00:16:04,740 ‫So not get access to this variable directly, this volume variable, but they will actually get the 259 00:16:04,740 --> 00:16:06,420 ‫result of the calculation. 260 00:16:06,630 --> 00:16:18,810 ‫So now if I save this and instead go ahead and here I use get volume, then boxes volume is and now 261 00:16:18,810 --> 00:16:21,300 ‫it will display the volume of the box. 262 00:16:22,240 --> 00:16:26,230 ‫So let's say boxes, volume is 60. 263 00:16:27,530 --> 00:16:33,770 ‫So now generally what we do with these setters and getters is that we don't give access directly to 264 00:16:33,770 --> 00:16:41,120 ‫the variable to the member variable, but we only give access to a method which then can adjust the 265 00:16:41,120 --> 00:16:50,600 ‫variable and we can ourselves decide how we pass this value, so how we hand out the value in this case 266 00:16:50,600 --> 00:16:54,770 ‫with get volume, we decided, okay, we don't want to give you the volume itself, but we want to give 267 00:16:54,770 --> 00:16:56,570 ‫you the result of a calculation. 268 00:16:56,570 --> 00:17:01,430 ‫Or we could say, okay, if anyone wants to get the volume, we are going to say it's going to be volume 269 00:17:01,430 --> 00:17:03,040 ‫times two or something like that. 270 00:17:03,050 --> 00:17:08,810 ‫Or if anyone wants to set the length and it's something negative. 271 00:17:08,810 --> 00:17:17,810 ‫So let's say we make this public again, public void, set length, and now we double check if length. 272 00:17:18,980 --> 00:17:20,930 ‫Is less than zero. 273 00:17:22,540 --> 00:17:36,130 ‫We're going to throw an error, throw new exception size or this one, a length should be higher than 274 00:17:36,130 --> 00:17:36,790 ‫zero. 275 00:17:37,240 --> 00:17:37,780 ‫All right. 276 00:17:38,020 --> 00:17:45,520 ‫So if length is less than zero, then we throw an exception and this will just crash our program. 277 00:17:45,520 --> 00:17:47,500 ‫So this line will never be executed. 278 00:17:47,680 --> 00:17:49,250 ‫So now let's try this. 279 00:17:49,270 --> 00:17:54,860 ‫Let's go to our program and set our box length to a negative value. 280 00:17:54,880 --> 00:17:56,320 ‫Let's set it to minus four. 281 00:17:56,620 --> 00:18:01,420 ‫Now we run it again and we will see that we get an exception. 282 00:18:01,480 --> 00:18:06,130 ‫So our program crashes and we get an exception here saying. 283 00:18:07,110 --> 00:18:09,900 ‫System exception length should be higher than zero. 284 00:18:10,140 --> 00:18:15,060 ‫So you see, with throwing the exception, we can throw our own exceptions. 285 00:18:15,060 --> 00:18:18,900 ‫We can even say what the exceptions should say and it should be meaningful. 286 00:18:19,080 --> 00:18:22,890 ‫It should be something that is useful and makes clear what the error is. 287 00:18:22,920 --> 00:18:28,800 ‫In this case, we created an error because we set the length to something that is less than zero. 288 00:18:29,910 --> 00:18:30,180 ‫All right. 289 00:18:30,180 --> 00:18:34,350 ‫So that's the power of these setters and getters, because you might wonder, why would I need those 290 00:18:34,350 --> 00:18:35,100 ‫setters and get us? 291 00:18:35,100 --> 00:18:39,200 ‫Why can I not just simply access the length directly if I just have setters and getters? 292 00:18:39,210 --> 00:18:45,420 ‫Well, now you see it, you can actively decide what will be set and what will be forgotten. 293 00:18:45,420 --> 00:18:51,150 ‫If somebody tries to access this variable and change it, or even just retrieve it. 294 00:18:52,070 --> 00:18:52,400 ‫All right. 295 00:18:52,400 --> 00:18:58,040 ‫So this was a lot longer than I had anticipated, and I was planning on doing this video. 296 00:18:58,040 --> 00:19:03,470 ‫But I really wanted to make everything clear because this is going to be an important stepping stone 297 00:19:03,470 --> 00:19:07,160 ‫for the next concept, which is going to be properties. 298 00:19:07,160 --> 00:19:13,460 ‫So you're going to see how we can sum this all up or make it a lot shorter by using properties. 299 00:19:13,700 --> 00:19:18,920 ‫So I'd say let's go to the next video where we're going to check out how to use properties and how they 300 00:19:18,920 --> 00:19:21,470 ‫relate to what we have learned in this video. 301 00:19:21,590 --> 00:19:22,790 ‫I hope it made sense. 302 00:19:22,790 --> 00:19:24,800 ‫If you have any questions, please let me know. 303 00:19:24,800 --> 00:19:29,750 ‫And yeah, just write a comment in the course discussions. 304 00:19:31,020 --> 00:19:36,480 ‫And by the way, thanks a lot for everyone who gives feedback because it really helps me to make the 305 00:19:36,480 --> 00:19:37,350 ‫course better. 306 00:19:37,350 --> 00:19:40,590 ‫If you just passively watching this course, it's totally fine. 307 00:19:40,590 --> 00:19:46,860 ‫But I really appreciate if you have feedback and it's constructive and really helps me to know at which 308 00:19:46,860 --> 00:19:48,540 ‫point something isn't clear. 309 00:19:48,540 --> 00:19:53,310 ‫Because if you just say, okay, this video is complete garbage, then it will not be as useful as if 310 00:19:53,310 --> 00:19:58,950 ‫you say, okay, I didn't understand concept A at minute five or something, then I understand a lot 311 00:19:58,950 --> 00:20:00,180 ‫better what the problem is. 312 00:20:00,180 --> 00:20:02,970 ‫So thanks again and see you in the next video.