All language subtitles for Learn Kotlin Programming – Full Course for Beginners.en2

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese Download
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 5001 03:43:27,720 --> 03:43:29,640 line of code we have one single 5002 03:43:29,640 --> 03:43:32,220 expression we just return some text or 5003 03:43:32,220 --> 03:43:34,319 function we don't have multiple 5004 03:43:34,319 --> 03:43:37,319 statements in our function we can write 5005 03:43:37,319 --> 03:43:39,779 this as a single expression function 5006 03:43:39,779 --> 03:43:42,479 and you can remove the calibration you 5007 03:43:42,479 --> 03:43:44,819 can write this as a single expression 5008 03:43:44,819 --> 03:43:46,920 body like we did in a previous video so 5009 03:43:46,920 --> 03:43:48,840 you can put it just equals and you can 5010 03:43:48,840 --> 03:43:50,340 remove the 5011 03:43:50,340 --> 03:43:54,680 type here also so if you run this code 5012 03:43:54,840 --> 03:43:57,600 now I'm gonna see it to the output for 5013 03:43:57,600 --> 03:44:00,359 the parameter message some text which is 5014 03:44:00,359 --> 03:44:02,160 the value which is returned from this 5015 03:44:02,160 --> 03:44:04,859 function from the single body expression 5016 03:44:04,859 --> 03:44:06,960 function right 5017 03:44:06,960 --> 03:44:09,239 so see in the next video 5018 03:44:09,239 --> 03:44:10,979 all right now it's time to look at the 5019 03:44:10,979 --> 03:44:13,620 VAR Arc keyword and how we can use the 5020 03:44:13,620 --> 03:44:15,479 water keyword with functions but first 5021 03:44:15,479 --> 03:44:17,100 let's declare the function below of our 5022 03:44:17,100 --> 03:44:18,420 enclosing 5023 03:44:18,420 --> 03:44:21,000 calibrase of our main function so I'm 5024 03:44:21,000 --> 03:44:23,460 going to type here the 5025 03:44:23,460 --> 03:44:26,460 fun keyword to declare a function 5026 03:44:26,460 --> 03:44:28,500 I'm going to call it sum because it's 5027 03:44:28,500 --> 03:44:30,660 going to return the sum of all the 5028 03:44:30,660 --> 03:44:32,640 parameter that we're going to Define so 5029 03:44:32,640 --> 03:44:34,500 I'm going to Define here some parameters 5030 03:44:34,500 --> 03:44:36,479 for it's going to be called a it's going 5031 03:44:36,479 --> 03:44:38,580 to be of type int 5032 03:44:38,580 --> 03:44:41,220 a single second is going to be called B 5033 03:44:41,220 --> 03:44:43,938 also in 5034 03:44:44,460 --> 03:44:46,680 third is going to be called C 5035 03:44:46,680 --> 03:44:50,420 also an end 5036 03:44:50,640 --> 03:44:52,200 the final one is going to be called the 5037 03:44:52,200 --> 03:44:54,000 alternate 5038 03:44:54,000 --> 03:44:55,800 and this this function is going to 5039 03:44:55,800 --> 03:44:58,020 return the sum of all of those 5040 03:44:58,020 --> 03:45:00,720 parameters so we need to Define here the 5041 03:45:00,720 --> 03:45:01,859 return type 5042 03:45:01,859 --> 03:45:04,319 which is going to be an hint 5043 03:45:04,319 --> 03:45:07,080 a whole number and now we can put the 5044 03:45:07,080 --> 03:45:10,200 curly braces and here we type return 5045 03:45:10,200 --> 03:45:11,760 a 5046 03:45:11,760 --> 03:45:14,840 plus b 5047 03:45:14,880 --> 03:45:16,800 plus c 5048 03:45:16,800 --> 03:45:17,939 Plus 5049 03:45:17,939 --> 03:45:19,080 d 5050 03:45:19,080 --> 03:45:21,540 and this is going to return the sum of 5051 03:45:21,540 --> 03:45:23,580 all the parameters that we Define here 5052 03:45:23,580 --> 03:45:26,640 or the values that is going to be passed 5053 03:45:26,640 --> 03:45:29,640 as arguments to this function and you 5054 03:45:29,640 --> 03:45:31,920 can write this as a single expression 5055 03:45:31,920 --> 03:45:33,479 function if you want so you can write 5056 03:45:33,479 --> 03:45:37,020 this uh like this if you want 5057 03:45:37,020 --> 03:45:40,340 this will have the same effect 5058 03:45:41,520 --> 03:45:46,160 but I will I will write it with the 5059 03:45:46,160 --> 03:45:48,960 curly braces because it will make things 5060 03:45:48,960 --> 03:45:52,739 more clear so let's undo this 5061 03:45:52,739 --> 03:45:55,620 right now let's call this function here 5062 03:45:55,620 --> 03:45:57,720 sum 5063 03:45:57,720 --> 03:46:00,960 let's press Ctrl alt L to format the 5064 03:46:00,960 --> 03:46:03,779 code and let's pass some values here so 5065 03:46:03,779 --> 03:46:05,939 I'm going to type here five 5066 03:46:05,939 --> 03:46:09,840 six seven 5067 03:46:10,680 --> 03:46:12,359 and 5068 03:46:12,359 --> 03:46:14,160 10. 5069 03:46:14,160 --> 03:46:16,920 all right if you press shift Ctrl p on 5070 03:46:16,920 --> 03:46:19,140 this you're gonna see that the type of 5071 03:46:19,140 --> 03:46:22,140 this function let's press again 5072 03:46:22,140 --> 03:46:24,600 the type of this function is an ins so 5073 03:46:24,600 --> 03:46:29,520 this function returns an integer so it's 5074 03:46:29,520 --> 03:46:32,700 it's an expression that means that we 5075 03:46:32,700 --> 03:46:35,939 can put this instead of putting storing 5076 03:46:35,939 --> 03:46:36,479 um 5077 03:46:36,479 --> 03:46:40,200 uh the return value in a variable we can 5078 03:46:40,200 --> 03:46:42,540 put this direction a printerland 5079 03:46:42,540 --> 03:46:46,819 function so you can type here print 5080 03:46:46,920 --> 03:46:48,359 area 5081 03:46:48,359 --> 03:46:50,460 and we can put the function inside here 5082 03:46:50,460 --> 03:46:52,680 because that function is of type in so 5083 03:46:52,680 --> 03:46:54,300 it's going to return 5084 03:46:54,300 --> 03:46:57,000 the sum of all the arguments that we 5085 03:46:57,000 --> 03:46:58,739 type there 5086 03:46:58,739 --> 03:47:01,560 so now if you run this code 5087 03:47:01,560 --> 03:47:05,600 we got we get 28 because 5 plus 6 Plus 5088 03:47:05,600 --> 03:47:09,420 7.10 is 28. now if I want to pass 5089 03:47:09,420 --> 03:47:11,640 another parameter here let's say that I 5090 03:47:11,640 --> 03:47:14,160 want to pass another number here I can't 5091 03:47:14,160 --> 03:47:17,040 because I am limited by the number of 5092 03:47:17,040 --> 03:47:19,140 parameters that we Define here of course 5093 03:47:19,140 --> 03:47:22,260 that I can Define another parameter here 5094 03:47:22,260 --> 03:47:25,319 but what if I want to pass 10 numbers 5095 03:47:25,319 --> 03:47:29,279 for 15 numbers or 100 numbers in that 5096 03:47:29,279 --> 03:47:31,200 case it will be very tedious to write 5097 03:47:31,200 --> 03:47:34,020 all of those parameters and and to solve 5098 03:47:34,020 --> 03:47:35,700 that you need to use what is called a 5099 03:47:35,700 --> 03:47:39,239 varg varag keyword and Avada keyword 5100 03:47:39,239 --> 03:47:41,340 allows us to pass more than one 5101 03:47:41,340 --> 03:47:43,620 parameter actually more than one 5102 03:47:43,620 --> 03:47:45,720 argument to our 5103 03:47:45,720 --> 03:47:48,479 parameters so to declare a Vari keyword 5104 03:47:48,479 --> 03:47:51,380 we type here 5105 03:47:51,960 --> 03:47:55,319 we type here far ARG so this is the 5106 03:47:55,319 --> 03:47:58,319 keyword varag it stands for variable 5107 03:47:58,319 --> 03:47:59,880 arguments 5108 03:47:59,880 --> 03:48:02,700 now we need to define a name for this 5109 03:48:02,700 --> 03:48:06,720 bar Arc I'm going to call it numbers 5110 03:48:06,720 --> 03:48:10,439 and we also need to specify a type what 5111 03:48:10,439 --> 03:48:13,620 kind of values this varag is going to 5112 03:48:13,620 --> 03:48:14,340 store 5113 03:48:14,340 --> 03:48:18,319 now this return keyword is this 5114 03:48:18,319 --> 03:48:20,600 variables here are red because they 5115 03:48:20,600 --> 03:48:23,640 don't exist 5116 03:48:23,640 --> 03:48:27,180 now if I want to 5117 03:48:27,180 --> 03:48:29,700 but first let's say that I want to type 5118 03:48:29,700 --> 03:48:32,460 here another parameter now now you see 5119 03:48:32,460 --> 03:48:34,200 that we have no problem so I can type 5120 03:48:34,200 --> 03:48:36,420 here whatever 5121 03:48:36,420 --> 03:48:40,680 number of numbers I want and there is no 5122 03:48:40,680 --> 03:48:44,640 problem as you can see now 5123 03:48:44,640 --> 03:48:47,399 to to 5124 03:48:47,399 --> 03:48:51,000 sum the values which are stored in these 5125 03:48:51,000 --> 03:48:52,739 viral numbers we need to use something 5126 03:48:52,739 --> 03:48:55,680 which we didn't cover yet but we have a 5127 03:48:55,680 --> 03:48:58,979 separate section which I show you how to 5128 03:48:58,979 --> 03:49:01,200 we need to explain this thing that I'm 5129 03:49:01,200 --> 03:49:03,060 going to use now and that is Loops 5130 03:49:03,060 --> 03:49:07,200 because now we need to Loop through our 5131 03:49:07,200 --> 03:49:10,020 numbers and to add our numbers to a 5132 03:49:10,020 --> 03:49:12,300 variable so we need to use what is 5133 03:49:12,300 --> 03:49:15,239 called a for Loop so to do that first we 5134 03:49:15,239 --> 03:49:16,739 declare a variable 5135 03:49:16,739 --> 03:49:20,899 I'm gonna call it um 5136 03:49:21,180 --> 03:49:25,460 let's say uh result 5137 03:49:27,420 --> 03:49:30,840 I'm gonna assign a value of zero 5138 03:49:30,840 --> 03:49:35,220 now to use a for Loop we Type 4 5139 03:49:35,220 --> 03:49:37,979 and again I'm gonna go into more detail 5140 03:49:37,979 --> 03:49:40,260 about this in uh the separate section 5141 03:49:40,260 --> 03:49:42,060 but now we need to use this because 5142 03:49:42,060 --> 03:49:45,600 there's not another way to to sum up the 5143 03:49:45,600 --> 03:49:49,979 values inside the varag numbers 5144 03:49:49,979 --> 03:49:53,640 and here we need to define a name for 5145 03:49:53,640 --> 03:49:55,020 our variable so it's going to be called 5146 03:49:55,020 --> 03:49:56,220 number 5147 03:49:56,220 --> 03:50:00,479 and now we use the keyword in 5148 03:50:00,479 --> 03:50:02,939 and now we're referring the 5149 03:50:02,939 --> 03:50:06,720 parameter the varag parameter which is 5150 03:50:06,720 --> 03:50:09,960 defined up called numbers 5151 03:50:09,960 --> 03:50:11,520 all right 5152 03:50:11,520 --> 03:50:15,300 now we here you can put the curly braces 5153 03:50:15,300 --> 03:50:17,220 and inside the cutter blazes we're going 5154 03:50:17,220 --> 03:50:19,319 to type 5155 03:50:19,319 --> 03:50:22,319 numbers 5156 03:50:23,279 --> 03:50:25,200 result 5157 03:50:25,200 --> 03:50:26,520 and now we're going to use that 5158 03:50:26,520 --> 03:50:30,120 abbreviation which is plus n equals 5159 03:50:30,120 --> 03:50:33,200 plus equals 5160 03:50:33,899 --> 03:50:36,000 number n 5161 03:50:36,000 --> 03:50:38,760 now here gonna return 5162 03:50:38,760 --> 03:50:42,359 the result return 5163 03:50:42,420 --> 03:50:44,840 result 5164 03:50:45,779 --> 03:50:48,120 all right so let's explain what we have 5165 03:50:48,120 --> 03:50:50,640 here so first we declare a variable we 5166 03:50:50,640 --> 03:50:52,859 assign a value of 0 to it so it's just 5167 03:50:52,859 --> 03:50:54,840 type int 5168 03:50:54,840 --> 03:50:58,620 next what this for Loop is doing so this 5169 03:50:58,620 --> 03:51:01,020 for Loop is doing what is doing is 5170 03:51:01,020 --> 03:51:02,399 executing 5171 03:51:02,399 --> 03:51:06,300 um a line of code multiple times so it 5172 03:51:06,300 --> 03:51:10,260 executing is executing uh uh what's 5173 03:51:10,260 --> 03:51:14,340 inside the curly braces until the it 5174 03:51:14,340 --> 03:51:17,399 finishes to it finishes the to Loop 5175 03:51:17,399 --> 03:51:19,920 through all these numbers which are 5176 03:51:19,920 --> 03:51:24,979 stored in the varark number so it first 5177 03:51:24,979 --> 03:51:30,000 assigns uh the first the first number or 5178 03:51:30,000 --> 03:51:31,500 number here 5179 03:51:31,500 --> 03:51:35,640 and it loops and it has that it adds 5180 03:51:35,640 --> 03:51:37,859 that number to a result then it does 5181 03:51:37,859 --> 03:51:41,279 that again and again and again until it 5182 03:51:41,279 --> 03:51:43,920 finishes so then it reaches uh the final 5183 03:51:43,920 --> 03:51:46,080 number five and in that case also gonna 5184 03:51:46,080 --> 03:51:47,100 sign 5185 03:51:47,100 --> 03:51:50,700 from our numbers uh the number five to 5186 03:51:50,700 --> 03:51:54,720 our number and then that number is going 5187 03:51:54,720 --> 03:51:57,660 to be added to our result variable and 5188 03:51:57,660 --> 03:52:01,680 when it finishes uh that looping when it 5189 03:52:01,680 --> 03:52:05,700 finishes that thing is gonna return the 5190 03:52:05,700 --> 03:52:07,560 result it's gonna return all the values 5191 03:52:07,560 --> 03:52:11,160 which are which were been added to our 5192 03:52:11,160 --> 03:52:12,960 result variable 5193 03:52:12,960 --> 03:52:16,460 so now if you print this 5194 03:52:17,279 --> 03:52:20,580 now I get 85 and probably this is the 5195 03:52:20,580 --> 03:52:24,060 result of this calculation so this is 5196 03:52:24,060 --> 03:52:27,420 how you can use varag numbers what are 5197 03:52:27,420 --> 03:52:29,819 the keyword to store more than one 5198 03:52:29,819 --> 03:52:33,180 single more than a limited number of 5199 03:52:33,180 --> 03:52:35,220 parameters defined 5200 03:52:35,220 --> 03:52:40,020 bio and another short shortcut to to 5201 03:52:40,020 --> 03:52:42,779 Loop through this is using a for each 5202 03:52:42,779 --> 03:52:46,739 Loop so we can do something like this 5203 03:52:46,739 --> 03:52:49,260 but this time let's say that I want I 5204 03:52:49,260 --> 03:52:52,080 don't want to return something 5205 03:52:52,080 --> 03:52:55,640 so let's see that I delete this 5206 03:52:59,340 --> 03:53:04,140 and I I can type here actually numbers 5207 03:53:04,140 --> 03:53:05,819 four 5208 03:53:05,819 --> 03:53:08,340 dot here to put that 5209 03:53:08,340 --> 03:53:10,620 four 5210 03:53:10,620 --> 03:53:12,540 each 5211 03:53:12,540 --> 03:53:16,979 and here we type print Ln 5212 03:53:16,979 --> 03:53:20,399 again this is a more complex subject 5213 03:53:20,399 --> 03:53:22,140 which is going to be covered in the next 5214 03:53:22,140 --> 03:53:27,000 videos so here we type print Ln 5215 03:53:27,000 --> 03:53:30,180 and we type it 5216 03:53:30,180 --> 03:53:33,540 all right now let's uh 5217 03:53:33,540 --> 03:53:36,180 let's call our sum 5218 03:53:36,180 --> 03:53:38,160 let's define some numbers here let's say 5219 03:53:38,160 --> 03:53:40,859 that I type here 5220 03:53:40,859 --> 03:53:42,840 one 5221 03:53:42,840 --> 03:53:45,200 four 5222 03:53:50,880 --> 03:53:54,180 all right so if you run this code 5223 03:53:54,180 --> 03:53:56,880 now you can see that every number in 5224 03:53:56,880 --> 03:53:58,140 that 5225 03:53:58,140 --> 03:54:00,540 that we defined here is printed one by 5226 03:54:00,540 --> 03:54:03,779 one so this number this for each which 5227 03:54:03,779 --> 03:54:06,600 is also a loop it goes through all of 5228 03:54:06,600 --> 03:54:09,359 these numbers it and it prints them one 5229 03:54:09,359 --> 03:54:12,120 by one and again we're gonna look at uh 5230 03:54:12,120 --> 03:54:14,399 for the for Loop and the four each in 5231 03:54:14,399 --> 03:54:18,479 the next videos but I had no other way 5232 03:54:18,479 --> 03:54:21,060 to show you this example without using 5233 03:54:21,060 --> 03:54:23,819 them but if you feel confused by for the 5234 03:54:23,819 --> 03:54:26,220 for each or the for Loop don't worry we 5235 03:54:26,220 --> 03:54:27,840 have a separate 5236 03:54:27,840 --> 03:54:30,180 section which in which you talk about in 5237 03:54:30,180 --> 03:54:32,520 detail about this for now we need to 5238 03:54:32,520 --> 03:54:34,319 only the only thing you need to know is 5239 03:54:34,319 --> 03:54:36,540 that you can 5240 03:54:36,540 --> 03:54:40,140 pass an indefinite number of parameters 5241 03:54:40,140 --> 03:54:42,600 of actually you can pass it an 5242 03:54:42,600 --> 03:54:45,979 indefinite number of arguments to our 5243 03:54:45,979 --> 03:54:49,979 varag numbers because this allows us to 5244 03:54:49,979 --> 03:54:53,279 to power to to pass uh 5245 03:54:53,279 --> 03:54:57,479 an indefinite number of values so here 5246 03:54:57,479 --> 03:54:59,760 you see that we have one four five six 5247 03:54:59,760 --> 03:55:04,020 seven eight nine five seven and six 5248 03:55:04,020 --> 03:55:07,020 numbers that we have here so this for 5249 03:55:07,020 --> 03:55:09,060 each is going through all of them and it 5250 03:55:09,060 --> 03:55:10,319 prints 5251 03:55:10,319 --> 03:55:13,080 them one by one so 5252 03:55:13,080 --> 03:55:16,220 see you in the next video 5253 03:55:18,060 --> 03:55:19,680 all right now it's time to start our 5254 03:55:19,680 --> 03:55:21,840 discussion about loops but first let's 5255 03:55:21,840 --> 03:55:24,620 create a new project 5256 03:55:24,660 --> 03:55:27,920 I'm going to call it loops 5257 03:55:29,520 --> 03:55:34,460 select IntelliJ click on next and finish 5258 03:55:35,640 --> 03:55:37,680 right a project is created let's delete 5259 03:55:37,680 --> 03:55:40,319 the code inside the color braces 5260 03:55:40,319 --> 03:55:42,720 let's hide the project pane 5261 03:55:42,720 --> 03:55:46,800 and let's start by explaining what Loops 5262 03:55:46,800 --> 03:55:48,960 are and why you need to use them 5263 03:55:48,960 --> 03:55:53,040 so Loops allows us to execute a piece of 5264 03:55:53,040 --> 03:55:56,279 code multiple times without 5265 03:55:56,279 --> 03:56:00,979 you writing down every line of code 5266 03:56:00,979 --> 03:56:04,199 manually so 5267 03:56:04,199 --> 03:56:06,840 let's say that I want to print 5268 03:56:06,840 --> 03:56:10,680 10 numbers in the console or 20 or 50. 5269 03:56:10,680 --> 03:56:13,920 instead of printing instead of typing 5270 03:56:13,920 --> 03:56:16,199 that code manually move line by line 5271 03:56:16,199 --> 03:56:19,260 which will be very tedious you can use a 5272 03:56:19,260 --> 03:56:21,540 loop and you can put that code in a loop 5273 03:56:21,540 --> 03:56:23,720 and in that code in the loop it will be 5274 03:56:23,720 --> 03:56:28,560 executed repeatedly until 5275 03:56:29,040 --> 03:56:34,260 a certain number is reached so let's see 5276 03:56:34,260 --> 03:56:36,479 how we can define a loop to define a 5277 03:56:36,479 --> 03:56:38,640 loop we type here 5278 03:56:38,640 --> 03:56:41,520 four this is the first Loop that we're 5279 03:56:41,520 --> 03:56:43,140 gonna look at because there are other 5280 03:56:43,140 --> 03:56:45,560 loops 5281 03:56:45,600 --> 03:56:49,199 so this is the for Loop we press enter 5282 03:56:49,199 --> 03:56:51,540 inside the parenthesis we type I this is 5283 03:56:51,540 --> 03:56:53,520 convention you can put whatever name you 5284 03:56:53,520 --> 03:56:54,600 wonder 5285 03:56:54,600 --> 03:56:57,420 now we put here in this is the keyword 5286 03:56:57,420 --> 03:56:59,699 in and now let's say that I want to loop 5287 03:56:59,699 --> 03:57:00,720 from 5288 03:57:00,720 --> 03:57:02,220 one 5289 03:57:02,220 --> 03:57:05,340 and from 1 to 10 and to do that to type 5290 03:57:05,340 --> 03:57:10,160 1 dot dot this is a range 5291 03:57:10,620 --> 03:57:12,660 one to ten now 5292 03:57:12,660 --> 03:57:15,120 I put a curly braces 5293 03:57:15,120 --> 03:57:17,520 and inside the pre the curly braces and 5294 03:57:17,520 --> 03:57:19,319 I'm gonna put the code that I want to be 5295 03:57:19,319 --> 03:57:21,060 executed 5296 03:57:21,060 --> 03:57:24,180 10 times so I'm gonna type here println 5297 03:57:24,180 --> 03:57:26,760 and I'm gonna type the value of I at 5298 03:57:26,760 --> 03:57:30,779 each iteration and uh 5299 03:57:30,779 --> 03:57:33,479 we can put it like this you can type 5300 03:57:33,479 --> 03:57:35,279 here uh 5301 03:57:35,279 --> 03:57:37,140 you can put in the quotation marks and 5302 03:57:37,140 --> 03:57:40,460 you can do the placeholder 5303 03:57:41,520 --> 03:57:42,960 you can put I 5304 03:57:42,960 --> 03:57:46,278 so now if you run this 5305 03:57:47,939 --> 03:57:50,640 so now I get 5306 03:57:50,640 --> 03:57:52,680 is one two 5307 03:57:52,680 --> 03:57:57,199 until 10. so it what is doing is is 5308 03:57:57,199 --> 03:58:01,800 looping through this range until it 5309 03:58:01,800 --> 03:58:03,960 reaches 10 then it stops 5310 03:58:03,960 --> 03:58:07,680 now uh you can write this I like I did 5311 03:58:07,680 --> 03:58:09,660 here using a placeholder where you can 5312 03:58:09,660 --> 03:58:12,619 write it like this 5313 03:58:13,140 --> 03:58:15,359 I've run this code it will have the same 5314 03:58:15,359 --> 03:58:17,359 effect but will not have the taste 5315 03:58:17,359 --> 03:58:20,720 before here 5316 03:58:23,100 --> 03:58:24,840 so now you get 5317 03:58:24,840 --> 03:58:25,979 one 5318 03:58:25,979 --> 03:58:29,220 until 10. 5319 03:58:29,279 --> 03:58:31,920 now there are other other very 5320 03:58:31,920 --> 03:58:35,880 variations to use this range and one of 5321 03:58:35,880 --> 03:58:40,560 them is to use until so let's uh command 5322 03:58:40,560 --> 03:58:41,640 this code 5323 03:58:41,640 --> 03:58:45,500 so let's press Ctrl slash 5324 03:58:49,080 --> 03:58:52,620 right now let's type 4 again 5325 03:58:52,620 --> 03:58:55,260 and now I'm gonna 5326 03:58:55,260 --> 03:58:57,180 I'm gonna call it also I here you can 5327 03:58:57,180 --> 03:58:59,040 put whatever name you want 5328 03:58:59,040 --> 03:59:02,580 and instead of now I'm going to put also 5329 03:59:02,580 --> 03:59:05,640 again the in keyword and instead of 5330 03:59:05,640 --> 03:59:09,000 putting the dot dot to Loop to arrange 5331 03:59:09,000 --> 03:59:10,199 I'm gonna put 5332 03:59:10,199 --> 03:59:13,620 one until 5333 03:59:13,620 --> 03:59:15,479 10. 5334 03:59:15,479 --> 03:59:17,640 all right 5335 03:59:17,640 --> 03:59:20,939 now I'm gonna add the printerliner 5336 03:59:20,939 --> 03:59:23,040 statement 5337 03:59:23,040 --> 03:59:26,100 and here I'm going to type I 5338 03:59:26,100 --> 03:59:27,420 and 5339 03:59:27,420 --> 03:59:30,779 this will have the same effect with the 5340 03:59:30,779 --> 03:59:33,239 thing with a single difference that 1 5341 03:59:33,239 --> 03:59:37,979 until 10 is is gonna exclude ten and one 5342 03:59:37,979 --> 03:59:39,479 uh 5343 03:59:39,479 --> 03:59:43,380 using the range.10 it will include 10. 5344 03:59:43,380 --> 03:59:46,339 so if you run this code 5345 03:59:46,620 --> 03:59:49,140 we're gonna see that you will not have 5346 03:59:49,140 --> 03:59:52,100 10 here because 10 it is excluded when 5347 03:59:52,100 --> 03:59:54,899 you're using the until 5348 03:59:54,899 --> 03:59:57,180 so now as you can see we have one until 5349 03:59:57,180 --> 03:59:59,699 nine and then it's excluded because now 5350 03:59:59,699 --> 04:00:01,500 we're using the until 5351 04:00:01,500 --> 04:00:04,859 so if you if you're using the range 5352 04:00:04,859 --> 04:00:08,460 which is uh dot the last number is going 5353 04:00:08,460 --> 04:00:10,979 to be included but if you use the 5354 04:00:10,979 --> 04:00:13,439 until the last number is going to be 5355 04:00:13,439 --> 04:00:15,779 excluded so it's important to know that 5356 04:00:15,779 --> 04:00:20,220 this two respectively the until and the 5357 04:00:20,220 --> 04:00:24,560 range can only count upwards 5358 04:00:25,979 --> 04:00:29,460 so we cannot put here 10 until 1 because 5359 04:00:29,460 --> 04:00:32,640 the first number the number of on the 5360 04:00:32,640 --> 04:00:34,859 left needs to be smaller than the number 5361 04:00:34,859 --> 04:00:37,560 on the right so we cannot put 10 until 5362 04:00:37,560 --> 04:00:40,439 1. we can only count upwards you can 5363 04:00:40,439 --> 04:00:43,699 only put one until 10 or 5364 04:00:43,699 --> 04:00:46,140 1.10 so 5365 04:00:46,140 --> 04:00:49,739 to Loop backwards we need to use 5366 04:00:49,739 --> 04:00:52,439 another thing so let's comment this code 5367 04:00:52,439 --> 04:00:54,859 also 5368 04:00:56,580 --> 04:01:00,600 now to Loop backwards we Type 4 5369 04:01:00,600 --> 04:01:03,540 again in the paradise you put the I here 5370 04:01:03,540 --> 04:01:06,020 you can put 5371 04:01:07,739 --> 04:01:09,600 whatever if you want 5372 04:01:09,600 --> 04:01:10,979 in 5373 04:01:10,979 --> 04:01:13,260 so here you can put whatever name but 5374 04:01:13,260 --> 04:01:16,080 the convention is I issues now I'm going 5375 04:01:16,080 --> 04:01:19,920 to put in and now I'm going to put down 5376 04:01:19,920 --> 04:01:21,840 first let's put uh 5377 04:01:21,840 --> 04:01:23,160 10 5378 04:01:23,160 --> 04:01:25,260 down 5379 04:01:25,260 --> 04:01:28,220 down to 5380 04:01:29,640 --> 04:01:31,680 one 5381 04:01:31,680 --> 04:01:35,160 let's add the curly braces 5382 04:01:35,160 --> 04:01:37,800 the printerland 5383 04:01:37,800 --> 04:01:40,260 and let's put I here 5384 04:01:40,260 --> 04:01:42,680 now this is gonna this is gonna count 5385 04:01:42,680 --> 04:01:45,000 downwards now it's gonna it's gonna 5386 04:01:45,000 --> 04:01:47,880 count from ten to one so if you run this 5387 04:01:47,880 --> 04:01:50,119 code 5388 04:01:57,359 --> 04:02:00,540 now get 10 9 so it's counting it's it's 5389 04:02:00,540 --> 04:02:03,479 counting backwards right another thing 5390 04:02:03,479 --> 04:02:04,680 that you can do is that you can 5391 04:02:04,680 --> 04:02:09,300 introduce an arbitrary step so let's uh 5392 04:02:09,300 --> 04:02:12,979 let's comment this code also 5393 04:02:14,340 --> 04:02:17,520 let's add below another call 5394 04:02:17,520 --> 04:02:20,699 another for Loop so here I'm gonna type 5395 04:02:20,699 --> 04:02:21,660 four 5396 04:02:21,660 --> 04:02:23,399 parenthesis 5397 04:02:23,399 --> 04:02:25,920 let's put I 5398 04:02:25,920 --> 04:02:29,279 and let's use uh done TL 5399 04:02:29,279 --> 04:02:30,779 so in 5400 04:02:30,779 --> 04:02:32,699 one 5401 04:02:32,699 --> 04:02:35,300 until 5402 04:02:36,359 --> 04:02:38,939 10. so now I can put a 5403 04:02:38,939 --> 04:02:43,560 an arbitrary step so at uh at this 5404 04:02:43,560 --> 04:02:47,000 arbitrary step that number which 5405 04:02:47,000 --> 04:02:50,399 is that is that is at dotted arbitrary 5406 04:02:50,399 --> 04:02:52,500 step is going to be skip so I can put 5407 04:02:52,500 --> 04:02:54,660 here step 5408 04:02:54,660 --> 04:02:57,000 to let's say 5409 04:02:57,000 --> 04:02:59,160 so now I'm gonna put the quarter Blazers 5410 04:02:59,160 --> 04:03:02,220 to have the some code executed I'm going 5411 04:03:02,220 --> 04:03:03,899 to put println 5412 04:03:03,899 --> 04:03:06,300 and let's put here I 5413 04:03:06,300 --> 04:03:09,679 so now if you run the squad 5414 04:03:12,120 --> 04:03:15,300 I get one three five seven and nine so 5415 04:03:15,300 --> 04:03:17,760 you can see that uh 5416 04:03:17,760 --> 04:03:21,540 at the second iteration the number is 5417 04:03:21,540 --> 04:03:23,819 Skip so we have one two is skipped now 5418 04:03:23,819 --> 04:03:26,819 we have then we have three four three 5419 04:03:26,819 --> 04:03:29,939 and four skips and so on so you can put 5420 04:03:29,939 --> 04:03:31,739 this arbitrary step if you want so let's 5421 04:03:31,739 --> 04:03:34,880 comment this code also 5422 04:03:39,239 --> 04:03:41,460 and let's add the first code that we had 5423 04:03:41,460 --> 04:03:42,479 here 5424 04:03:42,479 --> 04:03:44,520 to 5425 04:03:44,520 --> 04:03:47,460 to talk a little bit more so let's put I 5426 04:03:47,460 --> 04:03:48,540 in 5427 04:03:48,540 --> 04:03:51,319 one 5428 04:03:51,479 --> 04:03:54,600 let's put the range Dot 10. 5429 04:03:54,600 --> 04:03:57,060 and curly braces 5430 04:03:57,060 --> 04:04:00,560 you're gonna type println 5431 04:04:04,080 --> 04:04:06,620 hi 5432 04:04:08,160 --> 04:04:12,080 now if run this code 5433 04:04:18,359 --> 04:04:21,359 we'll get the same output that as at the 5434 04:04:21,359 --> 04:04:25,500 beginning of the video so get one 5435 04:04:25,500 --> 04:04:29,760 until 10 inclusive now again what this 5436 04:04:29,760 --> 04:04:34,080 uh loop is doing is going through this 5437 04:04:34,080 --> 04:04:37,800 is going to this to this range from 1 to 5438 04:04:37,800 --> 04:04:41,040 10 and it's executing the code inside 5439 04:04:41,040 --> 04:04:44,460 the curly braces 10 times and when it 5440 04:04:44,460 --> 04:04:47,580 reaches 10 it stops and it executes the 5441 04:04:47,580 --> 04:04:49,140 next line of code below here that 5442 04:04:49,140 --> 04:04:50,699 because here we don't have any line of 5443 04:04:50,699 --> 04:04:53,279 code our 5444 04:04:53,279 --> 04:04:57,899 program X is exited and is finished so 5445 04:04:57,899 --> 04:04:59,600 this is our discussion about for Loop 5446 04:04:59,600 --> 04:05:03,439 there are other Loops which can Loop 5447 04:05:03,439 --> 04:05:05,939 which can loop as long as a certain 5448 04:05:05,939 --> 04:05:09,840 condition is true not as long as a 5449 04:05:09,840 --> 04:05:12,600 certain number is reached so we're going 5450 04:05:12,600 --> 04:05:15,359 to look at the next Loops in the next 5451 04:05:15,359 --> 04:05:16,140 video 5452 04:05:16,140 --> 04:05:18,540 see you next 5453 04:05:18,540 --> 04:05:20,160 all right now it's time to look at the 5454 04:05:20,160 --> 04:05:21,420 next two Loops that you can use in 5455 04:05:21,420 --> 04:05:23,279 quadrant and those are the while and the 5456 04:05:23,279 --> 04:05:24,479 Dual Loop 5457 04:05:24,479 --> 04:05:26,939 but first let's open our previous 5458 04:05:26,939 --> 04:05:28,560 project called Loops because I'm going 5459 04:05:28,560 --> 04:05:30,660 to type my code there so open your 5460 04:05:30,660 --> 04:05:33,120 previous project and here I already have 5461 04:05:33,120 --> 04:05:34,800 deleted the code that we had previously 5462 04:05:34,800 --> 04:05:38,160 here but if you have the code delete it 5463 04:05:38,160 --> 04:05:40,680 and let's start our discussion about the 5464 04:05:40,680 --> 04:05:41,760 while loop 5465 04:05:41,760 --> 04:05:44,279 so first I'm going to type 5466 04:05:44,279 --> 04:05:46,800 while 5467 04:05:46,800 --> 04:05:48,720 this is this is how you declare the 5468 04:05:48,720 --> 04:05:49,979 while loop 5469 04:05:49,979 --> 04:05:52,580 and here you can press enter to fill in 5470 04:05:52,580 --> 04:05:55,620 the the wallykid on the parenthesis for 5471 04:05:55,620 --> 04:05:57,300 you or you can type manually the while 5472 04:05:57,300 --> 04:05:58,680 and the parenthesis so I'm going to 5473 04:05:58,680 --> 04:06:00,660 press enter to fill that for you fill in 5474 04:06:00,660 --> 04:06:02,160 that for me 5475 04:06:02,160 --> 04:06:04,260 and next you put a space and you put 5476 04:06:04,260 --> 04:06:06,359 curly brace here so I'm going to put the 5477 04:06:06,359 --> 04:06:08,279 left curly brace and press enter and the 5478 04:06:08,279 --> 04:06:09,420 radical base is going to be added 5479 04:06:09,420 --> 04:06:11,100 automatically as you can see on the line 5480 04:06:11,100 --> 04:06:14,580 five now 5481 04:06:14,580 --> 04:06:17,640 a while loop Loops as long as a certain 5482 04:06:17,640 --> 04:06:20,760 condition is true in contrast to the for 5483 04:06:20,760 --> 04:06:23,040 Loop which Loops for a specific number 5484 04:06:23,040 --> 04:06:26,819 of time while a loop Loops as long as a 5485 04:06:26,819 --> 04:06:29,760 condition or an expression is true so 5486 04:06:29,760 --> 04:06:34,760 this gives us more flexibility in a 5487 04:06:34,819 --> 04:06:38,040 circumstances or you don't know for what 5488 04:06:38,040 --> 04:06:40,560 specific number of times we want our 5489 04:06:40,560 --> 04:06:43,620 code to be repeated so 5490 04:06:43,620 --> 04:06:45,720 we put our condition inside the 5491 04:06:45,720 --> 04:06:47,220 parenthesis here 5492 04:06:47,220 --> 04:06:49,920 and as long as this condition is true 5493 04:06:49,920 --> 04:06:51,720 the code inside the current base is 5494 04:06:51,720 --> 04:06:54,359 going to be executed repeatedly so let's 5495 04:06:54,359 --> 04:06:56,220 see how we can do that so first let's 5496 04:06:56,220 --> 04:06:58,020 discard a variable here 5497 04:06:58,020 --> 04:07:00,180 I'm going to call it number and I'm 5498 04:07:00,180 --> 04:07:02,160 going to sign a value of 0 to it 5499 04:07:02,160 --> 04:07:05,939 and here I'm gonna type while number 5500 04:07:05,939 --> 04:07:09,260 is less than 10 5501 04:07:09,479 --> 04:07:11,520 inside the calibration I'm going to type 5502 04:07:11,520 --> 04:07:14,279 the println function 5503 04:07:14,279 --> 04:07:17,279 and we're going to Output the value of 5504 04:07:17,279 --> 04:07:19,680 our number and then we're going to 5505 04:07:19,680 --> 04:07:21,540 increment the value of a number so I'm 5506 04:07:21,540 --> 04:07:23,040 going to put number I'm going to use the 5507 04:07:23,040 --> 04:07:26,460 increment operator plus plus 5508 04:07:26,460 --> 04:07:28,800 let's put a space here to have things 5509 04:07:28,800 --> 04:07:33,319 more clear now if you run this code 5510 04:07:37,080 --> 04:07:39,779 you're going to see the numbers from 0 5511 04:07:39,779 --> 04:07:41,279 to 9. 5512 04:07:41,279 --> 04:07:44,279 so what is happening how this works 5513 04:07:44,279 --> 04:07:45,600 first 5514 04:07:45,600 --> 04:07:47,220 with the color variable with a sine of 5515 04:07:47,220 --> 04:07:50,160 value of 0 to it next we Loop using the 5516 04:07:50,160 --> 04:07:53,160 while loop and first the while loop is 5517 04:07:53,160 --> 04:07:54,660 checking to see if this condition inside 5518 04:07:54,660 --> 04:07:57,000 the parenthesis is true so it checks to 5519 04:07:57,000 --> 04:07:59,160 see if the value of our variable number 5520 04:07:59,160 --> 04:08:01,439 is less than 10 and it's true because we 5521 04:08:01,439 --> 04:08:03,779 have zero then it executes the code 5522 04:08:03,779 --> 04:08:07,800 inside the curly braces and it outputs 5523 04:08:07,800 --> 04:08:09,600 the value of our number in the console 5524 04:08:09,600 --> 04:08:12,000 using the printerland function then it 5525 04:08:12,000 --> 04:08:14,520 increments the value of our number 5526 04:08:14,520 --> 04:08:16,979 and then it Loops again but now the 5527 04:08:16,979 --> 04:08:19,260 value of our number is one so now it 5528 04:08:19,260 --> 04:08:21,899 will compare again if 5529 04:08:21,899 --> 04:08:24,060 the value for number is less than 10 5530 04:08:24,060 --> 04:08:26,520 which is true it will loop again it will 5531 04:08:26,520 --> 04:08:28,260 output its value it will increment this 5532 04:08:28,260 --> 04:08:30,660 value so now we have two and so on until 5533 04:08:30,660 --> 04:08:33,359 it reaches nine when it reaches 9 it 5534 04:08:33,359 --> 04:08:36,000 will increment the value it will it will 5535 04:08:36,000 --> 04:08:37,680 print it will output the value in the 5536 04:08:37,680 --> 04:08:40,020 console it will increment this value 5537 04:08:40,020 --> 04:08:42,180 then it will check again to see if this 5538 04:08:42,180 --> 04:08:44,220 condition is true but now the value is 5539 04:08:44,220 --> 04:08:45,239 going to be 5540 04:08:45,239 --> 04:08:48,300 10 because not it was incremented when 5541 04:08:48,300 --> 04:08:49,680 it was nine 5542 04:08:49,680 --> 04:08:51,420 and this condition is going to be ever 5543 04:08:51,420 --> 04:08:53,520 it's going to evaluate to false and the 5544 04:08:53,520 --> 04:08:55,199 code inside the cutter base is not going 5545 04:08:55,199 --> 04:08:57,239 to be executed this way we don't have 10 5546 04:08:57,239 --> 04:09:00,060 here and it executes the code that comes 5547 04:09:00,060 --> 04:09:01,680 below of our while loop but because you 5548 04:09:01,680 --> 04:09:03,000 don't have any code 5549 04:09:03,000 --> 04:09:06,120 the program is terminated 5550 04:09:06,120 --> 04:09:08,699 and you can write this thing in one 5551 04:09:08,699 --> 04:09:11,580 single line but it's recommended to use 5552 04:09:11,580 --> 04:09:14,760 this form only if you know that you have 5553 04:09:14,760 --> 04:09:18,239 one single statement in our Loop so if I 5554 04:09:18,239 --> 04:09:20,899 can delete this 5555 04:09:21,000 --> 04:09:24,420 this tool 5556 04:09:24,420 --> 04:09:27,960 and I can put here directly number plus 5557 04:09:27,960 --> 04:09:31,020 plus and because this Returns the value 5558 04:09:31,020 --> 04:09:33,000 it increments the value and it Returns 5559 04:09:33,000 --> 04:09:36,359 the values and expression I can 5560 04:09:36,359 --> 04:09:38,399 I can run this code and it will have the 5561 04:09:38,399 --> 04:09:41,000 same output 5562 04:09:41,040 --> 04:09:43,680 but if I delete the increment operator 5563 04:09:43,680 --> 04:09:46,140 out after the 5564 04:09:46,140 --> 04:09:50,100 variable and if I put it before 5565 04:09:50,100 --> 04:09:52,439 now I'm gonna see a different output 5566 04:09:52,439 --> 04:09:54,540 here because this increments the value 5567 04:09:54,540 --> 04:09:55,800 of 5568 04:09:55,800 --> 04:09:58,560 of our number then it decrements the 5569 04:09:58,560 --> 04:10:00,540 value of our number and this is called 5570 04:10:00,540 --> 04:10:04,620 prefix in increment increment so this is 5571 04:10:04,620 --> 04:10:07,920 called prefix incrementing and the the 5572 04:10:07,920 --> 04:10:11,279 previous one let's press Ctrl G 5573 04:10:11,279 --> 04:10:14,939 this is called postfix increment so this 5574 04:10:14,939 --> 04:10:17,220 is postfix because it's the end of our 5575 04:10:17,220 --> 04:10:20,100 variable and uh if you put the 5576 04:10:20,100 --> 04:10:23,040 incremental operator at the beginning of 5577 04:10:23,040 --> 04:10:25,080 our variable is called prefix oh 5578 04:10:25,080 --> 04:10:26,640 increment anyway 5579 04:10:26,640 --> 04:10:29,460 let's press Ctrl Z to have the code that 5580 04:10:29,460 --> 04:10:33,438 I had previously and the curly braces 5581 04:10:34,140 --> 04:10:36,180 now look what happens if I delete the 5582 04:10:36,180 --> 04:10:37,680 increment 5583 04:10:37,680 --> 04:10:39,600 if I don't increment all variable now 5584 04:10:39,600 --> 04:10:43,560 now uh let's just talk what is happening 5585 04:10:43,560 --> 04:10:45,620 because we don't increment the variable 5586 04:10:45,620 --> 04:10:48,000 and the variable always has the value of 5587 04:10:48,000 --> 04:10:50,100 zero this will always be true and it 5588 04:10:50,100 --> 04:10:51,840 will execute the code inside the current 5589 04:10:51,840 --> 04:10:54,000 places forever so we'll have what is 5590 04:10:54,000 --> 04:10:55,560 called an infinite Loop so if you run 5591 04:10:55,560 --> 04:10:57,920 this code 5592 04:11:01,260 --> 04:11:04,920 now you can see that it prints 0 forever 5593 04:11:04,920 --> 04:11:07,680 because this condition never ever has to 5594 04:11:07,680 --> 04:11:10,560 to to false so let's stop this by 5595 04:11:10,560 --> 04:11:12,720 pressing on this uh 5596 04:11:12,720 --> 04:11:16,979 Red Square so now the process the 5597 04:11:16,979 --> 04:11:19,080 program is terminated so this is why you 5598 04:11:19,080 --> 04:11:20,340 need to increment the value of our 5599 04:11:20,340 --> 04:11:23,580 number because we want that condition to 5600 04:11:23,580 --> 04:11:27,180 be false at some point in the in the in 5601 04:11:27,180 --> 04:11:29,939 the in the future 5602 04:11:29,939 --> 04:11:31,140 next 5603 04:11:31,140 --> 04:11:35,340 let's say that I assign here 10. 5604 04:11:35,340 --> 04:11:37,439 so if I run this code now this condition 5605 04:11:37,439 --> 04:11:39,239 is going to be false because 10 is not 5606 04:11:39,239 --> 04:11:41,580 less than 10. and the code inside the 5607 04:11:41,580 --> 04:11:43,380 curly brace is not going to be executive 5608 04:11:43,380 --> 04:11:45,180 so now we have no output here because 5609 04:11:45,180 --> 04:11:47,460 this condition is evaluates to false and 5610 04:11:47,460 --> 04:11:49,380 is not executing the code inside the 5611 04:11:49,380 --> 04:11:50,760 calibration but there are certain 5612 04:11:50,760 --> 04:11:52,380 situations where you'll want to have 5613 04:11:52,380 --> 04:11:55,140 your code executed at least once even 5614 04:11:55,140 --> 04:11:57,120 though the initial condition the initial 5615 04:11:57,120 --> 04:11:59,699 uh condition or there is expression is 5616 04:11:59,699 --> 04:12:02,340 false so if I delete the curly braces 5617 04:12:02,340 --> 04:12:03,840 and the code inside the curly braces 5618 04:12:03,840 --> 04:12:05,819 here 5619 04:12:05,819 --> 04:12:10,439 now to to do what I said if to execute 5620 04:12:10,439 --> 04:12:12,359 the the code even though the condition 5621 04:12:12,359 --> 04:12:15,720 is initially set to false to execute the 5622 04:12:15,720 --> 04:12:17,640 code at least once you need to use a do 5623 04:12:17,640 --> 04:12:19,080 while loop and to use a do while loop 5624 04:12:19,080 --> 04:12:21,359 you put here at do press enter to add 5625 04:12:21,359 --> 04:12:24,779 the calibrases and we put our while at 5626 04:12:24,779 --> 04:12:27,600 the end of of our enclosing curly brace 5627 04:12:27,600 --> 04:12:30,600 of Urdu and here inside the calibration 5628 04:12:30,600 --> 04:12:32,399 or I'm going to put our printerland 5629 04:12:32,399 --> 04:12:34,199 function 5630 04:12:34,199 --> 04:12:37,920 and I'm gonna output the value of our 5631 04:12:37,920 --> 04:12:41,060 variable number 5632 04:12:42,300 --> 04:12:45,500 so now if you run this 5633 04:12:48,540 --> 04:12:52,080 now get 10 because this executes the 5634 04:12:52,080 --> 04:12:54,300 code inside the curly braces until at 5635 04:12:54,300 --> 04:12:58,680 least once and after that it comes down 5636 04:12:58,680 --> 04:13:00,060 here and it checks to see if this 5637 04:13:00,060 --> 04:13:02,520 condition is true or false and if this 5638 04:13:02,520 --> 04:13:05,460 condition is true it will repeat the 5639 04:13:05,460 --> 04:13:06,600 code 5640 04:13:06,600 --> 04:13:09,420 but if the condition is false it uh it 5641 04:13:09,420 --> 04:13:11,399 will not repeat the code but the 5642 04:13:11,399 --> 04:13:13,939 important aspect here is that it will 5643 04:13:13,939 --> 04:13:16,500 execute the code inside the curly braces 5644 04:13:16,500 --> 04:13:19,680 at least once irrespective of our 5645 04:13:19,680 --> 04:13:22,020 condition if the condition is true of 5646 04:13:22,020 --> 04:13:24,239 order false in our case is false 5647 04:13:24,239 --> 04:13:26,819 so these are the three Loops that you 5648 04:13:26,819 --> 04:13:29,100 can use in a kotlin the for Loop the 5649 04:13:29,100 --> 04:13:30,960 while loop and the do while loop now 5650 04:13:30,960 --> 04:13:33,840 it's time to see how we can use the 5651 04:13:33,840 --> 04:13:36,960 continue and the break keywords with 5652 04:13:36,960 --> 04:13:39,840 those Loops so let's press Ctrl Z to 5653 04:13:39,840 --> 04:13:42,239 undo the code have the while loop back 5654 04:13:42,239 --> 04:13:44,040 because I'm going to start the while 5655 04:13:44,040 --> 04:13:46,920 loop and to see how we can use the but 5656 04:13:46,920 --> 04:13:48,779 you can continue cured with the while 5657 04:13:48,779 --> 04:13:50,819 loop 5658 04:13:50,819 --> 04:13:53,819 let's bring this up a little bit 5659 04:13:53,819 --> 04:13:55,439 so let's first look at the continue 5660 04:13:55,439 --> 04:13:57,479 keyword and the continue keyword can be 5661 04:13:57,479 --> 04:14:00,660 used to bypass a section of code so 5662 04:14:00,660 --> 04:14:03,000 let's say that I want to skip the number 5663 04:14:03,000 --> 04:14:08,340 seven in our Loop so I can type here if 5664 04:14:08,340 --> 04:14:10,739 our variable number 5665 04:14:10,739 --> 04:14:14,520 equals 2 so 2 equals seven let's put 5666 04:14:14,520 --> 04:14:16,560 curly braces press enter toward the 5667 04:14:16,560 --> 04:14:19,620 right calibrase continue 5668 04:14:19,620 --> 04:14:22,380 so now what this is gonna do is when it 5669 04:14:22,380 --> 04:14:25,020 is gonna reach the number seven and this 5670 04:14:25,020 --> 04:14:26,100 condition is going to be true it's gonna 5671 04:14:26,100 --> 04:14:28,080 call continue and it's going to bypass 5672 04:14:28,080 --> 04:14:30,960 the code that comes below of our if 5673 04:14:30,960 --> 04:14:34,460 statement so if you run this code 5674 04:14:34,620 --> 04:14:37,880 look uh what happens 5675 04:14:39,720 --> 04:14:43,140 it happens nothing because uh here our 5676 04:14:43,140 --> 04:14:45,180 number has a value of 10 so this 5677 04:14:45,180 --> 04:14:46,680 condition is false so the code is not 5678 04:14:46,680 --> 04:14:49,500 executed so let's put here zero 5679 04:14:49,500 --> 04:14:52,699 now if you run this code 5680 04:14:56,399 --> 04:15:01,199 so we get 0 1 2 3 4 5 and 6 but uh 5681 04:15:01,199 --> 04:15:03,600 and you see that I save at seven it uh 5682 04:15:03,600 --> 04:15:05,760 it skips the code but why I don't have 5683 04:15:05,760 --> 04:15:08,580 the next numbers this is because uh 5684 04:15:08,580 --> 04:15:10,560 unintentionally we created here an 5685 04:15:10,560 --> 04:15:12,899 infinite look so let's stop this and uh 5686 04:15:12,899 --> 04:15:15,319 to explain 5687 04:15:15,359 --> 04:15:18,060 so what happens here is that because 5688 04:15:18,060 --> 04:15:20,880 um we said we say here explicit to 5689 04:15:20,880 --> 04:15:23,399 you to check here explicit we check here 5690 04:15:23,399 --> 04:15:25,080 to see if our number is equal to seven 5691 04:15:25,080 --> 04:15:26,939 then we're gonna continue and in other 5692 04:15:26,939 --> 04:15:28,859 words you're gonna skip the code which 5693 04:15:28,859 --> 04:15:30,779 comes below of our 5694 04:15:30,779 --> 04:15:34,620 of order is the then statement then this 5695 04:15:34,620 --> 04:15:37,380 code is never called so this code is 5696 04:15:37,380 --> 04:15:39,479 never called the printerland and more 5697 04:15:39,479 --> 04:15:42,239 importantly the increment operator is 5698 04:15:42,239 --> 04:15:45,359 now never called on our number variable 5699 04:15:45,359 --> 04:15:47,220 and because of that 5700 04:15:47,220 --> 04:15:49,500 because our variable is not incremented 5701 04:15:49,500 --> 04:15:51,840 here either when it's gonna loop again 5702 04:15:51,840 --> 04:15:56,340 it will have again the value 7. so this 5703 04:15:56,340 --> 04:15:59,520 condition or the in the if is going to 5704 04:15:59,520 --> 04:16:02,279 evaluate again to true and it's going to 5705 04:16:02,279 --> 04:16:04,859 call continue again and because this 5706 04:16:04,859 --> 04:16:07,859 code again is never reached Discord and 5707 04:16:07,859 --> 04:16:10,199 the values no not not incremented it 5708 04:16:10,199 --> 04:16:13,080 will still have the value seven so this 5709 04:16:13,080 --> 04:16:15,660 is gonna evaluate again to true so it 5710 04:16:15,660 --> 04:16:18,660 does this forever and to change that we 5711 04:16:18,660 --> 04:16:20,340 need to put our 5712 04:16:20,340 --> 04:16:22,140 number 5713 04:16:22,140 --> 04:16:25,859 and which is incremented before our reef 5714 04:16:25,859 --> 04:16:27,660 and that is going to solve the problem 5715 04:16:27,660 --> 04:16:29,939 so now if I 5716 04:16:29,939 --> 04:16:32,040 Let's uh 5717 04:16:32,040 --> 04:16:34,520 now 5718 04:16:37,859 --> 04:16:42,439 if I run the code let's increase this 5719 04:16:51,060 --> 04:16:53,939 now get one two three four five 5720 04:16:53,939 --> 04:16:58,020 and seven is skipped then 8 9 and 10. 5721 04:16:58,020 --> 04:17:00,600 because now when this condition level is 5722 04:17:00,600 --> 04:17:02,399 to true it will continue it's not going 5723 04:17:02,399 --> 04:17:04,680 to print the code it's not going to be 5724 04:17:04,680 --> 04:17:07,859 executed below of our if and it will 5725 04:17:07,859 --> 04:17:10,020 come back to Loop and now it's going to 5726 04:17:10,020 --> 04:17:11,340 increment the value and the value is 5727 04:17:11,340 --> 04:17:12,359 going to be 5728 04:17:12,359 --> 04:17:15,540 8 instead of 7 this is going to evaluate 5729 04:17:15,540 --> 04:17:17,340 to false and the continue is not going 5730 04:17:17,340 --> 04:17:19,739 to be called it will print the number 5731 04:17:19,739 --> 04:17:22,500 eight and it will do the same for it it 5732 04:17:22,500 --> 04:17:24,720 will increment the value again and then 5733 04:17:24,720 --> 04:17:27,779 we'll have 9 and so on so this is how 5734 04:17:27,779 --> 04:17:29,939 you can use the continue keyword and you 5735 04:17:29,939 --> 04:17:31,500 have to be careful where you put your in 5736 04:17:31,500 --> 04:17:34,199 uh your whatever incremental variable 5737 04:17:34,199 --> 04:17:36,720 because I you saw you can easily get 5738 04:17:36,720 --> 04:17:38,399 intro infinite Loop 5739 04:17:38,399 --> 04:17:40,859 now you can have a more complex 5740 04:17:40,859 --> 04:17:43,140 expression here so you can put here if 5741 04:17:43,140 --> 04:17:48,000 number is greater than let's say 2 and 5742 04:17:48,000 --> 04:17:49,920 number 5743 04:17:49,920 --> 04:17:52,500 is less than eight 5744 04:17:52,500 --> 04:17:54,239 now if you run this 5745 04:17:54,239 --> 04:17:57,020 look what happens 5746 04:17:57,120 --> 04:18:00,479 now get one two and then we get 8 9 and 5747 04:18:00,479 --> 04:18:03,120 10 because what this condition now 5748 04:18:03,120 --> 04:18:07,500 checks is is if our number is greater 5749 04:18:07,500 --> 04:18:10,620 than 10 and less than 8 then continue 5750 04:18:10,620 --> 04:18:13,380 then skip that number so this is why the 5751 04:18:13,380 --> 04:18:16,620 numbers between 2 and 8 are skipped 5752 04:18:16,620 --> 04:18:20,880 because we call continue on on them and 5753 04:18:20,880 --> 04:18:22,859 if you hover over here you see that it 5754 04:18:22,859 --> 04:18:24,840 says the two comparison should be 5755 04:18:24,840 --> 04:18:26,580 converted to a rain check so you can 5756 04:18:26,580 --> 04:18:28,680 write the same condition that have here 5757 04:18:28,680 --> 04:18:32,100 using a range but this is different from 5758 04:18:32,100 --> 04:18:35,699 a range that you you've used in a for 5759 04:18:35,699 --> 04:18:37,739 Loop in the sense that this is now 5760 04:18:37,739 --> 04:18:40,560 checking to see if our number is in the 5761 04:18:40,560 --> 04:18:42,720 range 3 5762 04:18:42,720 --> 04:18:47,060 from 3 to 7 so if you run the squad 5763 04:18:50,160 --> 04:18:52,699 now I have the exact same output but now 5764 04:18:52,699 --> 04:18:56,399 we are using the range to check to see 5765 04:18:56,399 --> 04:18:58,199 if our number is in the other engine if 5766 04:18:58,199 --> 04:19:01,199 this is the underage we call continue to 5767 04:19:01,199 --> 04:19:02,880 skip that 5768 04:19:02,880 --> 04:19:03,720 um 5769 04:19:03,720 --> 04:19:05,460 the next thing that you can do is you 5770 04:19:05,460 --> 04:19:06,779 can 5771 04:19:06,779 --> 04:19:09,840 break the loop you can stop the loop 5772 04:19:09,840 --> 04:19:12,840 using the break here so I can say here 5773 04:19:12,840 --> 04:19:14,460 again if number 5774 04:19:14,460 --> 04:19:16,800 equals equals let's say seven 5775 04:19:16,800 --> 04:19:18,420 call break 5776 04:19:18,420 --> 04:19:20,819 so I'm going to type here break 5777 04:19:20,819 --> 04:19:22,920 now when this condition is going to vary 5778 04:19:22,920 --> 04:19:25,020 to true it's gonna call break and the 5779 04:19:25,020 --> 04:19:26,520 loop is going to stop it's not going to 5780 04:19:26,520 --> 04:19:28,739 execute any line of code after that it's 5781 04:19:28,739 --> 04:19:30,300 gonna stop it's gonna leave and it's 5782 04:19:30,300 --> 04:19:31,979 going to execute the code which comes 5783 04:19:31,979 --> 04:19:33,540 below here but in our case we don't 5784 04:19:33,540 --> 04:19:35,340 don't have any code 5785 04:19:35,340 --> 04:19:37,560 so you see that we have one two three 5786 04:19:37,560 --> 04:19:40,340 four five and six and when it reaches 5787 04:19:40,340 --> 04:19:44,279 seven it breaks and it nuts is stopping 5788 04:19:44,279 --> 04:19:46,859 the loop it's not printing the value and 5789 04:19:46,859 --> 04:19:49,560 it executes the loop so it terminates so 5790 04:19:49,560 --> 04:19:51,739 foreign 5791 04:19:53,279 --> 04:19:55,140 and we can do the same thing with the 5792 04:19:55,140 --> 04:19:57,359 for Loop so we can use the four we can 5793 04:19:57,359 --> 04:19:59,640 use the continue and 5794 04:19:59,640 --> 04:20:01,319 and break with a for Loop so you can 5795 04:20:01,319 --> 04:20:03,120 type here four 5796 04:20:03,120 --> 04:20:07,739 let's say I in let's create the 5797 04:20:07,739 --> 04:20:09,779 let's say 5798 04:20:09,779 --> 04:20:12,359 zero 5799 04:20:12,359 --> 04:20:15,979 to 10 let's create this range 5800 04:20:16,020 --> 04:20:18,120 let's add the curly braces 5801 04:20:18,120 --> 04:20:22,460 now I can say here if 5802 04:20:23,939 --> 04:20:26,660 I 5803 04:20:28,260 --> 04:20:29,819 in 5804 04:20:29,819 --> 04:20:32,220 let's say three 5805 04:20:32,220 --> 04:20:34,819 to eight 5806 04:20:37,020 --> 04:20:39,800 then continue 5807 04:20:40,199 --> 04:20:42,300 let's put in the volume 5808 04:20:42,300 --> 04:20:45,619 let's type here println 5809 04:20:46,979 --> 04:20:50,660 let's print the value of I here 5810 04:20:51,180 --> 04:20:54,319 so now if you run this code 5811 04:21:01,739 --> 04:21:05,460 so we have our first Loop which Loops a 5812 04:21:05,460 --> 04:21:08,580 to six and then it breaks because you 5813 04:21:08,580 --> 04:21:11,220 call break there then it's using the for 5814 04:21:11,220 --> 04:21:14,279 Loop it Loops it loops 5815 04:21:14,279 --> 04:21:18,540 from 0 to 10 but because here we're 5816 04:21:18,540 --> 04:21:21,840 checking to see if our number is in the 5817 04:21:21,840 --> 04:21:24,660 range three to eight and skip it you get 5818 04:21:24,660 --> 04:21:29,640 0 1 2 and then the numbers uh the three 5819 04:21:29,640 --> 04:21:33,080 and uh the numbers between the three and 5820 04:21:33,080 --> 04:21:35,699 the numbers between 3 and 8 inclusive 5821 04:21:35,699 --> 04:21:40,080 are skipped here so we have 0 1 2 and 5822 04:21:40,080 --> 04:21:42,479 then the numbers between two and nine 5823 04:21:42,479 --> 04:21:45,660 are skipped and we have nine and ten 5824 04:21:45,660 --> 04:21:48,000 and we can use also the brake keyword 5825 04:21:48,000 --> 04:21:50,819 with the four I can put here if I equals 5826 04:21:50,819 --> 04:21:54,080 equals seven 5827 04:21:56,699 --> 04:21:57,460 break 5828 04:21:57,460 --> 04:21:59,460 [Music] 5829 04:21:59,460 --> 04:22:02,778 so now if you run this code 5830 04:22:06,600 --> 04:22:09,899 now you get 0 1 2 3 4 5 6 and when it 5831 04:22:09,899 --> 04:22:12,479 reaches seven it breaks similar to our 5832 04:22:12,479 --> 04:22:15,239 while loop so this is how you can use 5833 04:22:15,239 --> 04:22:17,939 the break and continue cured with the 5834 04:22:17,939 --> 04:22:21,359 while and for Loop and you can use the 5835 04:22:21,359 --> 04:22:23,699 brake and continue cured also with the 5836 04:22:23,699 --> 04:22:25,260 Dual Loop 5837 04:22:25,260 --> 04:22:27,180 next we're gonna look at how you can 5838 04:22:27,180 --> 04:22:29,460 Nest Loops within each others and we're 5839 04:22:29,460 --> 04:22:30,960 going to look at an example using the 5840 04:22:30,960 --> 04:22:32,340 while loop so I'm going to delete the 5841 04:22:32,340 --> 04:22:34,199 code of the for Loop and the printable 5842 04:22:34,199 --> 04:22:35,760 statement there 5843 04:22:35,760 --> 04:22:37,500 I'm going to delete the if then 5844 04:22:37,500 --> 04:22:39,540 statement the break yard and I'm going 5845 04:22:39,540 --> 04:22:41,880 to bring the printerland function up 5846 04:22:41,880 --> 04:22:43,140 there 5847 04:22:43,140 --> 04:22:44,580 down here I'm going to declare a 5848 04:22:44,580 --> 04:22:46,560 variable it's going to be a VAR I'm 5849 04:22:46,560 --> 04:22:48,840 going to call it I and I'm gonna assign 5850 04:22:48,840 --> 04:22:51,960 a value of 0 to it now in inside the 5851 04:22:51,960 --> 04:22:53,220 while loop I'm going to type another 5852 04:22:53,220 --> 04:22:55,140 while loop here I'm going to put a 5853 04:22:55,140 --> 04:22:59,300 conditional while I is less than 5 5854 04:22:59,699 --> 04:23:02,819 we're gonna increment I and we're gonna 5855 04:23:02,819 --> 04:23:04,979 print and output the value of I in the 5856 04:23:04,979 --> 04:23:07,460 console but let's put some text here to 5857 04:23:07,460 --> 04:23:11,160 make uh clear what Loops what Loop is 5858 04:23:11,160 --> 04:23:13,140 looping so I'm going to put three 5859 04:23:13,140 --> 04:23:15,000 asterisk signs here 5860 04:23:15,000 --> 04:23:19,399 I'm gonna type the dollar sign and I 5861 04:23:20,460 --> 04:23:23,160 so now if you run this code let's also 5862 04:23:23,160 --> 04:23:24,899 change this condition too 5863 04:23:24,899 --> 04:23:27,920 less than five 5864 04:23:28,920 --> 04:23:32,060 so if you run this code 5865 04:23:39,680 --> 04:23:42,960 now we get first you get one then we get 5866 04:23:42,960 --> 04:23:46,500 our code inside of our inner while loop 5867 04:23:46,500 --> 04:23:49,439 which is the three asterisks and get one 5868 04:23:49,439 --> 04:23:51,359 two three four five then it Loops again 5869 04:23:51,359 --> 04:23:54,060 but let's take this chord line by line 5870 04:23:54,060 --> 04:23:56,160 to explain what is happening here 5871 04:23:56,160 --> 04:23:58,080 so first decade is variable call number 5872 04:23:58,080 --> 04:24:00,120 assign a value of zero to it then we 5873 04:24:00,120 --> 04:24:02,880 Loop using the while loop the outer loop 5874 04:24:02,880 --> 04:24:04,380 and it checks to see this condition 5875 04:24:04,380 --> 04:24:06,840 issue and is true zero is less than five 5876 04:24:06,840 --> 04:24:09,840 so it executes the code inside the curly 5877 04:24:09,840 --> 04:24:12,300 braces and it increments the value of a 5878 04:24:12,300 --> 04:24:14,399 number by one then it outputs the value 5879 04:24:14,399 --> 04:24:16,260 of our number in the console so you get 5880 04:24:16,260 --> 04:24:17,340 here one 5881 04:24:17,340 --> 04:24:19,739 then it declared this variable called I 5882 04:24:19,739 --> 04:24:23,279 and then it starts looping inside our 5883 04:24:23,279 --> 04:24:25,739 outer loop while using the inner loop 5884 04:24:25,739 --> 04:24:28,680 while so it Loops here five times 5885 04:24:28,680 --> 04:24:31,859 so when it finishes it goes back 5886 04:24:31,859 --> 04:24:34,020 and now the value was incremented by one 5887 04:24:34,020 --> 04:24:35,880 it will check again if the condition is 5888 04:24:35,880 --> 04:24:39,180 true it will uh execute the code inside 5889 04:24:39,180 --> 04:24:40,880 the calibration to increment the value 5890 04:24:40,880 --> 04:24:44,399 now so the it will have two it will 5891 04:24:44,399 --> 04:24:46,020 output that value so now we have two 5892 04:24:46,020 --> 04:24:47,160 here 5893 04:24:47,160 --> 04:24:50,279 and then it will loop again five star 5894 04:24:50,279 --> 04:24:53,040 five times using the inner loop and it 5895 04:24:53,040 --> 04:24:55,500 will do the same thing for the next 5896 04:24:55,500 --> 04:24:57,060 round and the next round until you get 5897 04:24:57,060 --> 04:24:58,080 to 5898 04:24:58,080 --> 04:25:00,000 5. when 5899 04:25:00,000 --> 04:25:03,060 we'll get to five five will be not be 5900 04:25:03,060 --> 04:25:05,279 less than five and this condition will 5901 04:25:05,279 --> 04:25:07,439 evaluate to to false and the code inside 5902 04:25:07,439 --> 04:25:10,260 the caliber is not going to be executed 5903 04:25:10,260 --> 04:25:12,739 right 5904 04:25:12,840 --> 04:25:15,420 and you can use the break keyword inside 5905 04:25:15,420 --> 04:25:19,739 the inner loop so I can say here if 5906 04:25:19,739 --> 04:25:21,779 you can put it above here so you can say 5907 04:25:21,779 --> 04:25:23,760 if 5908 04:25:23,760 --> 04:25:28,140 I equals equals zero 5909 04:25:28,140 --> 04:25:30,420 then break and now you can put the break 5910 04:25:30,420 --> 04:25:32,939 and continue cures in one single line if 5911 04:25:32,939 --> 04:25:36,680 you want you can avoid the curly braces 5912 04:25:36,920 --> 04:25:40,819 so now if you run this code 5913 04:25:46,620 --> 04:25:49,920 now get one two three four five because 5914 04:25:49,920 --> 04:25:53,279 now uh when it reaches the inner loop it 5915 04:25:53,279 --> 04:25:56,520 breaks immediately what every at every 5916 04:25:56,520 --> 04:26:00,120 iteration in the outer loop so when it 5917 04:26:00,120 --> 04:26:02,520 comes down here it will check to see if 5918 04:26:02,520 --> 04:26:03,899 the conditions through is going to be 5919 04:26:03,899 --> 04:26:05,520 true but when it gets to the if 5920 04:26:05,520 --> 04:26:07,560 statement it will break it will stop the 5921 04:26:07,560 --> 04:26:11,100 loop so it will execute only the Outer 5922 04:26:11,100 --> 04:26:12,239 Loop 5923 04:26:12,239 --> 04:26:15,659 and with a break keyword you can 5924 04:26:15,659 --> 04:26:19,500 not not Target only the loop so with 5925 04:26:19,500 --> 04:26:21,960 with this break keyword we stop you stop 5926 04:26:21,960 --> 04:26:25,859 only this while loop that uh 5927 04:26:25,859 --> 04:26:29,159 that we are currently that is currently 5928 04:26:29,159 --> 04:26:31,500 the nearest Loop but there is something 5929 04:26:31,500 --> 04:26:33,359 which which is called labels and with 5930 04:26:33,359 --> 04:26:35,880 labels you can Target an outer loop so 5931 04:26:35,880 --> 04:26:38,340 it can break the outer loop here so you 5932 04:26:38,340 --> 04:26:40,800 can break the while loop which is here 5933 04:26:40,800 --> 04:26:43,739 and to do that we we give to our while 5934 04:26:43,739 --> 04:26:46,319 loop a label so we type here a name I'm 5935 04:26:46,319 --> 04:26:50,220 going to call it outer and we put at 5936 04:26:50,220 --> 04:26:54,000 now here you can put break and now I'm 5937 04:26:54,000 --> 04:26:55,859 gonna type 5938 04:26:55,859 --> 04:26:57,120 add 5939 04:26:57,120 --> 04:27:00,000 and you put outer and this this should 5940 04:27:00,000 --> 04:27:01,800 be a single word 5941 04:27:01,800 --> 04:27:06,319 now if you run this look what happens 5942 04:27:11,699 --> 04:27:14,399 now get one because it breaks the outer 5943 04:27:14,399 --> 04:27:16,620 loop so it first evolves this condition 5944 04:27:16,620 --> 04:27:19,020 it increments the value of our number it 5945 04:27:19,020 --> 04:27:22,380 prints the value of a number and then it 5946 04:27:22,380 --> 04:27:24,479 goes in the Inner Loop it checks to see 5947 04:27:24,479 --> 04:27:26,520 if this condition is true it's true then 5948 04:27:26,520 --> 04:27:28,620 it's checked to see if I is equal to 0 5949 04:27:28,620 --> 04:27:31,800 which is uh true then it breaks but now 5950 04:27:31,800 --> 04:27:34,199 it's not breaking this Loop this inner 5951 04:27:34,199 --> 04:27:35,100 loop 5952 04:27:35,100 --> 04:27:38,760 it's breaking the Outer Loop so it's 5953 04:27:38,760 --> 04:27:40,560 breaking this Loop so this is why you 5954 04:27:40,560 --> 04:27:42,120 get only one and the code is not 5955 04:27:42,120 --> 04:27:43,800 executed because it breaks the outer 5956 04:27:43,800 --> 04:27:47,460 loop and the code our program or code 5957 04:27:47,460 --> 04:27:50,279 it's terminated because about down here 5958 04:27:50,279 --> 04:27:52,979 we don't have any code so this is how 5959 04:27:52,979 --> 04:27:54,600 you can use labels with the break 5960 04:27:54,600 --> 04:27:57,359 keyboard to Target an outer loop and the 5961 04:27:57,359 --> 04:27:59,520 labels can be used also with the 5962 04:27:59,520 --> 04:28:03,000 continue keyword and in other Loops that 5963 04:28:03,000 --> 04:28:05,100 we've looked at so this is our 5964 04:28:05,100 --> 04:28:08,040 discussion about loops what you can do I 5965 04:28:08,040 --> 04:28:09,380 should say is that you cannot Target 5966 04:28:09,380 --> 04:28:12,479 with the labels and inner loop from an 5967 04:28:12,479 --> 04:28:15,180 outer loop so you cannot 5968 04:28:15,180 --> 04:28:17,460 we can give here a name to a label to 5969 04:28:17,460 --> 04:28:19,680 our inner loop but you cannot Target our 5970 04:28:19,680 --> 04:28:22,979 inner from our outer loop so this is our 5971 04:28:22,979 --> 04:28:25,020 discussion about loops and the next 5972 04:28:25,020 --> 04:28:27,300 video we're going to do a challenge 5973 04:28:27,300 --> 04:28:29,159 all right so now it's time to do a 5974 04:28:29,159 --> 04:28:31,020 challenge using all the knowledge that 5975 04:28:31,020 --> 04:28:33,060 we have accumulated so far and I'm gonna 5976 04:28:33,060 --> 04:28:35,340 use the previous project to put my code 5977 04:28:35,340 --> 04:28:37,319 there but if you don't have the previous 5978 04:28:37,319 --> 04:28:39,479 project you can create a new project and 5979 04:28:39,479 --> 04:28:41,640 give it whatever name you want and type 5980 04:28:41,640 --> 04:28:43,140 your code there so I'm going to open my 5981 04:28:43,140 --> 04:28:44,640 previous project 5982 04:28:44,640 --> 04:28:47,279 I'm going to delete the code inside the 5983 04:28:47,279 --> 04:28:49,979 main function 5984 04:28:49,979 --> 04:28:53,358 now what is the challenge 5985 04:28:53,640 --> 04:28:55,680 the challenge is to 5986 04:28:55,680 --> 04:28:57,720 create an arbitrary range of numbers 5987 04:28:57,720 --> 04:29:00,120 then we need to find a way to go to that 5988 04:29:00,120 --> 04:29:02,279 arbitrary range of numbers 5989 04:29:02,279 --> 04:29:06,779 and to determine if a number is an even 5990 04:29:06,779 --> 04:29:09,420 number or if a number is another number 5991 04:29:09,420 --> 04:29:12,180 and based on that if the number is an 5992 04:29:12,180 --> 04:29:14,520 odd number skip that number 5993 04:29:14,520 --> 04:29:16,859 but if the number is an even number then 5994 04:29:16,859 --> 04:29:18,960 output a number in the console 5995 04:29:18,960 --> 04:29:21,120 additionally should create a way to 5996 04:29:21,120 --> 04:29:24,239 count all the even numbers that you 5997 04:29:24,239 --> 04:29:27,000 found in your range and display the 5998 04:29:27,000 --> 04:29:30,420 total number so if neither numbers 5999 04:29:30,420 --> 04:29:32,100 um at the end of your program 6000 04:29:32,100 --> 04:29:35,880 so try to do this solution and after 6001 04:29:35,880 --> 04:29:39,000 that you can watch my solution to this 6002 04:29:39,000 --> 04:29:40,739 so first I'm going to decide I'm going 6003 04:29:40,739 --> 04:29:43,140 to declare a variable call number 6004 04:29:43,140 --> 04:29:46,439 I'm going to assign a value of 1 to it 6005 04:29:46,439 --> 04:29:47,760 next I'm going to declare another 6006 04:29:47,760 --> 04:29:49,319 variable but this is going to be a Val 6007 04:29:49,319 --> 04:29:51,960 it's going to be called last number 6008 04:29:51,960 --> 04:29:54,060 and it's going to have a value of 20. 6009 04:29:54,060 --> 04:29:57,600 now I'm gonna Loop using the while loop 6010 04:29:57,600 --> 04:29:59,340 and in the parantes you're going to type 6011 04:29:59,340 --> 04:30:00,899 while number 6012 04:30:00,899 --> 04:30:04,819 is less than equal to last number 6013 04:30:04,920 --> 04:30:06,479 then you're going to put curly braces 6014 04:30:06,479 --> 04:30:10,698 now I'm going to increment our number 6015 04:30:11,460 --> 04:30:13,319 now here you need to add the logic to 6016 04:30:13,319 --> 04:30:15,120 determine if a number is an even number 6017 04:30:15,120 --> 04:30:16,979 or not and to do that I'm going to add 6018 04:30:16,979 --> 04:30:19,500 the function at the end of our enclosing 6019 04:30:19,500 --> 04:30:21,239 curly brace of our many functions so I'm 6020 04:30:21,239 --> 04:30:24,060 going to type here the fun keyword 6021 04:30:24,060 --> 04:30:25,800 now you need to give a name to our 6022 04:30:25,800 --> 04:30:30,239 function it's going to be called is even 6023 04:30:30,239 --> 04:30:32,399 number 6024 04:30:32,399 --> 04:30:34,199 I'm gonna put parenthesis and it's going 6025 04:30:34,199 --> 04:30:36,180 to take a parameter called number of 6026 04:30:36,180 --> 04:30:37,739 type int 6027 04:30:37,739 --> 04:30:40,560 this function and it's gonna return a 6028 04:30:40,560 --> 04:30:42,779 Boolean value true or false this 6029 04:30:42,779 --> 04:30:45,180 function so you put colon and we type 6030 04:30:45,180 --> 04:30:48,239 Boolean then you put curly braces and 6031 04:30:48,239 --> 04:30:49,920 now we'll add the logic to determine if 6032 04:30:49,920 --> 04:30:51,899 any number is even or not so you type 6033 04:30:51,899 --> 04:30:54,560 here if 6034 04:30:54,899 --> 04:30:56,939 and now inside the parenthesis we put 6035 04:30:56,939 --> 04:30:59,340 another set of parentheses 6036 04:30:59,340 --> 04:31:01,800 and here we refer our parameter number 6037 04:31:01,800 --> 04:31:04,859 that we have defined above and here we 6038 04:31:04,859 --> 04:31:07,500 put the modulus operator because the 6039 04:31:07,500 --> 04:31:09,300 modulus operator gives us the reminder 6040 04:31:09,300 --> 04:31:11,399 of a division so we put a modulus 6041 04:31:11,399 --> 04:31:13,680 operator 2. 6042 04:31:13,680 --> 04:31:16,680 now outside our inner parenthesis we put 6043 04:31:16,680 --> 04:31:20,100 equals equals zero then we put curly 6044 04:31:20,100 --> 04:31:21,479 braces 6045 04:31:21,479 --> 04:31:24,679 and return true 6046 04:31:25,319 --> 04:31:29,239 else we're gonna return 6047 04:31:30,479 --> 04:31:33,300 false now how this logic works 6048 04:31:33,300 --> 04:31:34,560 so 6049 04:31:34,560 --> 04:31:38,760 our num our number parameter which is 6050 04:31:38,760 --> 04:31:39,899 defined here it's going to take an 6051 04:31:39,899 --> 04:31:41,880 argument let's say that it's going to 6052 04:31:41,880 --> 04:31:44,279 take an arbitrary number 6053 04:31:44,279 --> 04:31:46,319 then he's gonna check to see if that 6054 04:31:46,319 --> 04:31:49,500 number divided by 2 has no reminder if 6055 04:31:49,500 --> 04:31:51,720 it's and if it has no reminder then that 6056 04:31:51,720 --> 04:31:53,460 number is an even number because the 6057 04:31:53,460 --> 04:31:55,620 because even numbers when they are 6058 04:31:55,620 --> 04:31:58,620 divided by two they have no reminder 6059 04:31:58,620 --> 04:32:01,199 and if that is the case if the reminder 6060 04:32:01,199 --> 04:32:04,500 equals equals to zero our number is an 6061 04:32:04,500 --> 04:32:06,180 even number and it's gonna return true 6062 04:32:06,180 --> 04:32:07,680 and their spot is not going to be 6063 04:32:07,680 --> 04:32:08,819 executed 6064 04:32:08,819 --> 04:32:12,600 now let's add the logic here 6065 04:32:12,600 --> 04:32:15,060 list user function here because as you 6066 04:32:15,060 --> 04:32:17,220 can see now it's uh gray out it's not 6067 04:32:17,220 --> 04:32:20,040 used so let's type here if 6068 04:32:20,040 --> 04:32:22,020 and in the parenthesis you're gonna type 6069 04:32:22,020 --> 04:32:24,600 is even number our functions we're going 6070 04:32:24,600 --> 04:32:26,220 to call our function here and we're 6071 04:32:26,220 --> 04:32:29,040 going to pass a parameter to it called 6072 04:32:29,040 --> 04:32:32,840 now I'm going to pass the parameter 6073 04:32:33,600 --> 04:32:35,520 we're going to pass an argument and the 6074 04:32:35,520 --> 04:32:36,720 argument is going to be the variable 6075 04:32:36,720 --> 04:32:38,939 that we Define above as you can see you 6076 04:32:38,939 --> 04:32:41,460 have the V there so press enter to fill 6077 04:32:41,460 --> 04:32:43,859 on that for you let's press Ctrl alt L 6078 04:32:43,859 --> 04:32:45,899 to format the code 6079 04:32:45,899 --> 04:32:47,279 now 6080 04:32:47,279 --> 04:32:49,979 if we press shift Ctrl p on this you're 6081 04:32:49,979 --> 04:32:51,120 going to see that this is a Boolean 6082 04:32:51,120 --> 04:32:53,159 expression because this returns true or 6083 04:32:53,159 --> 04:32:55,020 false so we can put the logical not 6084 04:32:55,020 --> 04:32:57,359 operator in front of it to change its 6085 04:32:57,359 --> 04:32:59,159 value to switch his value from True to 6086 04:32:59,159 --> 04:33:02,760 false and from and from false to true 6087 04:33:02,760 --> 04:33:05,340 so let's put curly braces 6088 04:33:05,340 --> 04:33:07,500 and here we're gonna call our continue 6089 04:33:07,500 --> 04:33:11,039 quick keyword down here I'm gonna add 6090 04:33:11,039 --> 04:33:12,900 a printerland 6091 04:33:12,900 --> 04:33:15,840 which is going to Output the the number 6092 04:33:15,840 --> 04:33:18,419 in our console 6093 04:33:18,419 --> 04:33:21,539 next we need to add the logic to to 6094 04:33:21,539 --> 04:33:24,419 count the total uh the total even 6095 04:33:24,419 --> 04:33:26,520 numbers which are found so I'm gonna 6096 04:33:26,520 --> 04:33:29,160 declare a VAR it's going to be 6097 04:33:29,160 --> 04:33:32,938 even numbers 6098 04:33:34,561 --> 04:33:37,020 counter and I'm going to assign a value 6099 04:33:37,020 --> 04:33:39,539 0 to it and here we're gonna increment 6100 04:33:39,539 --> 04:33:43,219 our even numbers counter 6101 04:33:43,500 --> 04:33:47,118 so now if you run this code 6102 04:33:52,680 --> 04:33:57,599 we get 2 4 6 8 10 12 14 16 18 and 20. so 6103 04:33:57,599 --> 04:33:59,580 these are the even numbers that they are 6104 04:33:59,580 --> 04:34:01,680 that they found that the our program 6105 04:34:01,680 --> 04:34:03,778 found in is correct but let's add a 6106 04:34:03,778 --> 04:34:05,820 print line at the end of our Loop to 6107 04:34:05,820 --> 04:34:08,278 print the total number of numbers of 6108 04:34:08,278 --> 04:34:11,219 even numbers found so let's put println 6109 04:34:11,219 --> 04:34:13,859 here and let's put let's refer our even 6110 04:34:13,859 --> 04:34:17,480 numbers count here 6111 04:34:18,180 --> 04:34:20,400 you have a numbers counter so if you run 6112 04:34:20,400 --> 04:34:22,641 this 6113 04:34:26,160 --> 04:34:28,680 let's put some text actually let's put 6114 04:34:28,680 --> 04:34:31,320 this in curly braces so let's type here 6115 04:34:31,320 --> 04:34:32,879 total 6116 04:34:32,879 --> 04:34:36,439 number of even 6117 04:34:36,840 --> 04:34:39,480 numbers found 6118 04:34:39,480 --> 04:34:41,458 equals 6119 04:34:41,458 --> 04:34:44,239 dollar sign 6120 04:34:47,580 --> 04:34:50,719 now if you run this 6121 04:34:55,859 --> 04:34:57,719 now you get total numbers of you know 6122 04:34:57,719 --> 04:34:59,400 when even numbers found this 10 and this 6123 04:34:59,400 --> 04:35:03,000 is correct so in an output correctly or 6124 04:35:03,000 --> 04:35:05,879 even numbers and it show that the total 6125 04:35:05,879 --> 04:35:08,039 number two newer numbers found this 10. 6126 04:35:08,039 --> 04:35:11,520 so let's now think about how this logic 6127 04:35:11,520 --> 04:35:13,500 works here 6128 04:35:13,500 --> 04:35:16,320 so let's look at the if statement that 6129 04:35:16,320 --> 04:35:18,480 we have here so what is doing in this 6130 04:35:18,480 --> 04:35:21,660 thing here so let's take an um an um 6131 04:35:21,660 --> 04:35:24,419 specific example a specific number to 6132 04:35:24,419 --> 04:35:26,879 store to see how this works so let's see 6133 04:35:26,879 --> 04:35:29,219 that here we pass four and it's gonna 6134 04:35:29,219 --> 04:35:31,141 use the logic to determine if this 6135 04:35:31,141 --> 04:35:33,359 number is an even number and if that 6136 04:35:33,359 --> 04:35:34,980 number is any of a number is going to 6137 04:35:34,980 --> 04:35:38,660 return true so this 6138 04:35:38,958 --> 04:35:41,160 this function which is a Boolean 6139 04:35:41,160 --> 04:35:44,160 expression is going to return true and 6140 04:35:44,160 --> 04:35:46,379 then we're gonna negate this value gonna 6141 04:35:46,379 --> 04:35:48,958 change this value from True to false and 6142 04:35:48,958 --> 04:35:52,080 continuously not gonna be called and the 6143 04:35:52,080 --> 04:35:54,900 code which comes below which is uh our 6144 04:35:54,900 --> 04:35:58,799 variable which uh counts how many even 6145 04:35:58,799 --> 04:36:03,180 numbers uh we have then it it increments 6146 04:36:03,180 --> 04:36:04,980 then it outputs that number in the 6147 04:36:04,980 --> 04:36:07,379 console so we have our even number four 6148 04:36:07,379 --> 04:36:10,799 output output it in the console 6149 04:36:10,799 --> 04:36:14,699 but if you pass here 3 that the number 6150 04:36:14,699 --> 04:36:16,080 is not going to be an even number 6151 04:36:16,080 --> 04:36:18,599 because has a reminder and this is going 6152 04:36:18,599 --> 04:36:22,320 to return uh false and because of here 6153 04:36:22,320 --> 04:36:23,938 you put the logical not operator that is 6154 04:36:23,938 --> 04:36:25,799 going to be switched to true 6155 04:36:25,799 --> 04:36:27,778 and the continue that's going to 6156 04:36:27,778 --> 04:36:29,340 continue keyword is going to be called 6157 04:36:29,340 --> 04:36:32,340 and our code Channel which comes below 6158 04:36:32,340 --> 04:36:34,799 here which is uh our variable even 6159 04:36:34,799 --> 04:36:36,180 number counter is not going to be 6160 04:36:36,180 --> 04:36:37,500 incremented 6161 04:36:37,500 --> 04:36:40,561 and our number respect to our number 6162 04:36:40,561 --> 04:36:43,020 three now it's not going to be outputted 6163 04:36:43,020 --> 04:36:46,199 in the console so this is how this works 6164 04:36:46,199 --> 04:36:50,400 so this is our solution to our 6165 04:36:50,400 --> 04:36:52,320 to our challenge of course you can 6166 04:36:52,320 --> 04:36:54,599 simplify this so if you hover over here 6167 04:36:54,599 --> 04:36:56,520 because you have all of those underlines 6168 04:36:56,520 --> 04:36:58,680 you can see that says remove redundancy 6169 04:36:58,680 --> 04:37:00,000 if statement 6170 04:37:00,000 --> 04:37:02,458 so this can be simplified to this this 6171 04:37:02,458 --> 04:37:05,039 does the exact same thing it is going to 6172 04:37:05,039 --> 04:37:08,759 return true if this uh divided by 2 has 6173 04:37:08,759 --> 04:37:10,199 no reminder otherwise it's going to 6174 04:37:10,199 --> 04:37:12,480 return false and this can can be 6175 04:37:12,480 --> 04:37:15,118 simplified even further because if here 6176 04:37:15,118 --> 04:37:17,759 we have a single expression and we know 6177 04:37:17,759 --> 04:37:19,500 when we have a single expression we can 6178 04:37:19,500 --> 04:37:21,240 write our function as a single 6179 04:37:21,240 --> 04:37:23,699 expression function so I can remove the 6180 04:37:23,699 --> 04:37:25,680 curly braces here 6181 04:37:25,680 --> 04:37:27,958 you can remove 6182 04:37:27,958 --> 04:37:30,778 the bully return type you can remove the 6183 04:37:30,778 --> 04:37:32,340 return keyword 6184 04:37:32,340 --> 04:37:34,980 I'm gonna put the equal zero and this 6185 04:37:34,980 --> 04:37:37,438 now if you run this code 6186 04:37:37,438 --> 04:37:40,618 this will work 6187 04:37:40,618 --> 04:37:43,218 the same 6188 04:37:43,259 --> 04:37:46,199 so we have the same output here 6189 04:37:46,199 --> 04:37:48,539 now I use the function here because 6190 04:37:48,539 --> 04:37:51,118 we've talked about functions in our 6191 04:37:51,118 --> 04:37:53,699 previous videos but you can remove the 6192 04:37:53,699 --> 04:37:56,699 function completely and add the logic to 6193 04:37:56,699 --> 04:37:59,278 determine if a new number is even or not 6194 04:37:59,278 --> 04:38:01,740 directly in the if statement so you can 6195 04:38:01,740 --> 04:38:03,840 put here 6196 04:38:03,840 --> 04:38:06,660 if 6197 04:38:06,660 --> 04:38:10,799 let's put parenthesis our number 6198 04:38:10,799 --> 04:38:12,599 now we're going to use the modulus 6199 04:38:12,599 --> 04:38:15,180 operator tool 6200 04:38:15,180 --> 04:38:17,520 not equal 6201 04:38:17,520 --> 04:38:19,020 to zero 6202 04:38:19,020 --> 04:38:21,359 then continue and this will have the 6203 04:38:21,359 --> 04:38:23,641 same effect so if you run this code 6204 04:38:23,641 --> 04:38:27,560 this will work in the same way 6205 04:38:27,660 --> 04:38:30,539 so we have the same code here because 6206 04:38:30,539 --> 04:38:33,061 this does the exact that the function 6207 04:38:33,061 --> 04:38:34,740 did previously it will check to see if 6208 04:38:34,740 --> 04:38:37,199 this is a new number if this is any of a 6209 04:38:37,199 --> 04:38:38,099 number 6210 04:38:38,099 --> 04:38:39,778 the 6211 04:38:39,778 --> 04:38:41,820 this is not going to be true because 6212 04:38:41,820 --> 04:38:43,980 hero checking to see if this is not 6213 04:38:43,980 --> 04:38:47,458 equal to zero and our number is equal to 6214 04:38:47,458 --> 04:38:48,958 zero so this condition inside the 6215 04:38:48,958 --> 04:38:51,599 parenthesis is going to be false the 6216 04:38:51,599 --> 04:38:53,340 continue is not going to be called and 6217 04:38:53,340 --> 04:38:55,141 the our code below is going to be called 6218 04:38:55,141 --> 04:38:57,000 our even number counter is going to be 6219 04:38:57,000 --> 04:38:59,458 incremented and our number is going to 6220 04:38:59,458 --> 04:39:02,039 be output in the console so this does 6221 04:39:02,039 --> 04:39:04,259 the same thing as previously 6222 04:39:04,259 --> 04:39:06,719 and we can do this challenge also in the 6223 04:39:06,719 --> 04:39:10,438 first of the four Loop if you want 6224 04:39:10,438 --> 04:39:13,020 so you can type here four 6225 04:39:13,020 --> 04:39:16,259 I in one 6226 04:39:16,259 --> 04:39:19,561 to 20 then execute the code inside the 6227 04:39:19,561 --> 04:39:23,240 curly braces and here you can 6228 04:39:25,020 --> 04:39:28,219 check if 6229 04:39:30,958 --> 04:39:35,039 parenthesis again if I present 6230 04:39:35,039 --> 04:39:38,779 2 not equal to 6231 04:39:40,440 --> 04:39:42,480 zero 6232 04:39:42,480 --> 04:39:44,940 then call continue 6233 04:39:44,940 --> 04:39:46,378 then 6234 04:39:46,378 --> 04:39:49,260 else is gonna execute the code which 6235 04:39:49,260 --> 04:39:52,020 comes below here 6236 04:39:52,020 --> 04:39:55,760 which is let's first 6237 04:39:56,940 --> 04:39:59,580 assign a value for your to a even number 6238 04:39:59,580 --> 04:40:01,440 counter to zero 6239 04:40:01,440 --> 04:40:03,840 because where when we're gonna enter in 6240 04:40:03,840 --> 04:40:07,020 the for Loop we should have a zero to 6241 04:40:07,020 --> 04:40:09,000 not have the previous value which was 6242 04:40:09,000 --> 04:40:10,440 20. 6243 04:40:10,440 --> 04:40:12,540 so let's put 0 here and here you can 6244 04:40:12,540 --> 04:40:16,280 increment or even number counter 6245 04:40:16,378 --> 04:40:20,400 plus plus and we're gonna print our even 6246 04:40:20,400 --> 04:40:23,458 number so I'm going to print I here 6247 04:40:23,458 --> 04:40:25,860 so if you run this code 6248 04:40:25,860 --> 04:40:29,780 let's run it from here 6249 04:40:33,600 --> 04:40:35,580 but let's add the printerland to make 6250 04:40:35,580 --> 04:40:37,560 things more clear so I'm going to add 6251 04:40:37,560 --> 04:40:40,160 here print 6252 04:40:44,040 --> 04:40:46,020 let's put 6253 04:40:46,020 --> 04:40:50,360 quotation marks and here I'm gonna type 6254 04:40:50,640 --> 04:40:52,560 total 6255 04:40:52,560 --> 04:40:54,980 number 6256 04:40:57,958 --> 04:41:00,739 phone 6257 04:41:01,920 --> 04:41:04,140 using four 6258 04:41:04,140 --> 04:41:06,920 Loop 6259 04:41:07,798 --> 04:41:10,340 r 6260 04:41:12,718 --> 04:41:14,520 let's put the dollar sign now let's 6261 04:41:14,520 --> 04:41:16,980 refer our even number variable 6262 04:41:16,980 --> 04:41:20,180 now if you run this 6263 04:41:24,298 --> 04:41:27,000 now I have the exact same output but now 6264 04:41:27,000 --> 04:41:28,920 we're using the 6265 04:41:28,920 --> 04:41:31,260 first you're using the while loop then 6266 04:41:31,260 --> 04:41:33,540 you're using the photo clip so let's 6267 04:41:33,540 --> 04:41:34,920 let's add the printerline statement 6268 04:41:34,920 --> 04:41:37,020 between our 6269 04:41:37,020 --> 04:41:39,180 Loops to make clear which Loop is 6270 04:41:39,180 --> 04:41:40,920 looping so let's draw the print line 6271 04:41:40,920 --> 04:41:42,480 here 6272 04:41:42,480 --> 04:41:45,060 and let's type here 6273 04:41:45,060 --> 04:41:47,400 now 6274 04:41:47,400 --> 04:41:49,940 we 6275 04:41:52,020 --> 04:41:55,020 so we are 6276 04:41:55,020 --> 04:41:57,540 looping 6277 04:41:57,540 --> 04:42:00,798 using the 6278 04:42:01,378 --> 04:42:02,638 four 6279 04:42:02,638 --> 04:42:05,280 Loop so now if you run this 6280 04:42:05,280 --> 04:42:09,378 to make more clear which Loop is looping 6281 04:42:12,420 --> 04:42:14,878 so now as you can see first the while 6282 04:42:14,878 --> 04:42:18,120 loop Loops then we have our printerland 6283 04:42:18,120 --> 04:42:19,138 which 6284 04:42:19,138 --> 04:42:22,138 says that now our while loop our for 6285 04:42:22,138 --> 04:42:24,180 Loop starts looping then we loop with 6286 04:42:24,180 --> 04:42:25,740 the for Loop 6287 04:42:25,740 --> 04:42:28,940 then at the end 6288 04:42:29,160 --> 04:42:31,980 we output the total number total number 6289 04:42:31,980 --> 04:42:33,958 of even only fever numbers found with 6290 04:42:33,958 --> 04:42:37,320 the while loop we have 10 then we output 6291 04:42:37,320 --> 04:42:40,440 the total number found the total number 6292 04:42:40,440 --> 04:42:42,840 of even numbers found using the for Loop 6293 04:42:42,840 --> 04:42:46,440 and r10 so our code works perfectly well 6294 04:42:46,440 --> 04:42:48,718 and 6295 04:42:48,718 --> 04:42:51,958 uh this do not does not represent the 6296 04:42:51,958 --> 04:42:54,920 the best 6297 04:42:56,520 --> 04:42:58,920 the best way to solve this challenge of 6298 04:42:58,920 --> 04:43:00,298 course that you can 6299 04:43:00,298 --> 04:43:03,180 find a totally different way a better 6300 04:43:03,180 --> 04:43:05,340 way to solve this so 6301 04:43:05,340 --> 04:43:08,040 this was my this was just my way of 6302 04:43:08,040 --> 04:43:09,660 solving the problem 6303 04:43:09,660 --> 04:43:12,360 so see you in the next video and how I 6304 04:43:12,360 --> 04:43:14,340 hope that you enjoy this challenge so 6305 04:43:14,340 --> 04:43:15,600 now it's time to start a discussion 6306 04:43:15,600 --> 04:43:17,458 about the race but first let's create a 6307 04:43:17,458 --> 04:43:20,878 new project I'm going to call it a race 6308 04:43:20,878 --> 04:43:22,798 for the language select kotlin for the 6309 04:43:22,798 --> 04:43:24,780 build system IntelliJ make sure to have 6310 04:43:24,780 --> 04:43:26,940 the jdk selected and check this little 6311 04:43:26,940 --> 04:43:29,458 box at sample code to have the main 6312 04:43:29,458 --> 04:43:31,440 function Auto generator for us and click 6313 04:43:31,440 --> 04:43:33,739 on create 6314 04:43:34,680 --> 04:43:36,420 all right now let's delete the code 6315 04:43:36,420 --> 04:43:37,920 inside the calibrases of the main 6316 04:43:37,920 --> 04:43:40,280 function 6317 04:43:41,580 --> 04:43:43,378 let's hide the project Pane and let's 6318 04:43:43,378 --> 04:43:46,020 start our discussion about arrays and so 6319 04:43:46,020 --> 04:43:47,700 far in our videos we've only looked at 6320 04:43:47,700 --> 04:43:49,440 how we can store only one single value 6321 04:43:49,440 --> 04:43:52,200 in a variable and that is very useful 6322 04:43:52,200 --> 04:43:56,120 but what if you have a large amount of 6323 04:43:56,120 --> 04:43:58,138 values that you want to store in our 6324 04:43:58,138 --> 04:43:59,580 variables let's say that you want to 6325 04:43:59,580 --> 04:44:01,200 store 6326 04:44:01,200 --> 04:44:03,320 30 or 40 different 6327 04:44:03,320 --> 04:44:06,360 values in our variable of a certain type 6328 04:44:06,360 --> 04:44:08,400 of course that you can declare a 6329 04:44:08,400 --> 04:44:11,360 variable for each of one of those 6330 04:44:11,360 --> 04:44:14,458 values then and then assign it to the 6331 04:44:14,458 --> 04:44:16,878 variables but what happens if you have a 6332 04:44:16,878 --> 04:44:19,080 1000 or 10 20 6333 04:44:19,080 --> 04:44:19,940 000 6334 04:44:19,940 --> 04:44:22,920 values in that case uh it will not be 6335 04:44:22,920 --> 04:44:25,320 practically possible to write each one 6336 04:44:25,320 --> 04:44:26,760 of those variables and then assign a 6337 04:44:26,760 --> 04:44:30,420 value to it in a colon help us here 6338 04:44:30,420 --> 04:44:33,420 because it provides us arrays and an 6339 04:44:33,420 --> 04:44:36,540 array allows us to store more than one 6340 04:44:36,540 --> 04:44:39,958 single element in a variable name so it 6341 04:44:39,958 --> 04:44:42,840 allows us to store more more elements in 6342 04:44:42,840 --> 04:44:44,940 one single variable name 6343 04:44:44,940 --> 04:44:49,560 now to declare an array we type Val 6344 04:44:49,560 --> 04:44:51,060 hero type 6345 04:44:51,060 --> 04:44:52,920 names 6346 04:44:52,920 --> 04:44:54,958 next we need to provide the type and the 6347 04:44:54,958 --> 04:44:57,420 type is going to be an array 6348 04:44:57,420 --> 04:45:00,060 and IntelliJ is giving us some 6349 04:45:00,060 --> 04:45:01,920 suggestion there so press enter to fill 6350 04:45:01,920 --> 04:45:04,260 it in that for you and now we put angle 6351 04:45:04,260 --> 04:45:06,600 brackets and inside the angle brackets 6352 04:45:06,600 --> 04:45:07,860 we're going to put what kind of data 6353 04:45:07,860 --> 04:45:09,600 this array is going to store so it's 6354 04:45:09,600 --> 04:45:12,600 going to store string or text or 6355 04:45:12,600 --> 04:45:14,100 sequence of characters 6356 04:45:14,100 --> 04:45:17,400 then you put equals and here we don't 6357 04:45:17,400 --> 04:45:19,740 type political value here we have a hero 6358 04:45:19,740 --> 04:45:21,420 type of function call and here we type 6359 04:45:21,420 --> 04:45:24,138 array of 6360 04:45:24,240 --> 04:45:27,600 and now we put our elements inside the 6361 04:45:27,600 --> 04:45:31,798 the parenthesis of the function called 6362 04:45:31,798 --> 04:45:33,360 separated by comma so I'm going to put 6363 04:45:33,360 --> 04:45:35,760 three names here so I'm going to put 6364 04:45:35,760 --> 04:45:37,920 John 6365 04:45:37,920 --> 04:45:39,480 Steven 6366 04:45:39,480 --> 04:45:41,820 and Megan 6367 04:45:41,820 --> 04:45:44,580 now let's uh let's see what we have here 6368 04:45:44,580 --> 04:45:46,980 so first we have the Val keyword next we 6369 04:45:46,980 --> 04:45:49,320 have a name for our variable and the 6370 04:45:49,320 --> 04:45:52,080 name is names then we have the type and 6371 04:45:52,080 --> 04:45:55,080 the type is an array which means that 6372 04:45:55,080 --> 04:45:57,120 you can store multiple values in our 6373 04:45:57,120 --> 04:45:59,760 variable and next we have angle brackets 6374 04:45:59,760 --> 04:46:01,560 and string and this means that this area 6375 04:46:01,560 --> 04:46:05,100 is going to store text or a sequence of 6376 04:46:05,100 --> 04:46:07,680 characters it's gonna store only strings 6377 04:46:07,680 --> 04:46:09,900 and then you put equals we have the 6378 04:46:09,900 --> 04:46:11,700 function called array of and then we put 6379 04:46:11,700 --> 04:46:14,400 our elements of type string which is 6380 04:46:14,400 --> 04:46:17,700 those names separated by comma but 6381 04:46:17,700 --> 04:46:20,700 because cotton has Type inference here 6382 04:46:20,700 --> 04:46:22,920 you can delete the type 6383 04:46:22,920 --> 04:46:25,080 and the type is going to be inferred in 6384 04:46:25,080 --> 04:46:26,820 the same way it was it was inferred 6385 04:46:26,820 --> 04:46:29,100 previously when I when you just declare 6386 04:46:29,100 --> 04:46:31,138 a simple variable so now the type is 6387 04:46:31,138 --> 04:46:32,638 still inferred as an array of strings 6388 04:46:32,638 --> 04:46:36,240 and now it's a good moment to introduce 6389 04:46:36,240 --> 04:46:39,780 type hints but we need to activate the 6390 04:46:39,780 --> 04:46:42,660 typings from settings and to do that you 6391 04:46:42,660 --> 04:46:44,820 can go to file 6392 04:46:44,820 --> 04:46:46,980 and click on settings or you can press 6393 04:46:46,980 --> 04:46:49,080 the the shortcut that you have here for 6394 04:46:49,080 --> 04:46:52,080 our particular system and for Windows is 6395 04:46:52,080 --> 04:46:54,540 Ctrl alt s so I'm going to press Ctrl 6396 04:46:54,540 --> 04:46:56,900 this 6397 04:46:57,540 --> 04:47:00,480 and here we go to if you have something 6398 04:47:00,480 --> 04:47:02,340 like this you go to editor click on this 6399 04:47:02,340 --> 04:47:04,740 greater arrow in front of editor 6400 04:47:04,740 --> 04:47:07,620 and then you go down here where it says 6401 04:47:07,620 --> 04:47:10,500 what it says inline hints click on on 6402 04:47:10,500 --> 04:47:12,420 this Creator are in front of inline 6403 04:47:12,420 --> 04:47:14,160 hints and from 6404 04:47:14,160 --> 04:47:16,320 those options select kotlin 6405 04:47:16,320 --> 04:47:18,780 and here you go to types and select 6406 04:47:18,780 --> 04:47:22,260 select local variable types then click 6407 04:47:22,260 --> 04:47:24,679 apply 6408 04:47:25,020 --> 04:47:27,120 and okay 6409 04:47:27,120 --> 04:47:29,820 now what you have here on the right of 6410 04:47:29,820 --> 04:47:33,540 our variable name is a hint so it's not 6411 04:47:33,540 --> 04:47:35,520 real text this is just there to remind 6412 04:47:35,520 --> 04:47:38,638 us what kind of type this variable has 6413 04:47:38,638 --> 04:47:40,440 and in our case is an array of string 6414 04:47:40,440 --> 04:47:43,020 and my suggestion for this to keep those 6415 04:47:43,020 --> 04:47:44,878 scenes activated because in this way 6416 04:47:44,878 --> 04:47:47,458 you'll always see what type of variable 6417 04:47:47,458 --> 04:47:50,520 is without explicitly declaring the type 6418 04:47:50,520 --> 04:47:53,340 so this is not uh this is this is just 6419 04:47:53,340 --> 04:47:56,218 there to help us to remind us what kind 6420 04:47:56,218 --> 04:47:58,620 of type a variable is 6421 04:47:58,620 --> 04:48:01,080 now let's say that I want to Output a 6422 04:48:01,080 --> 04:48:02,820 certain element in the array in the 6423 04:48:02,820 --> 04:48:03,840 console 6424 04:48:03,840 --> 04:48:05,940 to do that we cannot just type here 6425 04:48:05,940 --> 04:48:08,638 println and pass names because you need 6426 04:48:08,638 --> 04:48:10,560 more to be more specific than that we 6427 04:48:10,560 --> 04:48:14,160 cannot pass the array here and to get a 6428 04:48:14,160 --> 04:48:16,500 specific element in the array you need 6429 04:48:16,500 --> 04:48:18,900 to know that the elements in the array 6430 04:48:18,900 --> 04:48:22,920 are stored at indexes and the index of 6431 04:48:22,920 --> 04:48:26,160 an array always starts at zero so the 6432 04:48:26,160 --> 04:48:28,260 first element is stored at the index 0 6433 04:48:28,260 --> 04:48:30,540 and to get the first element we tab we 6434 04:48:30,540 --> 04:48:31,560 type here 6435 04:48:31,560 --> 04:48:35,340 square brackets and you put 0 and that 6436 04:48:35,340 --> 04:48:37,378 if you press Ctrl 6437 04:48:37,378 --> 04:48:40,920 if you press shift Ctrl p on it 6438 04:48:40,920 --> 04:48:43,440 it's a string because that is going to 6439 04:48:43,440 --> 04:48:45,958 return as the first value in the array 6440 04:48:45,958 --> 04:48:49,680 which is uh John so if you run this code 6441 04:48:49,680 --> 04:48:52,200 we get John but let's put some text here 6442 04:48:52,200 --> 04:48:53,458 to make it more 6443 04:48:53,458 --> 04:48:55,500 clear that this is the first element so 6444 04:48:55,500 --> 04:48:57,240 let's type first 6445 04:48:57,240 --> 04:48:59,340 element now let's choose the dollar sign 6446 04:48:59,340 --> 04:49:03,000 because this now is a placeholder and 6447 04:49:03,000 --> 04:49:07,100 let's put the curly braces around our 6448 04:49:07,500 --> 04:49:10,320 our element 6449 04:49:10,320 --> 04:49:13,020 and the quotation mark here now if you 6450 04:49:13,020 --> 04:49:15,560 run this code 6451 04:49:18,480 --> 04:49:20,878 you get first element John let's put 6452 04:49:20,878 --> 04:49:23,240 here some 6453 04:49:25,200 --> 04:49:27,660 so get the first element John because uh 6454 04:49:27,660 --> 04:49:31,378 John is stored at the index 0. and in 6455 04:49:31,378 --> 04:49:32,940 the same way 6456 04:49:32,940 --> 04:49:36,180 John is stored at the index 0 Steven is 6457 04:49:36,180 --> 04:49:38,160 stored at the index one and Megan is 6458 04:49:38,160 --> 04:49:41,820 stored at the index two so our index 6459 04:49:41,820 --> 04:49:44,100 starts at zero the index of an array 6460 04:49:44,100 --> 04:49:47,040 always starts at zero and you can 6461 04:49:47,040 --> 04:49:48,120 replace 6462 04:49:48,120 --> 04:49:51,240 a specific in an element in the array 6463 04:49:51,240 --> 04:49:54,900 also using the index operator so you can 6464 04:49:54,900 --> 04:49:56,340 type here 6465 04:49:56,340 --> 04:49:58,680 names again 6466 04:49:58,680 --> 04:50:01,378 square brackets zero 6467 04:50:01,378 --> 04:50:03,360 equals so now I'm going to assign a new 6468 04:50:03,360 --> 04:50:06,060 value to our element stored at index 0 6469 04:50:06,060 --> 04:50:08,458 which is now put here 6470 04:50:08,458 --> 04:50:10,620 the value 6471 04:50:10,620 --> 04:50:15,200 Alex so now if you run this code 6472 04:50:18,420 --> 04:50:21,660 now get first elementalics because 6473 04:50:21,660 --> 04:50:25,798 John was John which is at index 0 was 6474 04:50:25,798 --> 04:50:28,680 replaced with the value Alex so now we 6475 04:50:28,680 --> 04:50:32,340 have Alex Steven and Megan but what you 6476 04:50:32,340 --> 04:50:35,760 can't do is you cannot get an uh element 6477 04:50:35,760 --> 04:50:38,218 at neither and an index that doesn't 6478 04:50:38,218 --> 04:50:42,060 exist so we cannot get here let's say an 6479 04:50:42,060 --> 04:50:44,878 element of the index 4. because if you 6480 04:50:44,878 --> 04:50:48,080 run this look what happens 6481 04:50:51,660 --> 04:50:53,040 we get 6482 04:50:53,040 --> 04:50:56,040 an error which says array index out of 6483 04:50:56,040 --> 04:50:59,040 bond extension index 4 out of bonds 6484 04:50:59,040 --> 04:51:01,920 bonds for length 3. that is because the 6485 04:51:01,920 --> 04:51:05,400 index 4 doesn't exist and here we try to 6486 04:51:05,400 --> 04:51:08,340 access that value and that value at that 6487 04:51:08,340 --> 04:51:10,798 value and that index doesn't exist and 6488 04:51:10,798 --> 04:51:13,320 this is called the runtime error because 6489 04:51:13,320 --> 04:51:18,480 this error is is called only one while 6490 04:51:18,480 --> 04:51:21,298 our app is still running so you don't 6491 04:51:21,298 --> 04:51:24,240 have here an error like we have for uh 6492 04:51:24,240 --> 04:51:27,600 here we don't have a compile error which 6493 04:51:27,600 --> 04:51:30,540 you saw in our previous videos that is 6494 04:51:30,540 --> 04:51:32,160 because 6495 04:51:32,160 --> 04:51:35,520 the size of an array is not known we 6496 04:51:35,520 --> 04:51:37,620 tell our compile time also it's very 6497 04:51:37,620 --> 04:51:40,260 important to know here that the size of 6498 04:51:40,260 --> 04:51:43,200 an array is fixed so this will always 6499 04:51:43,200 --> 04:51:47,700 have the size three and uh the only way 6500 04:51:47,700 --> 04:51:50,040 to add new elements to it is to put the 6501 04:51:50,040 --> 04:51:52,638 elements directly here but there is not 6502 04:51:52,638 --> 04:51:55,920 another way to let's say that I want I 6503 04:51:55,920 --> 04:51:57,780 can put another element in the array 6504 04:51:57,780 --> 04:52:00,180 down here we can only replace elements 6505 04:52:00,180 --> 04:52:03,000 and then in the next videos you're gonna 6506 04:52:03,000 --> 04:52:06,000 see that there are other arrays 6507 04:52:06,000 --> 04:52:08,820 which are more flexible in the sense 6508 04:52:08,820 --> 04:52:10,980 that they can grow and Shrink we can add 6509 04:52:10,980 --> 04:52:15,420 the or remove elements from those arrays 6510 04:52:15,420 --> 04:52:18,060 but for now uh we're gonna look just at 6511 04:52:18,060 --> 04:52:20,040 the for this array 6512 04:52:20,040 --> 04:52:21,840 so let's change this back to zero to 6513 04:52:21,840 --> 04:52:23,580 don't have that error 6514 04:52:23,580 --> 04:52:24,840 and um 6515 04:52:24,840 --> 04:52:27,480 to avoid this kind of Errors you can 6516 04:52:27,480 --> 04:52:29,878 check to see what is the size of the 6517 04:52:29,878 --> 04:52:31,560 array in other words to see what is the 6518 04:52:31,560 --> 04:52:34,500 number of elements stored in Array using 6519 04:52:34,500 --> 04:52:36,958 the size variable so let's do that so 6520 04:52:36,958 --> 04:52:38,040 I'm going to add the printer lens 6521 04:52:38,040 --> 04:52:39,900 statement below for first one so I'm 6522 04:52:39,900 --> 04:52:42,920 going to type here println 6523 04:52:45,000 --> 04:52:46,680 and here I'm going to put the text the 6524 04:52:46,680 --> 04:52:48,240 size of the array is 6525 04:52:48,240 --> 04:52:49,560 and here I'm going to put the dollar 6526 04:52:49,560 --> 04:52:52,080 sign curly braces now I'm going to use 6527 04:52:52,080 --> 04:52:54,600 our names array 6528 04:52:54,600 --> 04:52:57,180 and you're going to put that 6529 04:52:57,180 --> 04:52:58,440 size 6530 04:52:58,440 --> 04:53:00,900 and this is gonna give us the size of 6531 04:53:00,900 --> 04:53:03,120 the array or or it's going to give us 6532 04:53:03,120 --> 04:53:05,700 the another words the number of elements 6533 04:53:05,700 --> 04:53:07,798 which are stored in this particular 6534 04:53:07,798 --> 04:53:10,620 array so if you run this code 6535 04:53:10,620 --> 04:53:12,060 you're going to see first output the 6536 04:53:12,060 --> 04:53:13,920 first element is Alex 6537 04:53:13,920 --> 04:53:15,780 and then you're going to see the size of 6538 04:53:15,780 --> 04:53:18,980 the array is 3. 6539 04:53:19,920 --> 04:53:22,320 so we get first element Alex and the 6540 04:53:22,320 --> 04:53:24,180 size of the array is three and this is 6541 04:53:24,180 --> 04:53:26,100 very important to note that the size of 6542 04:53:26,100 --> 04:53:30,120 the array is always one times greater 6543 04:53:30,120 --> 04:53:32,458 than the last index in the array so we 6544 04:53:32,458 --> 04:53:35,280 have an array which starts as index 0 6545 04:53:35,280 --> 04:53:38,520 and and ends at index two 6546 04:53:38,520 --> 04:53:41,160 which has a size of three so very 6547 04:53:41,160 --> 04:53:44,040 important to remember this to avoid the 6548 04:53:44,040 --> 04:53:47,580 crashes and your programming and of 6549 04:53:47,580 --> 04:53:49,378 course that you can put other data types 6550 04:53:49,378 --> 04:53:51,120 in our array so I can declare another 6551 04:53:51,120 --> 04:53:53,820 array here also Val 6552 04:53:53,820 --> 04:53:55,860 called 6553 04:53:55,860 --> 04:53:58,400 numbers 6554 04:53:58,500 --> 04:54:01,980 and we can use the array of again 6555 04:54:01,980 --> 04:54:05,480 and here I can pass some numbers 6556 04:54:06,060 --> 04:54:08,160 and now you see the type is an array of 6557 04:54:08,160 --> 04:54:11,400 Fint or an array of integers or whole 6558 04:54:11,400 --> 04:54:14,638 numbers and you can also mix types here 6559 04:54:14,638 --> 04:54:16,560 so you can put here 6560 04:54:16,560 --> 04:54:20,060 numbers and strings 6561 04:54:20,580 --> 04:54:23,160 so now we have another and this which is 6562 04:54:23,160 --> 04:54:25,020 between our angle brackets you don't 6563 04:54:25,020 --> 04:54:26,340 know you don't need to understand this 6564 04:54:26,340 --> 04:54:29,580 for now but this allows us to have array 6565 04:54:29,580 --> 04:54:32,458 of two types respectively of type int 6566 04:54:32,458 --> 04:54:35,280 and of type string and you can post also 6567 04:54:35,280 --> 04:54:38,780 a chart here if you want 6568 04:54:39,180 --> 04:54:42,780 let's say a so we can create an array of 6569 04:54:42,780 --> 04:54:44,760 any type if you want 6570 04:54:44,760 --> 04:54:47,400 so this allows us to 6571 04:54:47,400 --> 04:54:52,260 to put to mix our types in in the array 6572 04:54:52,260 --> 04:54:54,900 and because we've looked at loops and 6573 04:54:54,900 --> 04:54:56,820 how you can use loops 6574 04:54:56,820 --> 04:54:59,340 you can use Loops to Loop to this array 6575 04:54:59,340 --> 04:55:01,200 so I can put here 6576 04:55:01,200 --> 04:55:03,000 let's say that I want to use the first 6577 04:55:03,000 --> 04:55:06,920 array so I can put here four 6578 04:55:07,620 --> 04:55:09,000 name 6579 04:55:09,000 --> 04:55:11,580 in names now I'm going to refer our 6580 04:55:11,580 --> 04:55:15,200 array of strings 6581 04:55:16,260 --> 04:55:18,920 then 6582 04:55:19,080 --> 04:55:20,580 print 6583 04:55:20,580 --> 04:55:24,180 print A lender name 6584 04:55:24,180 --> 04:55:26,040 so now you see that we have this type 6585 04:55:26,040 --> 04:55:29,160 hint for our name variable that we have 6586 04:55:29,160 --> 04:55:31,020 here of course this can have whatever 6587 04:55:31,020 --> 04:55:33,900 name you want now if you run this you're 6588 04:55:33,900 --> 04:55:36,540 gonna see our three names outputted down 6589 04:55:36,540 --> 04:55:39,500 here in the console 6590 04:55:41,638 --> 04:55:44,940 so now we have Alex Stefan Steven and 6591 04:55:44,940 --> 04:55:48,660 Megan because uh now you're we're using 6592 04:55:48,660 --> 04:55:53,100 the for Loop to Loop through our array 6593 04:55:53,100 --> 04:55:54,600 names 6594 04:55:54,600 --> 04:55:56,218 and uh 6595 04:55:56,218 --> 04:55:58,200 this is interesting but what if you want 6596 04:55:58,200 --> 04:56:00,600 to Loop through our array numbers which 6597 04:56:00,600 --> 04:56:03,420 is a mixed array we have numbers and 6598 04:56:03,420 --> 04:56:08,179 strings and charts we can refer it here 6599 04:56:11,480 --> 04:56:14,218 let's call it 6600 04:56:14,218 --> 04:56:15,420 I 6601 04:56:15,420 --> 04:56:17,899 in 6602 04:56:21,540 --> 04:56:24,540 and now you see the type that was 6603 04:56:24,540 --> 04:56:26,638 inferred for the eye is this comparable 6604 04:56:26,638 --> 04:56:28,200 that we have here 6605 04:56:28,200 --> 04:56:30,060 so this 6606 04:56:30,060 --> 04:56:32,160 is going to Loop through our mixed array 6607 04:56:32,160 --> 04:56:33,900 so we put here I 6608 04:56:33,900 --> 04:56:35,580 now if you run this 6609 04:56:35,580 --> 04:56:38,520 you're gonna see our 6610 04:56:38,520 --> 04:56:40,740 numbers and in our name one and our 6611 04:56:40,740 --> 04:56:44,060 chart output down here 6612 04:56:45,298 --> 04:56:48,060 so now we get 6613 04:56:48,060 --> 04:56:49,560 first element Alex which is a 6614 04:56:49,560 --> 04:56:51,600 printerland statement then the next 6615 04:56:51,600 --> 04:56:53,700 print line statement the size of the red 6616 04:56:53,700 --> 04:56:55,080 then you have our 6617 04:56:55,080 --> 04:56:58,138 numbers in our numbers array 6618 04:56:58,138 --> 04:57:00,660 and then we have our name one and our 6619 04:57:00,660 --> 04:57:02,700 and then we have our chart 6620 04:57:02,700 --> 04:57:04,980 so this allows us to to Loop through our 6621 04:57:04,980 --> 04:57:07,560 mix arrays but what if I want to 6622 04:57:07,560 --> 04:57:09,480 to Output 6623 04:57:09,480 --> 04:57:12,900 only if I want to Output in the console 6624 04:57:12,900 --> 04:57:15,480 only the integers in this mixed are 6625 04:57:15,480 --> 04:57:18,420 equal numbers to to do that and this is 6626 04:57:18,420 --> 04:57:19,520 a 6627 04:57:19,520 --> 04:57:21,260 good good 6628 04:57:21,260 --> 04:57:25,440 way to introduce this keyword and with 6629 04:57:25,440 --> 04:57:28,860 this keyword we can check to see if 6630 04:57:28,860 --> 04:57:31,500 a literal or a variable is supposed 6631 04:57:31,500 --> 04:57:36,718 certain type so I can put here if 6632 04:57:36,718 --> 04:57:38,040 I 6633 04:57:38,040 --> 04:57:41,218 is let's say an INT 6634 04:57:41,218 --> 04:57:43,680 then and only then I'm gonna output this 6635 04:57:43,680 --> 04:57:45,060 value in the console then I'm going to 6636 04:57:45,060 --> 04:57:47,520 call our printerland and I'm gonna post 6637 04:57:47,520 --> 04:57:50,218 our password I variable to our println 6638 04:57:50,218 --> 04:57:51,298 function 6639 04:57:51,298 --> 04:57:54,180 now if you run this you gonna you 6640 04:57:54,180 --> 04:57:57,680 gonna see in our Loop only 6641 04:57:57,680 --> 04:58:00,660 the numbers here only the integers 6642 04:58:00,660 --> 04:58:01,580 because 6643 04:58:01,580 --> 04:58:05,280 this is keyword is checking to see if 6644 04:58:05,280 --> 04:58:07,740 our right is of type integer and this is 6645 04:58:07,740 --> 04:58:10,020 going to return true or false in our 6646 04:58:10,020 --> 04:58:11,760 case it's going to return true for our 6647 04:58:11,760 --> 04:58:13,620 for all of our numbers that we have here 6648 04:58:13,620 --> 04:58:15,958 so we have four five six seven and four 6649 04:58:15,958 --> 04:58:16,860 again 6650 04:58:16,860 --> 04:58:20,218 and you can check to see if this is a HR 6651 04:58:20,218 --> 04:58:25,080 I'm gonna see in the console only our 6652 04:58:25,080 --> 04:58:30,138 single element a I'll put it down here 6653 04:58:32,040 --> 04:58:34,500 so we have a or you can check to see if 6654 04:58:34,500 --> 04:58:37,400 this is a string 6655 04:58:37,400 --> 04:58:40,980 this element that We're looping at this 6656 04:58:40,980 --> 04:58:42,540 particular iteration let's say so if you 6657 04:58:42,540 --> 04:58:44,400 run this you're gonna see in the console 6658 04:58:44,400 --> 04:58:47,458 only name one because only name one is 6659 04:58:47,458 --> 04:58:50,420 of type string 6660 04:58:50,638 --> 04:58:53,298 so I get name one so this is very 6661 04:58:53,298 --> 04:58:55,620 important to know that with the is 6662 04:58:55,620 --> 04:58:57,718 keyword you can check to see if a 6663 04:58:57,718 --> 04:58:59,878 variable or a literally is of a certain 6664 04:58:59,878 --> 04:59:02,340 type or if it's not and based on that 6665 04:59:02,340 --> 04:59:06,260 too to just to do some 6666 04:59:06,740 --> 04:59:09,660 in our case you output that value in the 6667 04:59:09,660 --> 04:59:12,120 console if that condition is true 6668 04:59:12,120 --> 04:59:14,280 so this is how you can use the is 6669 04:59:14,280 --> 04:59:17,340 keyword and the for Loop to 6670 04:59:17,340 --> 04:59:19,798 to the for Loop to Loop through our 6671 04:59:19,798 --> 04:59:22,440 array and these keyword to check to see 6672 04:59:22,440 --> 04:59:25,260 if an element in our arrays of a certain 6673 04:59:25,260 --> 04:59:29,120 type or if it's not and let's change our 6674 04:59:29,120 --> 04:59:31,560 array numbers to a different name 6675 04:59:31,560 --> 04:59:33,958 because numbers is very specific it 6676 04:59:33,958 --> 04:59:35,820 implies that we only have numbers in 6677 04:59:35,820 --> 04:59:36,958 this array 6678 04:59:36,958 --> 04:59:41,400 but we have a mixed type so we have 6679 04:59:41,400 --> 04:59:43,798 multiple types in uh our array don't 6680 04:59:43,798 --> 04:59:45,240 have only numbers 6681 04:59:45,240 --> 04:59:47,940 and to do that instead of changing our 6682 04:59:47,940 --> 04:59:50,580 numbers array and everywhere in our code 6683 04:59:50,580 --> 04:59:53,340 where you're using the this variable we 6684 04:59:53,340 --> 04:59:55,138 can right click on it 6685 04:59:55,138 --> 04:59:58,560 go to refactor and click on rename or 6686 04:59:58,560 --> 05:00:02,240 you can click shift F6 6687 05:00:03,298 --> 05:00:06,120 now if I change the name of our array 6688 05:00:06,120 --> 05:00:08,700 numbers it will be change everywhere 6689 05:00:08,700 --> 05:00:11,700 where you call this variable so look if 6690 05:00:11,700 --> 05:00:14,280 I delete now the name the name is also 6691 05:00:14,280 --> 05:00:17,760 changed in our line of Code 9 we're 6692 05:00:17,760 --> 05:00:20,458 using in the loop so now if I change 6693 05:00:20,458 --> 05:00:25,500 this to let's call it mixed 6694 05:00:27,958 --> 05:00:30,958 elements 6695 05:00:31,920 --> 05:00:34,740 if I press enter now this will be also 6696 05:00:34,740 --> 05:00:37,798 changed everywhere or we call our our 6697 05:00:37,798 --> 05:00:41,160 variable respectively on the line 9 here 6698 05:00:41,160 --> 05:00:44,760 so now we have our our 6699 05:00:44,760 --> 05:00:47,040 array numbers change to mixed Elements 6700 05:00:47,040 --> 05:00:50,100 which is more uh descriptive because it 6701 05:00:50,100 --> 05:00:52,440 implies that we have an array of mixed 6702 05:00:52,440 --> 05:00:53,638 elements 6703 05:00:53,638 --> 05:00:55,680 so now it's time to do a challenge using 6704 05:00:55,680 --> 05:00:57,180 the knowledge that we have about arrays 6705 05:00:57,180 --> 05:00:58,740 but first of all I'm going to delete the 6706 05:00:58,740 --> 05:01:00,298 code inside the curry base of the main 6707 05:01:00,298 --> 05:01:02,878 function because I have the previous 6708 05:01:02,878 --> 05:01:04,860 project opened 6709 05:01:04,860 --> 05:01:07,080 now the challenge is to create a 6710 05:01:07,080 --> 05:01:08,878 function which is going to take as an 6711 05:01:08,878 --> 05:01:12,180 argument an array of integers then you 6712 05:01:12,180 --> 05:01:15,298 need to figure out a way to determine 6713 05:01:15,298 --> 05:01:17,820 which number is the biggest number in 6714 05:01:17,820 --> 05:01:20,760 that array then we need to return that 6715 05:01:20,760 --> 05:01:23,100 number to the function 6716 05:01:23,100 --> 05:01:25,260 and you need to create another function 6717 05:01:25,260 --> 05:01:27,600 which is going to do the same thing but 6718 05:01:27,600 --> 05:01:29,340 it's gonna figure out what is the 6719 05:01:29,340 --> 05:01:32,160 minimum number stored in that array and 6720 05:01:32,160 --> 05:01:34,138 then it's going to return that value 6721 05:01:34,138 --> 05:01:36,480 and third you need to find a way to 6722 05:01:36,480 --> 05:01:39,000 combine those two function in just one 6723 05:01:39,000 --> 05:01:40,860 function which is gonna 6724 05:01:40,860 --> 05:01:43,860 return the maximum value stored in that 6725 05:01:43,860 --> 05:01:46,260 array if you want or if you don't want 6726 05:01:46,260 --> 05:01:50,580 it to return the minimum value so try to 6727 05:01:50,580 --> 05:01:53,160 solve this Challenge and then watch my 6728 05:01:53,160 --> 05:01:54,120 solution 6729 05:01:54,120 --> 05:01:57,360 now let's solve this challenge 6730 05:01:57,360 --> 05:01:59,160 so first I'm going to create a function 6731 05:01:59,160 --> 05:02:01,378 which is going to be called the find 6732 05:02:01,378 --> 05:02:03,660 marks this function is going to return 6733 05:02:03,660 --> 05:02:05,580 the maximum value 6734 05:02:05,580 --> 05:02:07,920 and it's going to take us and I'm going 6735 05:02:07,920 --> 05:02:10,560 to Define here parameter and an array so 6736 05:02:10,560 --> 05:02:13,260 here I'm going to type a name for our 6737 05:02:13,260 --> 05:02:15,958 array is going to be called numbers 6738 05:02:15,958 --> 05:02:20,520 n is going to have the type array 6739 05:02:20,520 --> 05:02:24,060 of int of integers and it's going to 6740 05:02:24,060 --> 05:02:26,958 return an integer 6741 05:02:36,120 --> 05:02:38,400 now here I'm going to declare a variable 6742 05:02:38,400 --> 05:02:40,378 it's going to be a VAR and it's going to 6743 05:02:40,378 --> 05:02:43,700 be called Max 6744 05:02:44,580 --> 05:02:46,620 and in this Max variable I'm going to 6745 05:02:46,620 --> 05:02:50,160 store the first value in the which is uh 6746 05:02:50,160 --> 05:02:52,920 which is past which is in the which is 6747 05:02:52,920 --> 05:02:54,900 in the array so I'm gonna type here 6748 05:02:54,900 --> 05:02:56,820 numbers 6749 05:02:56,820 --> 05:02:58,620 square brackets 6750 05:02:58,620 --> 05:03:00,780 and I'm going to use the index 0 to get 6751 05:03:00,780 --> 05:03:04,138 the first value in the array then 6752 05:03:04,138 --> 05:03:06,600 I'm gonna Loop through this array 6753 05:03:06,600 --> 05:03:08,400 so 6754 05:03:08,400 --> 05:03:13,100 for number in numbers 6755 05:03:19,080 --> 05:03:20,820 then here you're going to check to see 6756 05:03:20,820 --> 05:03:23,420 if our 6757 05:03:23,940 --> 05:03:26,660 number 6758 05:03:26,940 --> 05:03:29,340 is greater than our first value which 6759 05:03:29,340 --> 05:03:31,500 was stored in the array or max value and 6760 05:03:31,500 --> 05:03:34,080 if that is true then I'm gonna store 6761 05:03:34,080 --> 05:03:36,120 this value which is now 6762 05:03:36,120 --> 05:03:39,600 greater than our than our maximum value 6763 05:03:39,600 --> 05:03:42,298 in our Max variable so here I'm going to 6764 05:03:42,298 --> 05:03:44,280 put Max now it's going to be equal to 6765 05:03:44,280 --> 05:03:46,200 this number which is found to be greater 6766 05:03:46,200 --> 05:03:49,560 than our previous max value 6767 05:03:49,560 --> 05:03:51,298 and at the end of this loop we're gonna 6768 05:03:51,298 --> 05:03:52,920 return 6769 05:03:52,920 --> 05:03:55,877 the maximum value 6770 05:03:56,100 --> 05:03:58,440 and we're going to do the same for the 6771 05:03:58,440 --> 05:04:00,480 finding the minimum value so I'm going 6772 05:04:00,480 --> 05:04:03,138 to copy this code 6773 05:04:05,580 --> 05:04:09,320 and this is going to be called find mean 6774 05:04:13,080 --> 05:04:15,298 but uh here gonna change a little bit 6775 05:04:15,298 --> 05:04:19,458 the logic let's call this variable mean 6776 05:04:24,540 --> 05:04:25,860 and you're going to change the condition 6777 05:04:25,860 --> 05:04:28,100 to less than min 6778 05:04:28,100 --> 05:04:31,168 [Music] 6779 05:04:44,280 --> 05:04:46,500 now let's call those function in our 6780 05:04:46,500 --> 05:04:48,718 main function and let's pass summaries 6781 05:04:48,718 --> 05:04:51,420 to see how it works 6782 05:04:51,420 --> 05:04:54,240 so first I'm going to declare a variable 6783 05:04:54,240 --> 05:04:55,860 it's going to be a Val and it's going to 6784 05:04:55,860 --> 05:04:58,320 be called Max 6785 05:04:58,320 --> 05:05:01,100 and now I'm going to call our find Max 6786 05:05:01,100 --> 05:05:03,240 function and here I'm going to pass an 6787 05:05:03,240 --> 05:05:05,040 array and I'm gonna I'm gonna pass the 6788 05:05:05,040 --> 05:05:07,680 array directly here so I'm going to type 6789 05:05:07,680 --> 05:05:09,420 array off and I'm going to pass some 6790 05:05:09,420 --> 05:05:12,980 arbitrary numbers like 6791 05:05:18,500 --> 05:05:20,638 then I'm going to create another valve 6792 05:05:20,638 --> 05:05:23,580 this is going to be called mean 6793 05:05:23,580 --> 05:05:25,920 now I'm going to call find 6794 05:05:25,920 --> 05:05:29,218 Min and here I'm going to also 6795 05:05:29,218 --> 05:05:31,980 call our array of function and here I'm 6796 05:05:31,980 --> 05:05:34,020 going to Define some 6797 05:05:34,020 --> 05:05:37,520 arbitrary numbers again 6798 05:05:44,400 --> 05:05:46,138 now I'm going to Output those value in 6799 05:05:46,138 --> 05:05:48,298 the consoles so I'm going to add 6800 05:05:48,298 --> 05:05:51,199 printerland here 6801 05:05:55,440 --> 05:05:57,298 I'm going to type max value is dollar 6802 05:05:57,298 --> 05:05:58,740 sign 6803 05:05:58,740 --> 05:06:00,900 and I'm going to refer our Max variable 6804 05:06:00,900 --> 05:06:03,560 defined above 6805 05:06:06,900 --> 05:06:10,520 another println 6806 05:06:19,138 --> 05:06:22,760 now let's run this code it's 6807 05:06:32,878 --> 05:06:36,298 get max value 7 which is correct this is 6808 05:06:36,298 --> 05:06:38,760 the biggest number then we get the 6809 05:06:38,760 --> 05:06:40,680 minimum value is 4 and this is also 6810 05:06:40,680 --> 05:06:44,580 correct so our code Works uh perfectly 6811 05:06:44,580 --> 05:06:46,860 well now the next thing is to combine 6812 05:06:46,860 --> 05:06:49,798 those two functions that we have here 6813 05:06:49,798 --> 05:06:51,600 let's 6814 05:06:51,600 --> 05:06:54,298 increase this now we need to find a way 6815 05:06:54,298 --> 05:06:56,400 to combine those function in one single 6816 05:06:56,400 --> 05:06:58,080 function which is going to also return 6817 05:06:58,080 --> 05:07:00,600 the maximum and minimum value but only 6818 05:07:00,600 --> 05:07:04,020 if I want to return to be returned so I 6819 05:07:04,020 --> 05:07:06,540 can determine if I want the minimum 6820 05:07:06,540 --> 05:07:09,420 value to be returned by that function or 6821 05:07:09,420 --> 05:07:11,760 if I want the maximum value to be 6822 05:07:11,760 --> 05:07:13,378 returned by that function so I need to 6823 05:07:13,378 --> 05:07:17,660 find a way to combine those two 6824 05:07:18,360 --> 05:07:20,700 and to do that I'm going to delete our 6825 05:07:20,700 --> 05:07:24,200 second function 6826 05:07:27,480 --> 05:07:29,520 I'm gonna delete the code inside the 6827 05:07:29,520 --> 05:07:32,480 this function 6828 05:07:32,820 --> 05:07:34,260 and I'm going to change the name of this 6829 05:07:34,260 --> 05:07:36,120 function to 6830 05:07:36,120 --> 05:07:41,638 find mean and Max and now here I'm going 6831 05:07:41,638 --> 05:07:43,440 to define a parameter which is going to 6832 05:07:43,440 --> 05:07:46,740 be a Boolean uh which is going to have a 6833 05:07:46,740 --> 05:07:49,620 Boolean type so I'm gonna call it 6834 05:07:49,620 --> 05:07:53,420 I'm going to call it search 6835 05:07:54,000 --> 05:07:56,540 Max 6836 05:07:56,580 --> 05:07:59,540 and it's going to be 6837 05:08:01,798 --> 05:08:03,718 a Boolean 6838 05:08:03,718 --> 05:08:06,180 all right 6839 05:08:06,180 --> 05:08:08,340 now 6840 05:08:08,340 --> 05:08:11,280 based on that Boolean value if is true 6841 05:08:11,280 --> 05:08:14,280 if I want to search the maximum value 6842 05:08:14,280 --> 05:08:16,680 here I'm going to type an if statement 6843 05:08:16,680 --> 05:08:19,620 so I'm going to put if search marks then 6844 05:08:19,620 --> 05:08:21,718 I'm gonna 6845 05:08:21,718 --> 05:08:24,000 add here the code to search for the 6846 05:08:24,000 --> 05:08:26,760 maximum value the first let's define um 6847 05:08:26,760 --> 05:08:29,160 a variable here is going to be also VAR 6848 05:08:29,160 --> 05:08:31,980 let's be called gonna be called 6849 05:08:31,980 --> 05:08:36,240 Max and uh it's also going to have the 6850 05:08:36,240 --> 05:08:39,420 first value in the array 6851 05:08:39,420 --> 05:08:42,298 which is also adding the zero now if our 6852 05:08:42,298 --> 05:08:45,780 search marks is true then we want our 6853 05:08:45,780 --> 05:08:48,840 maximum value to be search and return so 6854 05:08:48,840 --> 05:08:51,120 I'm gonna add here to the code to to do 6855 05:08:51,120 --> 05:08:52,860 that so I'm going to type here a for 6856 05:08:52,860 --> 05:08:54,780 Loop 6857 05:08:54,780 --> 05:08:56,878 which is going to go through our numbers 6858 05:08:56,878 --> 05:08:59,420 array 6859 05:09:05,280 --> 05:09:07,320 and here I'm gonna add the 6860 05:09:07,320 --> 05:09:09,420 the if statement which I'm gonna check 6861 05:09:09,420 --> 05:09:11,700 to see if our number 6862 05:09:11,700 --> 05:09:13,920 is greater than our Max then I'm gonna 6863 05:09:13,920 --> 05:09:16,699 assign our 6864 05:09:17,100 --> 05:09:18,480 our 6865 05:09:18,480 --> 05:09:22,340 number Tower Max 6866 05:09:22,920 --> 05:09:26,040 now I'm gonna go down here and and here 6867 05:09:26,040 --> 05:09:27,840 I'm going to add the else part so if 6868 05:09:27,840 --> 05:09:29,458 this uh 6869 05:09:29,458 --> 05:09:31,378 argument which is passed to our 6870 05:09:31,378 --> 05:09:34,440 parameter search Max is false so I don't 6871 05:09:34,440 --> 05:09:36,600 want to search for the maximum value 6872 05:09:36,600 --> 05:09:38,280 then that means that I want to search 6873 05:09:38,280 --> 05:09:39,718 for the minimum value so I'm going to 6874 05:09:39,718 --> 05:09:42,060 put here else now I'm going to add the 6875 05:09:42,060 --> 05:09:44,520 code to search for the minimum value and 6876 05:09:44,520 --> 05:09:46,378 for this you need to Define also 6877 05:09:46,378 --> 05:09:47,638 variable here 6878 05:09:47,638 --> 05:09:49,980 it's going to be called mean and it's 6879 05:09:49,980 --> 05:09:52,320 going to have the value of Max I'm going 6880 05:09:52,320 --> 05:09:54,840 to explain a minute how this works 6881 05:09:54,840 --> 05:09:56,218 now 6882 05:09:56,218 --> 05:09:59,820 here we're going to gonna also Loop 6883 05:09:59,820 --> 05:10:02,660 in our 6884 05:10:03,180 --> 05:10:07,200 numbers draw numbers array 6885 05:10:07,200 --> 05:10:09,660 and you're going to check if we 6886 05:10:09,660 --> 05:10:11,520 essentially we have the same code that 6887 05:10:11,520 --> 05:10:14,580 we have in our find the mean function so 6888 05:10:14,580 --> 05:10:17,700 here we're going to search if our number 6889 05:10:17,700 --> 05:10:20,100 is less than our mean 6890 05:10:20,100 --> 05:10:23,180 then gonna sign 6891 05:10:23,700 --> 05:10:27,260 our number to our mean 6892 05:10:33,360 --> 05:10:36,900 also I need to return this value 6893 05:10:36,900 --> 05:10:39,000 at the end of our Loop so here we need 6894 05:10:39,000 --> 05:10:40,378 to return 6895 05:10:40,378 --> 05:10:42,540 Max 6896 05:10:42,540 --> 05:10:46,160 and here you need to return mean 6897 05:10:48,718 --> 05:10:51,958 now let's press Ctrl alt L to format the 6898 05:10:51,958 --> 05:10:53,638 code 6899 05:10:53,638 --> 05:10:56,040 now let's delete the 6900 05:10:56,040 --> 05:10:59,580 those the code that you have here 6901 05:10:59,580 --> 05:11:02,638 let's delete also this code now 6902 05:11:02,638 --> 05:11:05,160 here now I'm going to put our fine Min 6903 05:11:05,160 --> 05:11:07,378 and Max function directly in the 6904 05:11:07,378 --> 05:11:09,180 printerland statement so I'm gonna type 6905 05:11:09,180 --> 05:11:12,420 here uh or to put in a variable for so 6906 05:11:12,420 --> 05:11:14,160 let's put in a variable first 6907 05:11:14,160 --> 05:11:16,440 so I'm gonna 6908 05:11:16,440 --> 05:11:18,780 Define a variable he's going to be also 6909 05:11:18,780 --> 05:11:21,000 a map it's going to be Auto Max and here 6910 05:11:21,000 --> 05:11:23,820 you're gonna type find 6911 05:11:23,820 --> 05:11:26,218 Min and Max 6912 05:11:26,218 --> 05:11:28,500 now here you need to pass an array first 6913 05:11:28,500 --> 05:11:30,900 so I need to pass 6914 05:11:30,900 --> 05:11:33,660 we need to call our array off we need to 6915 05:11:33,660 --> 05:11:37,798 Define some numbers here let's say uh 6916 05:11:37,798 --> 05:11:40,399 20 6917 05:11:41,718 --> 05:11:43,620 40. 6918 05:11:43,620 --> 05:11:46,579 50. 6919 05:11:48,480 --> 05:11:50,580 now we need to pass 6920 05:11:50,580 --> 05:11:53,638 an argument to our parameter search maps 6921 05:11:53,638 --> 05:11:55,620 which is going to be either true or 6922 05:11:55,620 --> 05:11:57,420 false so first I'm going to pass through 6923 05:11:57,420 --> 05:11:59,780 here 6924 05:12:00,360 --> 05:12:04,320 now if we print this value if you output 6925 05:12:04,320 --> 05:12:07,340 this value in the console 6926 05:12:07,620 --> 05:12:10,820 let's say that we type here 6927 05:12:11,040 --> 05:12:14,000 the marks 6928 05:12:14,700 --> 05:12:16,798 equals to dollar sign I'm going to refer 6929 05:12:16,798 --> 05:12:19,340 our variable 6930 05:12:22,980 --> 05:12:26,360 now if you're around the squad 6931 05:12:28,620 --> 05:12:31,138 we get the maximum value is 100 which is 6932 05:12:31,138 --> 05:12:32,940 correct now let's duplicate this code 6933 05:12:32,940 --> 05:12:34,920 that we have here and let's try it for 6934 05:12:34,920 --> 05:12:37,579 Dominion value 6935 05:12:40,500 --> 05:12:43,458 let's call it mean 6936 05:12:45,958 --> 05:12:48,480 and let's add the printerland uh 6937 05:12:48,480 --> 05:12:51,980 statement down here 6938 05:12:55,798 --> 05:12:58,520 let's type 6939 05:12:58,620 --> 05:13:00,060 domain 6940 05:13:00,060 --> 05:13:02,780 values 6941 05:13:05,280 --> 05:13:09,120 dollar sign mean 6942 05:13:10,920 --> 05:13:13,080 now if you run this code 6943 05:13:13,080 --> 05:13:16,580 and let's pass here to false 6944 05:13:19,138 --> 05:13:22,160 let's run this code again 6945 05:13:29,580 --> 05:13:32,040 now get the maximum value is 100 which 6946 05:13:32,040 --> 05:13:33,120 is correct 6947 05:13:33,120 --> 05:13:36,240 and the minimum value is 20 and we did 6948 05:13:36,240 --> 05:13:39,120 this with our find Min and Max function 6949 05:13:39,120 --> 05:13:42,958 by combining our two previous functions 6950 05:13:42,958 --> 05:13:47,400 and the our code on the networks okay 6951 05:13:47,400 --> 05:13:49,740 now it's time to 6952 05:13:49,740 --> 05:13:52,138 have a discussion about how this code 6953 05:13:52,138 --> 05:13:55,500 how this code how this solution to my 6954 05:13:55,500 --> 05:13:58,200 challenge works 6955 05:13:58,200 --> 05:14:00,958 so now let's see how our logic inside 6956 05:14:00,958 --> 05:14:03,780 our find Min and Max function works so 6957 05:14:03,780 --> 05:14:05,520 firstly we declare our function find the 6958 05:14:05,520 --> 05:14:07,320 minion marks then we declare two 6959 05:14:07,320 --> 05:14:09,540 parameters which is one is numbers and 6960 05:14:09,540 --> 05:14:12,120 is of type array of integers so here 6961 05:14:12,120 --> 05:14:13,440 we're gonna pass only an array of 6962 05:14:13,440 --> 05:14:14,760 integers 6963 05:14:14,760 --> 05:14:17,280 and the second parameter is called 6964 05:14:17,280 --> 05:14:19,080 search marks and it's a Boolean so you 6965 05:14:19,080 --> 05:14:21,780 can pass here only true or false then 6966 05:14:21,780 --> 05:14:23,218 we're going to say to the then we say 6967 05:14:23,218 --> 05:14:24,540 here that this function is going to 6968 05:14:24,540 --> 05:14:25,980 return a value 6969 05:14:25,980 --> 05:14:29,940 next on the line 9 we declare a variable 6970 05:14:29,940 --> 05:14:31,680 which is called Max 6971 05:14:31,680 --> 05:14:34,740 and we assign using the index operator 6972 05:14:34,740 --> 05:14:36,900 the first 6973 05:14:36,900 --> 05:14:39,540 element in the array in our Max so in 6974 05:14:39,540 --> 05:14:42,298 this case we're sine 20 in our Max 6975 05:14:42,298 --> 05:14:44,638 variable and then we declare another 6976 05:14:44,638 --> 05:14:47,160 variable called mean and we assign our 6977 05:14:47,160 --> 05:14:50,580 marks in our mean but our Max has the 6978 05:14:50,580 --> 05:14:52,798 value of 20 because we assigned it here 6979 05:14:52,798 --> 05:14:56,280 so this variable mean is also is also 6980 05:14:56,280 --> 05:14:59,580 also having the value 20. so both have 6981 05:14:59,580 --> 05:15:03,298 have the have the value 20 at the start 6982 05:15:03,298 --> 05:15:06,138 then we check to see if our search marks 6983 05:15:06,138 --> 05:15:10,620 parameter is true or false and if it's 6984 05:15:10,620 --> 05:15:13,378 true then I'm gonna execute the If part 6985 05:15:13,378 --> 05:15:17,580 and if part works in the following way 6986 05:15:17,580 --> 05:15:20,160 first we Loop using the for Loop through 6987 05:15:20,160 --> 05:15:22,320 our numbers array 6988 05:15:22,320 --> 05:15:26,040 then we check at each iteration if our 6989 05:15:26,040 --> 05:15:29,458 number let's say if 20 is greater than 6990 05:15:29,458 --> 05:15:31,560 our Max so if 20 is greater than 20 6991 05:15:31,560 --> 05:15:33,780 which is going to be false so the code 6992 05:15:33,780 --> 05:15:35,700 inside the calibration if it's not going 6993 05:15:35,700 --> 05:15:37,260 to be executed 6994 05:15:37,260 --> 05:15:40,260 then go for the next iteration so here 6995 05:15:40,260 --> 05:15:42,060 now we're gonna see it gonna check to 6996 05:15:42,060 --> 05:15:45,718 see if our number respectively 40 is 6997 05:15:45,718 --> 05:15:48,540 greater than our Max which is 20. and 6998 05:15:48,540 --> 05:15:50,638 this is going to be true and because of 6999 05:15:50,638 --> 05:15:52,378 that the code inside the calibration is 7000 05:15:52,378 --> 05:15:54,240 going to be executed so now we're going 7001 05:15:54,240 --> 05:15:56,100 to assign to our marks which is now is 7002 05:15:56,100 --> 05:15:59,700 20. our number which is 40. so now our 7003 05:15:59,700 --> 05:16:02,400 Max variable has a value of 40. and 7004 05:16:02,400 --> 05:16:03,480 we're going to we're going to do the 7005 05:16:03,480 --> 05:16:05,400 setting for the next for the 50. so this 7006 05:16:05,400 --> 05:16:06,840 condition is also going to be true so 7007 05:16:06,840 --> 05:16:08,160 we're going to assign it to our Max 50 7008 05:16:08,160 --> 05:16:09,660 and so on and so on 7009 05:16:09,660 --> 05:16:13,138 until it reaches 100 which is uh which 7010 05:16:13,138 --> 05:16:15,240 is going to be also true this condition 7011 05:16:15,240 --> 05:16:16,860 is going to repeat true and I'm going to 7012 05:16:16,860 --> 05:16:18,298 be assigning 7013 05:16:18,298 --> 05:16:20,520 100 to our Max 7014 05:16:20,520 --> 05:16:25,500 value of course that if we had another 7015 05:16:25,500 --> 05:16:27,840 number here let's say two is going to 7016 05:16:27,840 --> 05:16:29,760 Loop for that number also and this 7017 05:16:29,760 --> 05:16:30,958 condition is going to be false for that 7018 05:16:30,958 --> 05:16:32,520 because our number two is not going to 7019 05:16:32,520 --> 05:16:34,440 be greater than Max and it's going to 7020 05:16:34,440 --> 05:16:36,420 exit it's going to exit the loop it's 7021 05:16:36,420 --> 05:16:38,100 going to tell it's terminate and it's 7022 05:16:38,100 --> 05:16:40,500 gonna return the maximum value which is 7023 05:16:40,500 --> 05:16:43,260 100 like we saw in our 7024 05:16:43,260 --> 05:16:46,980 output in our console and at this point 7025 05:16:46,980 --> 05:16:49,320 when we return our max value the 7026 05:16:49,320 --> 05:16:52,080 function is left so it's not executing 7027 05:16:52,080 --> 05:16:54,920 any line of code 7028 05:16:55,980 --> 05:16:57,958 besides our 7029 05:16:57,958 --> 05:17:01,080 return Max is not going to execute the 7030 05:17:01,080 --> 05:17:04,320 subsequent code which comes below but if 7031 05:17:04,320 --> 05:17:06,298 our search marks is false it's going to 7032 05:17:06,298 --> 05:17:08,160 execute the else part so in this case 7033 05:17:08,160 --> 05:17:09,840 it's gonna search 7034 05:17:09,840 --> 05:17:12,120 for the minimum value so here we have 7035 05:17:12,120 --> 05:17:14,638 the same for Loop we search you go 7036 05:17:14,638 --> 05:17:15,958 through the 7037 05:17:15,958 --> 05:17:18,540 through all the numbers but here not 7038 05:17:18,540 --> 05:17:20,878 checking to see if our number is greater 7039 05:17:20,878 --> 05:17:23,700 then marks you're checking to see if our 7040 05:17:23,700 --> 05:17:26,700 number is less than our minimum number 7041 05:17:26,700 --> 05:17:29,760 so you check to see if our let's say uh 7042 05:17:29,760 --> 05:17:32,120 let's say let's take 20 30 40 7043 05:17:32,120 --> 05:17:34,620 is less than our minimum value which is 7044 05:17:34,620 --> 05:17:36,958 20. and that is going to be false so 7045 05:17:36,958 --> 05:17:38,520 this code is not going to be going to be 7046 05:17:38,520 --> 05:17:40,138 executed inside the calibration of the 7047 05:17:40,138 --> 05:17:42,780 if and it's gonna loop again and now 7048 05:17:42,780 --> 05:17:44,400 it's gonna check for the fourth and 7049 05:17:44,400 --> 05:17:47,160 that's also going to be gonna be 7050 05:17:47,160 --> 05:17:48,600 um 7051 05:17:48,600 --> 05:17:51,600 false so and so on so and so on until it 7052 05:17:51,600 --> 05:17:53,100 reaches 100 which is also going to be 7053 05:17:53,100 --> 05:17:55,378 false the loop is going to execute and 7054 05:17:55,378 --> 05:17:57,480 it's going to return 20. of course that 7055 05:17:57,480 --> 05:18:01,500 if we put here two 7056 05:18:01,500 --> 05:18:03,958 let's say now the minimum value is going 7057 05:18:03,958 --> 05:18:06,120 to be 2 because it's going to Loop for 7058 05:18:06,120 --> 05:18:08,160 another iteration and now one is going 7059 05:18:08,160 --> 05:18:10,260 to compare C4 number 7060 05:18:10,260 --> 05:18:12,060 respectively two 7061 05:18:12,060 --> 05:18:14,400 is less than our mean which our mean 7062 05:18:14,400 --> 05:18:16,980 previously was 20 that is going to be 7063 05:18:16,980 --> 05:18:19,440 true and it's going to assign our 2 to 7064 05:18:19,440 --> 05:18:22,020 our mean so if you run this code now 7065 05:18:22,020 --> 05:18:23,760 now you're going to see that the minimum 7066 05:18:23,760 --> 05:18:26,160 value is not 20 but it's but it's two 7067 05:18:26,160 --> 05:18:28,740 and after that we return this value to 7068 05:18:28,740 --> 05:18:32,160 the function which is called 7069 05:18:32,160 --> 05:18:34,080 so now get the minion values 2 which is 7070 05:18:34,080 --> 05:18:36,660 correct so this is how this uh code 7071 05:18:36,660 --> 05:18:38,820 works 7072 05:18:38,820 --> 05:18:42,000 and see you in the next video so now 7073 05:18:42,000 --> 05:18:43,378 it's time to start a discussion about 7074 05:18:43,378 --> 05:18:45,298 object orientated programming and 7075 05:18:45,298 --> 05:18:46,680 particularly in this video we're gonna 7076 05:18:46,680 --> 05:18:49,020 look at classes and objects but first 7077 05:18:49,020 --> 05:18:51,000 i'm going to create a new project 7078 05:18:51,000 --> 05:18:53,280 I'm going to call it op 7079 05:18:53,280 --> 05:18:55,620 for the language select Catalin and for 7080 05:18:55,620 --> 05:18:57,420 the build system IntelliJ make sure to 7081 05:18:57,420 --> 05:18:59,878 have the jdk selected and also I'm gonna 7082 05:18:59,878 --> 05:19:03,120 check this little box because this ad 7083 05:19:03,120 --> 05:19:04,740 sample code is going to add the main 7084 05:19:04,740 --> 05:19:07,200 function and that code for us 7085 05:19:07,200 --> 05:19:08,580 so I'm going to click on create to 7086 05:19:08,580 --> 05:19:11,180 create the project 7087 05:19:13,980 --> 05:19:17,239 now I'm going to delete this code 7088 05:19:18,958 --> 05:19:21,240 and I'm going to hide the project pane 7089 05:19:21,240 --> 05:19:23,700 now what is object oriented programming 7090 05:19:23,700 --> 05:19:26,580 object orientated programming is a way 7091 05:19:26,580 --> 05:19:30,120 of writing code in a sense in the way 7092 05:19:30,120 --> 05:19:32,638 you think about the real world so in the 7093 05:19:32,638 --> 05:19:35,820 real world we have objects 7094 05:19:35,820 --> 05:19:38,340 which can interact with it with each 7095 05:19:38,340 --> 05:19:40,860 other and which can do stuff but the 7096 05:19:40,860 --> 05:19:43,260 objects in the real world 7097 05:19:43,260 --> 05:19:46,860 have two major characteristics first is 7098 05:19:46,860 --> 05:19:49,680 the state so another the state of an 7099 05:19:49,680 --> 05:19:52,260 object in real life is represented let's 7100 05:19:52,260 --> 05:19:55,378 say that we think about of a car 7101 05:19:55,378 --> 05:19:57,420 the state is represented by the color of 7102 05:19:57,420 --> 05:19:59,240 that car by the number 7103 05:19:59,240 --> 05:20:03,240 of Gears that car has by uh by the brand 7104 05:20:03,240 --> 05:20:06,180 name of that car so this is the state of 7105 05:20:06,180 --> 05:20:08,458 a car the next thing that real life 7106 05:20:08,458 --> 05:20:10,860 objects have is functionality 7107 05:20:10,860 --> 05:20:13,560 so objects can do stuff so let's take 7108 05:20:13,560 --> 05:20:15,480 again the example of a car 7109 05:20:15,480 --> 05:20:18,660 a car can move a car can stop a car can 7110 05:20:18,660 --> 05:20:21,180 switch gears so 7111 05:20:21,180 --> 05:20:24,360 objects in real life have this uh though 7112 05:20:24,360 --> 05:20:27,180 this those two characteristics is those 7113 05:20:27,180 --> 05:20:28,860 two major character characteristics 7114 05:20:28,860 --> 05:20:32,280 represented by the state and the 7115 05:20:32,280 --> 05:20:35,458 functionality and the aim of object 7116 05:20:35,458 --> 05:20:38,600 orientative programming is to construct 7117 05:20:38,600 --> 05:20:43,040 objects using code in the way they are 7118 05:20:43,040 --> 05:20:45,420 they in the way they are in the real 7119 05:20:45,420 --> 05:20:46,700 life 7120 05:20:46,700 --> 05:20:49,440 but uh to do that let's say that you 7121 05:20:49,440 --> 05:20:51,360 want to build an object an actual object 7122 05:20:51,360 --> 05:20:53,580 in real life what you first need to do 7123 05:20:53,580 --> 05:20:55,260 the first thing that you need to do you 7124 05:20:55,260 --> 05:20:57,718 need to create a template or a 7125 05:20:57,718 --> 05:21:00,120 construction plan for creating that 7126 05:21:00,120 --> 05:21:02,820 object in which you're gonna Define the 7127 05:21:02,820 --> 05:21:05,940 generic things that a car has 7128 05:21:05,940 --> 05:21:09,000 and in programming you will do the same 7129 05:21:09,000 --> 05:21:11,878 thing but instead of using a piece of 7130 05:21:11,878 --> 05:21:16,280 paper or uh or a something 7131 05:21:16,280 --> 05:21:20,580 on on your PC to draw that that template 7132 05:21:20,580 --> 05:21:22,980 in programming you're going to use a 7133 05:21:22,980 --> 05:21:25,440 class so the class is used to create the 7134 05:21:25,440 --> 05:21:28,138 template which then is going to be used 7135 05:21:28,138 --> 05:21:30,840 to create an actual object an object 7136 05:21:30,840 --> 05:21:33,718 which is going to have a specific State 7137 05:21:33,718 --> 05:21:36,060 and a specific functionality 7138 05:21:36,060 --> 05:21:37,500 so um 7139 05:21:37,500 --> 05:21:41,218 in a way you can think that 7140 05:21:41,218 --> 05:21:44,218 the real world serves here as a model 7141 05:21:44,218 --> 05:21:46,560 for us to write code 7142 05:21:46,560 --> 05:21:51,000 and in this way in by combining uh the 7143 05:21:51,000 --> 05:21:52,860 state and the functionality in one 7144 05:21:52,860 --> 05:21:56,218 entity and have an object and as you can 7145 05:21:56,218 --> 05:21:58,200 see you can also have 7146 05:21:58,200 --> 05:22:00,240 like in the real life objects 7147 05:22:00,240 --> 05:22:02,280 communicating with each other you're 7148 05:22:02,280 --> 05:22:03,958 gonna see that this is really powerful 7149 05:22:03,958 --> 05:22:08,040 because you can you can model your code 7150 05:22:08,040 --> 05:22:11,520 in a more dynamic in a more you know 7151 05:22:11,520 --> 05:22:14,940 more create creative way so this is what 7152 05:22:14,940 --> 05:22:17,760 we're gonna do next so first let's say 7153 05:22:17,760 --> 05:22:20,340 that I want to create an object of type 7154 05:22:20,340 --> 05:22:21,480 car 7155 05:22:21,480 --> 05:22:25,260 and to do that we go down here and we 7156 05:22:25,260 --> 05:22:27,298 need to Define as I said the 7157 05:22:27,298 --> 05:22:29,458 construction plan the template for 7158 05:22:29,458 --> 05:22:31,138 building an object and in this case 7159 05:22:31,138 --> 05:22:33,480 we're going to build the car object so 7160 05:22:33,480 --> 05:22:35,638 we're going to Define what is called a 7161 05:22:35,638 --> 05:22:39,120 class and type the keyword class 7162 05:22:39,120 --> 05:22:41,280 now we need to name our class I'm going 7163 05:22:41,280 --> 05:22:43,500 to call it car and the name of the of 7164 05:22:43,500 --> 05:22:44,700 the 7165 05:22:44,700 --> 05:22:46,020 class should start with the capital 7166 05:22:46,020 --> 05:22:47,878 letter and if it has multiple words it 7167 05:22:47,878 --> 05:22:49,520 should be in Pascal case 7168 05:22:49,520 --> 05:22:51,660 curly braces 7169 05:22:51,660 --> 05:22:53,878 and press enter now inside the color 7170 05:22:53,878 --> 05:22:56,040 braces we need to define the state and 7171 05:22:56,040 --> 05:22:59,400 the functionality as I said for the 7172 05:22:59,400 --> 05:23:01,440 objects that are going to be built with 7173 05:23:01,440 --> 05:23:02,700 this template 7174 05:23:02,700 --> 05:23:06,060 and to do that we need to use variables 7175 05:23:06,060 --> 05:23:09,298 first for the state or 7176 05:23:09,298 --> 05:23:11,160 or in the context of object orientated 7177 05:23:11,160 --> 05:23:12,900 programming and more specifically in the 7178 05:23:12,900 --> 05:23:14,458 context of 7179 05:23:14,458 --> 05:23:17,280 classes they are called the classes and 7180 05:23:17,280 --> 05:23:19,378 objects they are called properties 7181 05:23:19,378 --> 05:23:22,080 and we Define them like we Define a 7182 05:23:22,080 --> 05:23:24,840 simple variable we Type R and the next 7183 05:23:24,840 --> 05:23:28,260 the first thing that that a car has is a 7184 05:23:28,260 --> 05:23:29,940 name or brand name so we're gonna type 7185 05:23:29,940 --> 05:23:30,840 name 7186 05:23:30,840 --> 05:23:32,600 but we cannot keep this variable 7187 05:23:32,600 --> 05:23:34,440 uninitialized we need to provide the 7188 05:23:34,440 --> 05:23:35,760 value to it and I'm going to put an 7189 05:23:35,760 --> 05:23:37,560 empty string 7190 05:23:37,560 --> 05:23:39,660 for uh for now so it's gonna have the 7191 05:23:39,660 --> 05:23:42,120 default value an empty string 7192 05:23:42,120 --> 05:23:44,638 the next thing is the model what kind of 7193 05:23:44,638 --> 05:23:47,218 model this car is so it's going to be 7194 05:23:47,218 --> 05:23:49,320 also a string I'm gonna assign an empty 7195 05:23:49,320 --> 05:23:50,878 string 7196 05:23:50,878 --> 05:23:52,620 the next property is going to be called 7197 05:23:52,620 --> 05:23:55,080 the color it's going to be also an empty 7198 05:23:55,080 --> 05:23:57,120 string 7199 05:23:57,120 --> 05:23:58,500 and the next property is going to be 7200 05:23:58,500 --> 05:23:59,940 called doors so this is going to store 7201 05:23:59,940 --> 05:24:02,218 the number of doors that a specific 7202 05:24:02,218 --> 05:24:03,780 object which is going to be built with 7203 05:24:03,780 --> 05:24:05,760 this template is going to have and we're 7204 05:24:05,760 --> 05:24:08,718 gonna put zero I'm gonna put zero now 7205 05:24:08,718 --> 05:24:11,280 we need to Define some functionality for 7206 05:24:11,280 --> 05:24:14,040 this class and for the objects for an 7207 05:24:14,040 --> 05:24:15,840 implicit for the objects that are going 7208 05:24:15,840 --> 05:24:17,580 to be built with this class 7209 05:24:17,580 --> 05:24:21,718 and um I'm gonna add two functions first 7210 05:24:21,718 --> 05:24:24,980 one is going to be called move 7211 05:24:27,240 --> 05:24:29,160 it's not going to take any parameters 7212 05:24:29,160 --> 05:24:31,860 it's gonna just output some text to the 7213 05:24:31,860 --> 05:24:33,420 console which is going to say that the 7214 05:24:33,420 --> 05:24:36,660 car is moving so we put quotation marks 7215 05:24:36,660 --> 05:24:39,298 the card is moving 7216 05:24:39,298 --> 05:24:42,780 now I'm gonna Define another function 7217 05:24:42,780 --> 05:24:45,500 call stop 7218 05:24:47,280 --> 05:24:49,620 also it's not going to take any 7219 05:24:49,620 --> 05:24:52,080 parameters it's gonna just outpost on 7220 05:24:52,080 --> 05:24:53,940 this the console it's gonna it's gonna 7221 05:24:53,940 --> 05:24:57,377 say that the car has stopped 7222 05:25:00,900 --> 05:25:02,160 now 7223 05:25:02,160 --> 05:25:06,180 we have the template we defined what our 7224 05:25:06,180 --> 05:25:08,340 car is gonna 7225 05:25:08,340 --> 05:25:13,560 have now we need to create an actual or 7226 05:25:13,560 --> 05:25:16,860 object an actual and specific object 7227 05:25:16,860 --> 05:25:18,660 using this template and to do that to 7228 05:25:18,660 --> 05:25:20,638 firstly declare a variable 7229 05:25:20,638 --> 05:25:23,280 and it's going to be a Val and I'm going 7230 05:25:23,280 --> 05:25:25,980 to call it car one so so our object is 7231 05:25:25,980 --> 05:25:27,840 going to be stored in this variable car 7232 05:25:27,840 --> 05:25:28,740 one 7233 05:25:28,740 --> 05:25:32,760 then you put equals and we type car and 7234 05:25:32,760 --> 05:25:34,620 now we have some suggestions and we have 7235 05:25:34,620 --> 05:25:38,160 our car that we Define below our car 7236 05:25:38,160 --> 05:25:40,620 class so we press enter then we put 7237 05:25:40,620 --> 05:25:42,180 parenthesis 7238 05:25:42,180 --> 05:25:46,560 so now we have created an actual object 7239 05:25:46,560 --> 05:25:48,780 a real object real in the programming 7240 05:25:48,780 --> 05:25:49,620 sense 7241 05:25:49,620 --> 05:25:52,399 and 7242 05:25:52,620 --> 05:25:55,980 which but now this actual object was 7243 05:25:55,980 --> 05:25:58,440 also this is called the instantiation 7244 05:25:58,440 --> 05:26:01,200 because we created an actual instance 7245 05:26:01,200 --> 05:26:03,480 but we don't need to bother with these 7246 05:26:03,480 --> 05:26:05,100 names for now 7247 05:26:05,100 --> 05:26:08,218 so we created an actual object using 7248 05:26:08,218 --> 05:26:11,780 this template that we have below but the 7249 05:26:11,780 --> 05:26:14,940 the properties of these specific objects 7250 05:26:14,940 --> 05:26:17,520 are those properties that we Define here 7251 05:26:17,520 --> 05:26:20,580 those default properties and to Define 7252 05:26:20,580 --> 05:26:24,298 some specific profit some specific 7253 05:26:24,298 --> 05:26:28,080 values to these properties that this car 7254 05:26:28,080 --> 05:26:31,080 object that created has we need to type 7255 05:26:31,080 --> 05:26:32,540 car one 7256 05:26:32,540 --> 05:26:35,820 dot so this is called informally dot 7257 05:26:35,820 --> 05:26:37,080 notation 7258 05:26:37,080 --> 05:26:38,878 and now we have some suggestions so we 7259 05:26:38,878 --> 05:26:40,920 have our properties that we defined for 7260 05:26:40,920 --> 05:26:42,060 a car 7261 05:26:42,060 --> 05:26:44,400 and now we type car one dot and we 7262 05:26:44,400 --> 05:26:45,660 choose the name 7263 05:26:45,660 --> 05:26:47,160 and now I'm going to assign a value to 7264 05:26:47,160 --> 05:26:49,980 this name I'm going to be called Tesla 7265 05:26:49,980 --> 05:26:53,458 so now our car one 7266 05:26:53,458 --> 05:26:57,900 object that was created has the property 7267 05:26:57,900 --> 05:26:58,920 name 7268 05:26:58,920 --> 05:27:01,980 Tesla now we do the same thing for the 7269 05:27:01,980 --> 05:27:04,560 next property for the model car one dot 7270 05:27:04,560 --> 05:27:06,860 model 7271 05:27:08,940 --> 05:27:11,700 press enter 7272 05:27:11,700 --> 05:27:15,000 and you put we put here s blade so the 7273 05:27:15,000 --> 05:27:16,740 model 7274 05:27:16,740 --> 05:27:19,680 next we also type car one 7275 05:27:19,680 --> 05:27:23,218 Dot now we're gonna assign a value to 7276 05:27:23,218 --> 05:27:24,780 the property caller 7277 05:27:24,780 --> 05:27:27,900 it's going to be red 7278 05:27:27,900 --> 05:27:31,378 next we type also car one and we Define 7279 05:27:31,378 --> 05:27:36,440 for the property names for the property 7280 05:27:36,480 --> 05:27:39,540 doors also value so we're gonna put here 7281 05:27:39,540 --> 05:27:41,340 four 7282 05:27:41,340 --> 05:27:43,680 so now our object R1 7283 05:27:43,680 --> 05:27:47,040 has for its property for for its 7284 05:27:47,040 --> 05:27:49,080 properties some specific values 7285 05:27:49,080 --> 05:27:51,420 respectively have for the name Tesla for 7286 05:27:51,420 --> 05:27:53,878 the model as played for the car for the 7287 05:27:53,878 --> 05:27:56,820 color red and for the doors 4. 7288 05:27:56,820 --> 05:27:59,100 so now let's output the property values 7289 05:27:59,100 --> 05:28:01,320 of this object that we created in the 7290 05:28:01,320 --> 05:28:03,780 console and to do that we put our 7291 05:28:03,780 --> 05:28:05,878 println function down here we put 7292 05:28:05,878 --> 05:28:07,620 quotation marks inside the parenthesis 7293 05:28:07,620 --> 05:28:09,900 because I'm gonna add some text here so 7294 05:28:09,900 --> 05:28:12,440 I'm going to type name 7295 05:28:12,440 --> 05:28:16,020 equals we put dollar sign then input 7296 05:28:16,020 --> 05:28:17,820 curly braces and inside the calibration 7297 05:28:17,820 --> 05:28:18,718 type 7298 05:28:18,718 --> 05:28:21,480 car one dot 7299 05:28:21,480 --> 05:28:22,798 name 7300 05:28:22,798 --> 05:28:25,260 and this is going to return the 7301 05:28:25,260 --> 05:28:28,560 the value of the property name for the 7302 05:28:28,560 --> 05:28:30,060 car one object that we created 7303 05:28:30,060 --> 05:28:32,638 respectively is going to return Tesla 7304 05:28:32,638 --> 05:28:35,040 let's press Ctrl d three times to do the 7305 05:28:35,040 --> 05:28:36,958 same thing for the next properties and 7306 05:28:36,958 --> 05:28:40,080 here we put car one dot model so this is 7307 05:28:40,080 --> 05:28:41,340 gonna return the 7308 05:28:41,340 --> 05:28:45,240 value of the model property 7309 05:28:45,240 --> 05:28:47,878 you're gonna put color 7310 05:28:47,878 --> 05:28:49,740 and here 7311 05:28:49,740 --> 05:28:52,560 I'm gonna type doors 7312 05:28:52,560 --> 05:28:55,878 now if you run this code 7313 05:28:58,520 --> 05:29:01,620 we get name Tesla 7314 05:29:01,620 --> 05:29:05,280 actually let's change the text here to 7315 05:29:05,280 --> 05:29:09,120 model to match the property name 7316 05:29:09,120 --> 05:29:11,660 model 7317 05:29:13,080 --> 05:29:15,560 color 7318 05:29:16,860 --> 05:29:19,760 endorse 7319 05:29:22,218 --> 05:29:26,060 so now if you're on this 7320 05:29:26,820 --> 05:29:28,920 we get the name Tesla Model is played 7321 05:29:28,920 --> 05:29:32,420 color red and doors 4 and those are the 7322 05:29:32,420 --> 05:29:36,060 values of the properties of this object 7323 05:29:36,060 --> 05:29:38,700 that we created here with our 7324 05:29:38,700 --> 05:29:41,100 template car 7325 05:29:41,100 --> 05:29:43,920 next what you can also do because in the 7326 05:29:43,920 --> 05:29:46,200 class we Define the two function we can 7327 05:29:46,200 --> 05:29:49,620 call those functions on this object that 7328 05:29:49,620 --> 05:29:50,878 we created 7329 05:29:50,878 --> 05:29:53,638 and to do that we also use the dot 7330 05:29:53,638 --> 05:29:55,560 notation we type car one 7331 05:29:55,560 --> 05:29:58,798 dot move and this is going to call the 7332 05:29:58,798 --> 05:30:00,600 move function 7333 05:30:00,600 --> 05:30:03,420 which is inside the car one 7334 05:30:03,420 --> 05:30:04,860 object 7335 05:30:04,860 --> 05:30:06,420 let's also 7336 05:30:06,420 --> 05:30:09,180 type car one dot stop to call the store 7337 05:30:09,180 --> 05:30:12,660 function so now if you run this code 7338 05:30:12,660 --> 05:30:14,400 we get uh 7339 05:30:14,400 --> 05:30:16,920 the values of our to our properties and 7340 05:30:16,920 --> 05:30:19,260 then we get the car is moving and the 7341 05:30:19,260 --> 05:30:21,958 car has stopped so the car one dot move 7342 05:30:21,958 --> 05:30:23,878 and car one the stop is calling the 7343 05:30:23,878 --> 05:30:25,378 functions 7344 05:30:25,378 --> 05:30:29,520 on this object that we created here now 7345 05:30:29,520 --> 05:30:31,320 let's uh 7346 05:30:31,320 --> 05:30:34,500 also add inside the functions 7347 05:30:34,500 --> 05:30:38,100 the name of the card that is moving so 7348 05:30:38,100 --> 05:30:40,138 he to do that with here dollar sign 7349 05:30:40,138 --> 05:30:42,120 inside the class 7350 05:30:42,120 --> 05:30:44,458 and we type name 7351 05:30:44,458 --> 05:30:47,400 and this is gonna reference this 7352 05:30:47,400 --> 05:30:51,000 property name that is defined here 7353 05:30:51,000 --> 05:30:53,760 let's also do the same thing for the 7354 05:30:53,760 --> 05:30:56,760 stop function let's type name 7355 05:30:56,760 --> 05:30:58,860 and now what this is gonna do because 7356 05:30:58,860 --> 05:31:02,218 those functions are in they are they're 7357 05:31:02,218 --> 05:31:04,080 living inside this object that we 7358 05:31:04,080 --> 05:31:07,680 created here car one and this object has 7359 05:31:07,680 --> 05:31:11,340 the name Tesla when those functions are 7360 05:31:11,340 --> 05:31:12,840 going to be called 7361 05:31:12,840 --> 05:31:15,480 they they gonna they're gonna say that 7362 05:31:15,480 --> 05:31:17,458 the car 7363 05:31:17,458 --> 05:31:20,100 and the name of the car which is in our 7364 05:31:20,100 --> 05:31:23,280 case for our objectives Tesla is moving 7365 05:31:23,280 --> 05:31:25,020 and then it's gonna say 7366 05:31:25,020 --> 05:31:28,260 the car again Tesla has stopped because 7367 05:31:28,260 --> 05:31:31,680 they are calling this uh specific object 7368 05:31:31,680 --> 05:31:35,820 that we created which has uh those 7369 05:31:35,820 --> 05:31:38,700 specific values for its properties 7370 05:31:38,700 --> 05:31:42,680 all right so now if you run this quad 7371 05:31:43,620 --> 05:31:46,440 we get as expected the car Tesla is 7372 05:31:46,440 --> 05:31:48,840 moving and the car Tesla has stopped so 7373 05:31:48,840 --> 05:31:51,958 the functions are called on this object 7374 05:31:51,958 --> 05:31:54,180 that we Define here and it's using the 7375 05:31:54,180 --> 05:31:55,980 property values that we Define for this 7376 05:31:55,980 --> 05:31:56,940 object 7377 05:31:56,940 --> 05:32:00,120 but as I said with uh you can create as 7378 05:32:00,120 --> 05:32:03,000 many objects as you want using the same 7379 05:32:03,000 --> 05:32:05,040 template and let's do that let's create 7380 05:32:05,040 --> 05:32:07,080 another object so I'm going to type here 7381 05:32:07,080 --> 05:32:08,400 Val 7382 05:32:08,400 --> 05:32:10,020 Car Tool 7383 05:32:10,020 --> 05:32:13,260 let's put equals we type car and then we 7384 05:32:13,260 --> 05:32:15,000 put parenthesis 7385 05:32:15,000 --> 05:32:17,580 and this create is now another object 7386 05:32:17,580 --> 05:32:19,680 but we need to Define some values to its 7387 05:32:19,680 --> 05:32:21,180 properties because now the values for 7388 05:32:21,180 --> 05:32:23,280 the properties are the default values 7389 05:32:23,280 --> 05:32:24,540 that we Define inside the class 7390 05:32:24,540 --> 05:32:26,958 respectively the empty string and the 7391 05:32:26,958 --> 05:32:28,680 zero 7392 05:32:28,680 --> 05:32:31,378 and to do that we type here car2 7393 05:32:31,378 --> 05:32:33,660 dot name 7394 05:32:33,660 --> 05:32:36,718 so now we are calling the properties 7395 05:32:36,718 --> 05:32:39,660 for the car 2 object and now we Define 7396 05:32:39,660 --> 05:32:41,520 some values to the cut to the properties 7397 05:32:41,520 --> 05:32:44,100 of the car 2 object so they are 7398 05:32:44,100 --> 05:32:46,620 this here is not overending 7399 05:32:46,620 --> 05:32:48,840 the value that we have here because 7400 05:32:48,840 --> 05:32:50,638 this 7401 05:32:50,638 --> 05:32:52,680 thing that we have here it's a distinct 7402 05:32:52,680 --> 05:32:55,440 object in memory uh from this object 7403 05:32:55,440 --> 05:32:57,420 they are this thing distinct objects and 7404 05:32:57,420 --> 05:32:59,218 they have their own properties with 7405 05:32:59,218 --> 05:33:01,798 their own values and their own functions 7406 05:33:01,798 --> 05:33:06,138 and now let's put here Ford 7407 05:33:07,920 --> 05:33:13,080 let's put the car to that model 7408 05:33:13,080 --> 05:33:16,100 let's put Mustang 7409 05:33:17,400 --> 05:33:19,798 let's put 7410 05:33:19,798 --> 05:33:22,620 car to that 7411 05:33:22,620 --> 05:33:25,218 color 7412 05:33:25,378 --> 05:33:27,240 blue 7413 05:33:27,240 --> 05:33:30,060 and for the doors let's put two 7414 05:33:30,060 --> 05:33:32,700 car two that doors 7415 05:33:32,700 --> 05:33:36,780 equals to two and let's output those 7416 05:33:36,780 --> 05:33:39,000 values in the console so let's output 7417 05:33:39,000 --> 05:33:41,940 the values of the properties for this 7418 05:33:41,940 --> 05:33:43,740 second object that we created in the 7419 05:33:43,740 --> 05:33:46,080 console and to that to do that I'm going 7420 05:33:46,080 --> 05:33:48,420 to copy this code 7421 05:33:48,420 --> 05:33:51,718 I'm gonna paste the paste it below here 7422 05:33:51,718 --> 05:33:53,820 and now I'm just gonna change 7423 05:33:53,820 --> 05:33:56,400 the 7424 05:33:56,400 --> 05:34:00,420 the variable to car two to to call to 7425 05:34:00,420 --> 05:34:01,680 call the properties on the cartoon 7426 05:34:01,680 --> 05:34:05,840 object so we put here car2 7427 05:34:09,718 --> 05:34:11,400 so now we're calling the properties 7428 05:34:11,400 --> 05:34:15,240 values on our car to object 7429 05:34:15,240 --> 05:34:16,920 and now if 7430 05:34:16,920 --> 05:34:19,877 I run this code 7431 05:34:20,060 --> 05:34:24,000 now we get name Ford Model Mustang color 7432 05:34:24,000 --> 05:34:26,760 blue endorseful and the first part is 7433 05:34:26,760 --> 05:34:28,680 from the our SEC for from our first 7434 05:34:28,680 --> 05:34:30,540 object which is named Tesla Model let's 7435 05:34:30,540 --> 05:34:32,820 play The Red Doors 4 and the Carter 7436 05:34:32,820 --> 05:34:34,138 slice movie and the cards has stopped 7437 05:34:34,138 --> 05:34:37,080 but let's add a space here 7438 05:34:37,080 --> 05:34:40,940 to make a clear uh to make a clear which 7439 05:34:40,940 --> 05:34:44,878 to make uh clear that this is the output 7440 05:34:44,878 --> 05:34:46,740 for the second object to make things 7441 05:34:46,740 --> 05:34:48,840 more clear and to add the space we put 7442 05:34:48,840 --> 05:34:52,080 here a printerland 7443 05:34:52,080 --> 05:34:55,440 statement we put quotation marks and we 7444 05:34:55,440 --> 05:34:58,458 put a backslash 7445 05:34:59,100 --> 05:35:00,780 n and this is going to add a space 7446 05:35:00,780 --> 05:35:03,718 between our println statements so it's 7447 05:35:03,718 --> 05:35:05,520 going to add a space between those 7448 05:35:05,520 --> 05:35:09,840 statements and we uh and those so now if 7449 05:35:09,840 --> 05:35:12,500 you run this code 7450 05:35:14,820 --> 05:35:17,218 let's increase this 7451 05:35:17,218 --> 05:35:19,440 we get the name Tesla so this is our 7452 05:35:19,440 --> 05:35:20,820 first object model let's play it called 7453 05:35:20,820 --> 05:35:22,980 Red Doors 4 The Carter size moving 7454 05:35:22,980 --> 05:35:24,958 because we're calling the functions on 7455 05:35:24,958 --> 05:35:27,600 the first object and the character has 7456 05:35:27,600 --> 05:35:30,480 stopped and then we get name Ford Model 7457 05:35:30,480 --> 05:35:32,878 Mustang color blue Doors 2 and we have 7458 05:35:32,878 --> 05:35:34,920 our space because you put here this 7459 05:35:34,920 --> 05:35:37,860 backflash and which adds that space for 7460 05:35:37,860 --> 05:35:38,820 us 7461 05:35:38,820 --> 05:35:42,360 let's um decrease this 7462 05:35:42,360 --> 05:35:45,780 and let's also call the functions the 7463 05:35:45,780 --> 05:35:48,060 move install functions on the second 7464 05:35:48,060 --> 05:35:49,980 object so I'm going to type here car2 7465 05:35:49,980 --> 05:35:52,500 that move 7466 05:35:52,500 --> 05:35:56,160 and card two dot stop 7467 05:35:56,160 --> 05:35:58,020 so now if you run this 7468 05:35:58,020 --> 05:35:59,280 let's 7469 05:35:59,280 --> 05:36:02,400 right click and just click on run 7470 05:36:02,400 --> 05:36:05,400 main.kt 7471 05:36:07,860 --> 05:36:09,420 now get 7472 05:36:09,420 --> 05:36:13,320 our first object which is with this uh 7473 05:36:13,320 --> 05:36:16,440 value for the value of the value for its 7474 05:36:16,440 --> 05:36:18,180 properties and then we get the two 7475 05:36:18,180 --> 05:36:21,000 functions called on our first object and 7476 05:36:21,000 --> 05:36:21,718 um 7477 05:36:21,718 --> 05:36:24,298 we get in our second object which is 7478 05:36:24,298 --> 05:36:25,980 values for its properties and then we 7479 05:36:25,980 --> 05:36:28,620 get the card for this moving and the car 7480 05:36:28,620 --> 05:36:31,260 Ford has stopped because now 7481 05:36:31,260 --> 05:36:32,160 um 7482 05:36:32,160 --> 05:36:34,500 we're calling the functions on the this 7483 05:36:34,500 --> 05:36:37,500 second object and this second object has 7484 05:36:37,500 --> 05:36:39,958 the value for the properties has has 7485 05:36:39,958 --> 05:36:42,360 different values for its properties so 7486 05:36:42,360 --> 05:36:44,638 this is why you get the card for this 7487 05:36:44,638 --> 05:36:46,740 movie and the car four has stopped again 7488 05:36:46,740 --> 05:36:48,440 they are different 7489 05:36:48,440 --> 05:36:51,240 objects they are distinct objects 7490 05:36:51,240 --> 05:36:54,540 created using the same template the same 7491 05:36:54,540 --> 05:36:56,458 construction plan 7492 05:36:56,458 --> 05:36:58,260 and 7493 05:36:58,260 --> 05:37:00,718 I'm gonna end this video now and see you 7494 05:37:00,718 --> 05:37:01,860 in the next video 7495 05:37:01,860 --> 05:37:03,718 so in the last video we saw how we can 7496 05:37:03,718 --> 05:37:05,638 create a class and then how we can 7497 05:37:05,638 --> 05:37:08,040 Define some properties and some 7498 05:37:08,040 --> 05:37:10,320 functionality to that class then we use 7499 05:37:10,320 --> 05:37:13,080 that class to create two objects and 7500 05:37:13,080 --> 05:37:15,780 then we defined for those objects for 7501 05:37:15,780 --> 05:37:17,638 the property of those objects some 7502 05:37:17,638 --> 05:37:21,360 values but the way we we Define values 7503 05:37:21,360 --> 05:37:23,340 to the properties of the object is to 7504 05:37:23,340 --> 05:37:26,218 call the the object and then the 7505 05:37:26,218 --> 05:37:27,780 property of logic and then we assign a 7506 05:37:27,780 --> 05:37:30,660 value and this is okay but if you have 7507 05:37:30,660 --> 05:37:34,200 more than than four properties let's say 7508 05:37:34,200 --> 05:37:36,240 you have 20 or 100 properties that will 7509 05:37:36,240 --> 05:37:38,820 be very tedious to write each one of 7510 05:37:38,820 --> 05:37:40,560 them and then assign a value 7511 05:37:40,560 --> 05:37:43,200 and a better way is to use what is 7512 05:37:43,200 --> 05:37:45,240 called a Constructor and as the name 7513 05:37:45,240 --> 05:37:46,920 implied The Constructor is used to 7514 05:37:46,920 --> 05:37:48,900 construct the object and the Constructor 7515 05:37:48,900 --> 05:37:51,000 is called at the time when we create a 7516 05:37:51,000 --> 05:37:53,458 new object so at the time of line we we 7517 05:37:53,458 --> 05:37:55,920 type this line of code when you finish 7518 05:37:55,920 --> 05:37:57,958 to type this line of code The 7519 05:37:57,958 --> 05:38:00,298 Constructor is called immediately so 7520 05:38:00,298 --> 05:38:02,218 that is the 7521 05:38:02,218 --> 05:38:05,580 the time where we need to pass the 7522 05:38:05,580 --> 05:38:07,440 values to our properties and to do that 7523 05:38:07,440 --> 05:38:09,540 we go down here where we Define our 7524 05:38:09,540 --> 05:38:11,218 class 7525 05:38:11,218 --> 05:38:12,718 we put 7526 05:38:12,718 --> 05:38:16,740 at the at the end of our 7527 05:38:16,740 --> 05:38:18,600 end of the name of the class we put 7528 05:38:18,600 --> 05:38:20,100 parenthesis and here we Define some 7529 05:38:20,100 --> 05:38:21,900 parameters the way we Define parameters 7530 05:38:21,900 --> 05:38:23,218 for functions 7531 05:38:23,218 --> 05:38:24,958 and the first one is going to be called 7532 05:38:24,958 --> 05:38:27,718 name it's going to be a string 7533 05:38:27,718 --> 05:38:30,420 the second one is going to be model 7534 05:38:30,420 --> 05:38:33,500 also string 7535 05:38:35,760 --> 05:38:37,200 the next one 7536 05:38:37,200 --> 05:38:40,020 color also string 7537 05:38:40,020 --> 05:38:42,420 and the last one is going to be doors 7538 05:38:42,420 --> 05:38:45,060 and this is going to be an integer 7539 05:38:45,060 --> 05:38:47,878 no we need to 7540 05:38:47,878 --> 05:38:50,458 assign whatever value values pass to 7541 05:38:50,458 --> 05:38:52,860 those parameters to the properties of 7542 05:38:52,860 --> 05:38:54,958 the class here and to do that just 7543 05:38:54,958 --> 05:38:57,000 delete the empty string here and type 7544 05:38:57,000 --> 05:38:59,218 name 7545 05:38:59,218 --> 05:39:01,200 and as I'm typing you see that we have 7546 05:39:01,200 --> 05:39:04,680 name and it has this p and this P stands 7547 05:39:04,680 --> 05:39:07,200 for prop for a parameter so you're 7548 05:39:07,200 --> 05:39:09,718 saying do you mean this parameter that 7549 05:39:09,718 --> 05:39:12,540 we Define up here and then we press 7550 05:39:12,540 --> 05:39:15,298 enter to fill that for us 7551 05:39:15,298 --> 05:39:19,638 then put we type here model 7552 05:39:20,638 --> 05:39:22,860 color 7553 05:39:22,860 --> 05:39:24,540 and then 7554 05:39:24,540 --> 05:39:26,160 doors 7555 05:39:26,160 --> 05:39:27,780 now if you go up here we have some 7556 05:39:27,780 --> 05:39:31,680 errors because now the the 7557 05:39:31,680 --> 05:39:33,718 when you create the object the objects 7558 05:39:33,718 --> 05:39:37,080 is expecting for its Constructor some 7559 05:39:37,080 --> 05:39:39,420 values so if you hover over here inside 7560 05:39:39,420 --> 05:39:42,900 the parenthesis it says no value fast 7561 05:39:42,900 --> 05:39:45,360 for parameter color Doors Model and name 7562 05:39:45,360 --> 05:39:47,700 so if you and if you look down here you 7563 05:39:47,700 --> 05:39:50,340 see that it has this public Constructor 7564 05:39:50,340 --> 05:39:52,440 so now what we need to do is to pass the 7565 05:39:52,440 --> 05:39:54,718 value that you passed down here to to 7566 05:39:54,718 --> 05:39:57,360 the properties by calling the 7567 05:39:57,360 --> 05:39:59,340 calling them on the object is to pass 7568 05:39:59,340 --> 05:40:01,680 them directly in the Constructor and 7569 05:40:01,680 --> 05:40:04,080 then the values the arguments if you 7570 05:40:04,080 --> 05:40:05,878 want are going to be assigned to the 7571 05:40:05,878 --> 05:40:09,420 properties so I'm going to put here 7572 05:40:09,420 --> 05:40:13,860 and you have this hint name Tesla 7573 05:40:13,860 --> 05:40:17,100 then we put a comma 7574 05:40:17,100 --> 05:40:20,100 so we have the again the hint which is 7575 05:40:20,100 --> 05:40:23,340 very helpful so for the model we put a 7576 05:40:23,340 --> 05:40:25,820 splade 7577 05:40:27,060 --> 05:40:30,120 and then for the color 7578 05:40:30,120 --> 05:40:32,700 we put red 7579 05:40:32,700 --> 05:40:36,440 and for the number of doors four 7580 05:40:36,480 --> 05:40:39,480 all right so now this is more concise 7581 05:40:39,480 --> 05:40:42,420 and now this is doing the exact thing 7582 05:40:42,420 --> 05:40:44,280 that we did previously but this is more 7583 05:40:44,280 --> 05:40:47,280 concise and we're using the Constructor 7584 05:40:47,280 --> 05:40:51,958 to pass the those values to the 7585 05:40:51,958 --> 05:40:54,298 parameters that we Define here and then 7586 05:40:54,298 --> 05:40:55,740 those parameters going to be assigned to 7587 05:40:55,740 --> 05:40:57,480 the properties respectively with those 7588 05:40:57,480 --> 05:40:59,218 properties name model core and doors 7589 05:40:59,218 --> 05:41:01,260 you're gonna see what is the difference 7590 05:41:01,260 --> 05:41:05,280 between the properties and parameters in 7591 05:41:05,280 --> 05:41:09,060 uh in the next video now we have also an 7592 05:41:09,060 --> 05:41:10,520 error here because 7593 05:41:10,520 --> 05:41:13,138 this also express some values to its 7594 05:41:13,138 --> 05:41:15,718 Constructor so instead of putting those 7595 05:41:15,718 --> 05:41:18,420 values here we put up here 7596 05:41:18,420 --> 05:41:21,138 Ford 7597 05:41:24,000 --> 05:41:26,900 Mustang 7598 05:41:29,760 --> 05:41:32,480 blue 7599 05:41:32,700 --> 05:41:34,200 and 7600 05:41:34,200 --> 05:41:35,520 two 7601 05:41:35,520 --> 05:41:37,860 now we can delete this 7602 05:41:37,860 --> 05:41:40,500 because we already passed the values for 7603 05:41:40,500 --> 05:41:43,320 this second object for its properties 7604 05:41:43,320 --> 05:41:45,480 now if you look at the codes the code is 7605 05:41:45,480 --> 05:41:50,040 more concise and more and easier to read 7606 05:41:50,040 --> 05:41:52,260 now if you run this code 7607 05:41:52,260 --> 05:41:55,260 you will have the same output 7608 05:41:55,260 --> 05:41:57,480 because the code 7609 05:41:57,480 --> 05:42:00,378 works well 7610 05:42:02,480 --> 05:42:07,200 so now if you look at the console let's 7611 05:42:07,200 --> 05:42:09,120 increase this a little bit 7612 05:42:09,120 --> 05:42:11,218 let's scroll up you have the name Tesla 7613 05:42:11,218 --> 05:42:13,440 Model let's Place color red doors4 the 7614 05:42:13,440 --> 05:42:15,000 cartridge lights moving the cut Isla has 7615 05:42:15,000 --> 05:42:17,040 stopped so you have the same output as 7616 05:42:17,040 --> 05:42:19,798 previously and down for the second 7617 05:42:19,798 --> 05:42:22,699 object we have 7618 05:42:22,798 --> 05:42:24,540 we have a 7619 05:42:24,540 --> 05:42:26,638 the name Ford the model Mustang the 7620 05:42:26,638 --> 05:42:28,500 color blue Doors 2 and the Carphone is 7621 05:42:28,500 --> 05:42:30,180 moving in a car 4 has stopped so our 7622 05:42:30,180 --> 05:42:34,378 code works well now let's close the 7623 05:42:34,378 --> 05:42:37,020 console but if you go down here you'll 7624 05:42:37,020 --> 05:42:39,000 see that you have this um 7625 05:42:39,000 --> 05:42:41,638 this uh the properties colored so if you 7626 05:42:41,638 --> 05:42:43,200 hover over them it says the property is 7627 05:42:43,200 --> 05:42:45,540 excretely assigned to parameter name so 7628 05:42:45,540 --> 05:42:47,040 it can be declared directly in 7629 05:42:47,040 --> 05:42:48,298 Constructor 7630 05:42:48,298 --> 05:42:51,660 now what is saying here in other words 7631 05:42:51,660 --> 05:42:54,000 is that we can put the properties 7632 05:42:54,000 --> 05:42:56,100 directly in the Constructor so you don't 7633 05:42:56,100 --> 05:42:58,620 need to define the properties down here 7634 05:42:58,620 --> 05:43:01,260 and then assign the value that is passed 7635 05:43:01,260 --> 05:43:05,218 to the parameter to the property and 7636 05:43:05,218 --> 05:43:07,740 the difference between the properties 7637 05:43:07,740 --> 05:43:10,620 and parameters parameters is that the 7638 05:43:10,620 --> 05:43:12,360 properties respectively those that we 7639 05:43:12,360 --> 05:43:14,760 Define in the in the header of the class 7640 05:43:14,760 --> 05:43:16,940 inside the parentheses 0 7641 05:43:16,940 --> 05:43:19,320 in the Constructor 7642 05:43:19,320 --> 05:43:20,940 is that 7643 05:43:20,940 --> 05:43:24,958 the properties are not uh storing the 7644 05:43:24,958 --> 05:43:28,080 state of of the class they are mastering 7645 05:43:28,080 --> 05:43:29,760 the state of the object there they are 7646 05:43:29,760 --> 05:43:32,700 only here to to get whatever value we 7647 05:43:32,700 --> 05:43:34,138 pass here 7648 05:43:34,138 --> 05:43:36,840 so whatever value you pass here 7649 05:43:36,840 --> 05:43:39,060 and his job is to only get that value 7650 05:43:39,060 --> 05:43:41,638 and assign it to the property name so 7651 05:43:41,638 --> 05:43:44,100 only the the variables which are 7652 05:43:44,100 --> 05:43:47,540 declared declared inside the class are 7653 05:43:47,540 --> 05:43:51,240 storing ultimately the the state of that 7654 05:43:51,240 --> 05:43:54,660 object so uh if I delete let's say if I 7655 05:43:54,660 --> 05:43:57,620 delete this here 7656 05:43:57,780 --> 05:44:01,320 now we have an error here because the 7657 05:44:01,320 --> 05:44:03,900 property name um it says on our result 7658 05:44:03,900 --> 05:44:06,360 reference name create member property 7659 05:44:06,360 --> 05:44:09,058 card.name and that is because the 7660 05:44:09,058 --> 05:44:12,780 property name is not declared it's not 7661 05:44:12,780 --> 05:44:16,820 exit it's not it's not a it's not uh 7662 05:44:16,820 --> 05:44:20,280 it's not a characteristic of a car we 7663 05:44:20,280 --> 05:44:23,160 didn't Define a property name because 7664 05:44:23,160 --> 05:44:26,120 here the parameters the parameter name 7665 05:44:26,120 --> 05:44:29,340 is only there to get the value and then 7666 05:44:29,340 --> 05:44:31,860 pass the value to the property 7667 05:44:31,860 --> 05:44:36,540 but we can let's press Ctrl Z to put the 7668 05:44:36,540 --> 05:44:37,680 code 7669 05:44:37,680 --> 05:44:39,120 back 7670 05:44:39,120 --> 05:44:40,920 now the error disappeared because we 7671 05:44:40,920 --> 05:44:43,558 have the property declared but because 7672 05:44:43,558 --> 05:44:46,680 the only thing that we do here is to to 7673 05:44:46,680 --> 05:44:48,900 to get the value that is passed to the 7674 05:44:48,900 --> 05:44:51,718 property as an argument here and for the 7675 05:44:51,718 --> 05:44:54,240 next ones and then assign it to the 7676 05:44:54,240 --> 05:44:57,540 property we can put those directly in 7677 05:44:57,540 --> 05:45:00,780 the Constructor so to do that we just we 7678 05:45:00,780 --> 05:45:04,378 can hover over here and 7679 05:45:04,378 --> 05:45:06,840 let's hover over 7680 05:45:06,840 --> 05:45:09,360 and click on this light bulb and click 7681 05:45:09,360 --> 05:45:11,760 move to Constructor so now you have this 7682 05:45:11,760 --> 05:45:13,440 you have the same 7683 05:45:13,440 --> 05:45:16,440 the same thing the same thing but the 7684 05:45:16,440 --> 05:45:18,420 only difference is that we have this VAR 7685 05:45:18,420 --> 05:45:20,940 keyword in front and that is responsible 7686 05:45:20,940 --> 05:45:23,458 for changing the 7687 05:45:23,458 --> 05:45:26,520 changing the parameter into a property 7688 05:45:26,520 --> 05:45:27,718 so 7689 05:45:27,718 --> 05:45:29,280 let's do the same thing for the next 7690 05:45:29,280 --> 05:45:30,540 ones 7691 05:45:30,540 --> 05:45:33,620 move to Constructor 7692 05:45:33,718 --> 05:45:35,700 and now we don't have that error that we 7693 05:45:35,700 --> 05:45:37,740 don't have a property for our class 7694 05:45:37,740 --> 05:45:40,440 because never because they have the VAR 7695 05:45:40,440 --> 05:45:42,900 so if you put the VAR or the VAR keyword 7696 05:45:42,900 --> 05:45:46,138 before declaring the the the parameter 7697 05:45:46,138 --> 05:45:48,420 it is going to be converted into a 7698 05:45:48,420 --> 05:45:49,320 property 7699 05:45:49,320 --> 05:45:53,160 so now those are properties of our class 7700 05:45:53,160 --> 05:45:55,620 car so they are storing actually they 7701 05:45:55,620 --> 05:45:57,718 are storing the the value that you pass 7702 05:45:57,718 --> 05:46:00,240 to to them 7703 05:46:00,240 --> 05:46:03,660 for the project that we create so uh 7704 05:46:03,660 --> 05:46:05,520 it's not like previously where you have 7705 05:46:05,520 --> 05:46:08,520 the property the parameter and then the 7706 05:46:08,520 --> 05:46:10,378 value is passed to the property inside 7707 05:46:10,378 --> 05:46:11,700 the class 7708 05:46:11,700 --> 05:46:15,020 so now if you run this code 7709 05:46:24,298 --> 05:46:25,440 foreign 7710 05:46:25,440 --> 05:46:27,900 we have the exact same output 7711 05:46:27,900 --> 05:46:30,360 the only difference now this that this 7712 05:46:30,360 --> 05:46:32,218 is uh more concise we have the 7713 05:46:32,218 --> 05:46:33,660 properties declared the right directly 7714 05:46:33,660 --> 05:46:35,040 in the Constructor so we don't have 7715 05:46:35,040 --> 05:46:37,620 declare the parameters and then we 7716 05:46:37,620 --> 05:46:39,120 assign the value that is passed to those 7717 05:46:39,120 --> 05:46:41,520 parameters to properties so you can 7718 05:46:41,520 --> 05:46:44,160 declare the The Constructor like this if 7719 05:46:44,160 --> 05:46:46,200 you want so the question may arise when 7720 05:46:46,200 --> 05:46:49,080 to use properties and when to use 7721 05:46:49,080 --> 05:46:53,580 parameters and to to 7722 05:46:53,580 --> 05:46:55,500 to answer that question 7723 05:46:55,500 --> 05:46:56,900 um you have you need to have in mind 7724 05:46:56,900 --> 05:47:00,298 that if you want to do some if you want 7725 05:47:00,298 --> 05:47:01,860 to do some work before you assign the 7726 05:47:01,860 --> 05:47:03,540 value to the property 7727 05:47:03,540 --> 05:47:06,600 because in our case uh the value that is 7728 05:47:06,600 --> 05:47:09,540 passed here to the to the 7729 05:47:09,540 --> 05:47:12,180 property in this case is directly stored 7730 05:47:12,180 --> 05:47:14,878 in the property so we cannot add some 7731 05:47:14,878 --> 05:47:18,240 validation to check to see if uh I don't 7732 05:47:18,240 --> 05:47:21,600 know if this name is if it starts to a 7733 05:47:21,600 --> 05:47:24,298 specific letter or if it's if it's an 7734 05:47:24,298 --> 05:47:26,760 uppercase or because the value that is 7735 05:47:26,760 --> 05:47:27,900 passed 7736 05:47:27,900 --> 05:47:30,360 that is passed as an argument to the 7737 05:47:30,360 --> 05:47:32,638 property there I say property because 7738 05:47:32,638 --> 05:47:34,558 they are there there are no declared as 7739 05:47:34,558 --> 05:47:38,760 properties it is directly stored as uh 7740 05:47:38,760 --> 05:47:42,900 as a the state of the object so 7741 05:47:42,900 --> 05:47:46,138 if you want to to do some work before 7742 05:47:46,138 --> 05:47:47,760 you assign the value of the property you 7743 05:47:47,760 --> 05:47:50,160 need to declare it let's say let's say 7744 05:47:50,160 --> 05:47:52,260 that you want to Let's actually delete 7745 05:47:52,260 --> 05:47:56,340 this and I'm going to declare it as a 7746 05:47:56,340 --> 05:48:00,000 parameter so let's say that you want to 7747 05:48:00,000 --> 05:48:02,878 to to trim the space that is passed so 7748 05:48:02,878 --> 05:48:06,320 let's say that I put some spaces here 7749 05:48:09,840 --> 05:48:12,660 let's actually press Ctrl G to see what 7750 05:48:12,660 --> 05:48:16,400 is happening if I don't do that 7751 05:48:19,740 --> 05:48:22,200 all right 7752 05:48:22,200 --> 05:48:26,700 I press Ctrl Z too many times so 7753 05:48:26,700 --> 05:48:29,340 let's let's again Paul let's put some 7754 05:48:29,340 --> 05:48:33,680 spaces here so now if you run this code 7755 05:48:37,500 --> 05:48:40,820 let's run it again 7756 05:48:44,800 --> 05:48:46,980 [Music] 7757 05:48:46,980 --> 05:48:51,000 now because the value that is passed 7758 05:48:51,000 --> 05:48:53,760 as an argument here to the object car 7759 05:48:53,760 --> 05:48:55,260 one 7760 05:48:55,260 --> 05:48:58,080 is passed directly to the property so 7761 05:48:58,080 --> 05:49:00,058 it's stored in the property and we we 7762 05:49:00,058 --> 05:49:03,298 don't do nothing with it we have also 7763 05:49:03,298 --> 05:49:05,878 the space but if you want to to delete 7764 05:49:05,878 --> 05:49:08,520 that space currently we can do that 7765 05:49:08,520 --> 05:49:10,798 because the values pass directly to the 7766 05:49:10,798 --> 05:49:12,900 property and there is nothing that we 7767 05:49:12,900 --> 05:49:14,820 can do from that point 7768 05:49:14,820 --> 05:49:18,600 and to to change that we can declare the 7769 05:49:18,600 --> 05:49:21,180 the property that is defined here the 7770 05:49:21,180 --> 05:49:24,000 property name as a parameter first 7771 05:49:24,000 --> 05:49:27,780 and we can declare it down here 7772 05:49:27,780 --> 05:49:29,580 we can declare down here now the 7773 05:49:29,580 --> 05:49:31,138 property 7774 05:49:31,138 --> 05:49:35,218 and we can put equals and now we we pass 7775 05:49:35,218 --> 05:49:38,000 the the 7776 05:49:38,000 --> 05:49:41,100 parameter to the property so it's a sign 7777 05:49:41,100 --> 05:49:42,360 name here 7778 05:49:42,360 --> 05:49:45,718 but before before that value is assigned 7779 05:49:45,718 --> 05:49:48,958 to the property name we put that 7780 05:49:48,958 --> 05:49:50,878 stream 7781 05:49:50,878 --> 05:49:52,740 and that is going to remove the spaces 7782 05:49:52,740 --> 05:49:54,958 so that Rim is a function which is part 7783 05:49:54,958 --> 05:49:59,100 of the string class because the the data 7784 05:49:59,100 --> 05:50:01,440 the all the data size that we looked at 7785 05:50:01,440 --> 05:50:04,798 are also classes so you can call 7786 05:50:04,798 --> 05:50:06,600 functions on them the way we called 7787 05:50:06,600 --> 05:50:10,020 functions so not our on our objects 7788 05:50:10,020 --> 05:50:13,798 so name that trim removes the space so 7789 05:50:13,798 --> 05:50:16,378 in this case it removes the space that 7790 05:50:16,378 --> 05:50:17,520 we have here 7791 05:50:17,520 --> 05:50:21,058 and after that it's assigning the value 7792 05:50:21,058 --> 05:50:24,298 to the name property so now if you run 7793 05:50:24,298 --> 05:50:26,520 this code now now we have the space but 7794 05:50:26,520 --> 05:50:27,958 if you run the code 7795 05:50:27,958 --> 05:50:30,420 so let's go up 7796 05:50:30,420 --> 05:50:33,260 run the code 7797 05:50:39,420 --> 05:50:41,340 now as you can see the space is removed 7798 05:50:41,340 --> 05:50:42,980 because 7799 05:50:42,980 --> 05:50:46,080 we pass the value to the 7800 05:50:46,080 --> 05:50:49,138 parameter now name then the parameter 7801 05:50:49,138 --> 05:50:52,080 name trims.space and after that it's 7802 05:50:52,080 --> 05:50:54,958 assign the value to the property name so 7803 05:50:54,958 --> 05:50:57,600 in this case so in this can be a 7804 05:50:57,600 --> 05:51:00,000 scenario where you can uh where you can 7805 05:51:00,000 --> 05:51:02,040 declare the instead of declaring 7806 05:51:02,040 --> 05:51:03,480 directly here the property can declare 7807 05:51:03,480 --> 05:51:07,260 as a parameter but what do what what you 7808 05:51:07,260 --> 05:51:09,840 can do if you want to add more code than 7809 05:51:09,840 --> 05:51:11,820 just one line of code in that case we'll 7810 05:51:11,820 --> 05:51:13,260 need to use what is called the 7811 05:51:13,260 --> 05:51:16,320 initializer blocks but 7812 05:51:16,320 --> 05:51:17,400 um we're going to look at the 7813 05:51:17,400 --> 05:51:20,760 initializer blocks and uh other things 7814 05:51:20,760 --> 05:51:22,680 in the next video because this video is 7815 05:51:22,680 --> 05:51:25,740 long enough so see you in the next video 7816 05:51:25,740 --> 05:51:28,160 so in the last video we looked at 7817 05:51:28,160 --> 05:51:30,660 Constructors and how you can use 7818 05:51:30,660 --> 05:51:33,480 Constructors to construct your objects 7819 05:51:33,480 --> 05:51:35,940 and it was an alternative to defining 7820 05:51:35,940 --> 05:51:39,360 the values to the properties of the for 7821 05:51:39,360 --> 05:51:40,920 particular object 7822 05:51:40,920 --> 05:51:43,320 and we've also looked at how we can 7823 05:51:43,320 --> 05:51:46,378 Define the this Source I should say it's 7824 05:51:46,378 --> 05:51:48,360 also called the primary Constructor that 7825 05:51:48,360 --> 05:51:50,218 we have here the parenthesis and then 7826 05:51:50,218 --> 05:51:52,500 the parameters or the properties 7827 05:51:52,500 --> 05:51:55,558 and we've also talked about how we can 7828 05:51:55,558 --> 05:51:57,780 Define parameters or Constructor and 7829 05:51:57,780 --> 05:52:00,120 then how we can convert those parameters 7830 05:52:00,120 --> 05:52:03,840 directly into properties and I've also 7831 05:52:03,840 --> 05:52:06,540 talked a little bit about how and when 7832 05:52:06,540 --> 05:52:07,700 to use 7833 05:52:07,700 --> 05:52:11,280 parameters and properties but in this 7834 05:52:11,280 --> 05:52:12,420 video we'll continue our discussion 7835 05:52:12,420 --> 05:52:14,400 about parameters and properties and 7836 05:52:14,400 --> 05:52:17,340 specifically going to look at how we can 7837 05:52:17,340 --> 05:52:20,520 execute more than one line of code when 7838 05:52:20,520 --> 05:52:23,400 the object is created because 7839 05:52:23,400 --> 05:52:28,260 now when the object is created and we in 7840 05:52:28,260 --> 05:52:29,760 the last video we trimmed this space 7841 05:52:29,760 --> 05:52:31,580 that we have here 7842 05:52:31,580 --> 05:52:34,200 you can execute only this line of course 7843 05:52:34,200 --> 05:52:35,878 so if you want to add more line of 7844 05:52:35,878 --> 05:52:38,280 course here to do some 7845 05:52:38,280 --> 05:52:42,660 um to do a more complex validation we 7846 05:52:42,660 --> 05:52:44,400 can't because here we can put just one 7847 05:52:44,400 --> 05:52:45,718 line of code 7848 05:52:45,718 --> 05:52:48,718 and to add more than one line of code of 7849 05:52:48,718 --> 05:52:50,458 code they need to use what is called 7850 05:52:50,458 --> 05:52:53,700 initializer blocks and the initializer 7851 05:52:53,700 --> 05:52:56,580 blocks are as you'll see some blocks 7852 05:52:56,580 --> 05:53:00,718 where we can put our code and you can 7853 05:53:00,718 --> 05:53:02,218 put more than online so forth you can 7854 05:53:02,218 --> 05:53:04,080 put as many lines of code you want and 7855 05:53:04,080 --> 05:53:06,298 that code is going to be executed when 7856 05:53:06,298 --> 05:53:08,760 an object is created on an instance of 7857 05:53:08,760 --> 05:53:12,660 our car of our of your class is going to 7858 05:53:12,660 --> 05:53:13,798 be created 7859 05:53:13,798 --> 05:53:16,080 so um this is what we're gonna do in 7860 05:53:16,080 --> 05:53:18,718 this video also I should say that you 7861 05:53:18,718 --> 05:53:20,820 can declare the class in another file 7862 05:53:20,820 --> 05:53:23,100 not just in this file like we did here 7863 05:53:23,100 --> 05:53:26,218 so we can open now the project pane 7864 05:53:26,218 --> 05:53:29,180 you can go to SRC go domain 7865 05:53:29,180 --> 05:53:31,500 go to kotlin 7866 05:53:31,500 --> 05:53:36,378 and here on kotlin you can right click 7867 05:53:36,900 --> 05:53:39,120 select kotlin class file 7868 05:53:39,120 --> 05:53:41,700 and from here make sure to select file 7869 05:53:41,700 --> 05:53:43,558 and I'm going to call this file classes 7870 05:53:43,558 --> 05:53:45,298 because you're going to put our classes 7871 05:53:45,298 --> 05:53:47,340 respect we're going to put our card 7872 05:53:47,340 --> 05:53:50,340 class but you can put more classes there 7873 05:53:50,340 --> 05:53:52,200 and uh 7874 05:53:52,200 --> 05:53:54,180 we're just going to copy this class that 7875 05:53:54,180 --> 05:53:56,340 we defined here 7876 05:53:56,340 --> 05:53:57,900 Ctrl C 7877 05:53:57,900 --> 05:53:59,458 and paste it here 7878 05:53:59,458 --> 05:54:01,620 now I have an error because this is 7879 05:54:01,620 --> 05:54:05,700 declared two times here and here and we 7880 05:54:05,700 --> 05:54:07,440 just delete it from here 7881 05:54:07,440 --> 05:54:10,320 and now the arrow disappears 7882 05:54:10,320 --> 05:54:13,520 and if you run this code 7883 05:54:18,480 --> 05:54:21,480 we get the same output because our code 7884 05:54:21,480 --> 05:54:23,040 works 7885 05:54:23,040 --> 05:54:25,740 perfectly well now the only difference 7886 05:54:25,740 --> 05:54:28,020 is that now we will put we put our class 7887 05:54:28,020 --> 05:54:30,540 in a different file and my suggestion is 7888 05:54:30,540 --> 05:54:32,940 for you is to put the class if you want 7889 05:54:32,940 --> 05:54:35,100 in a different fly file 7890 05:54:35,100 --> 05:54:37,620 because in this way you have the code in 7891 05:54:37,620 --> 05:54:39,780 when I'm going to organize the way 7892 05:54:39,780 --> 05:54:42,840 because here you have your class here we 7893 05:54:42,840 --> 05:54:45,420 have the code the objects that we 7894 05:54:45,420 --> 05:54:46,860 created with that class 7895 05:54:46,860 --> 05:54:49,558 and so on 7896 05:54:49,558 --> 05:54:50,638 no 7897 05:54:50,638 --> 05:54:52,680 for this video I'm gonna use I'm gonna 7898 05:54:52,680 --> 05:54:55,500 create a different class and 7899 05:54:55,500 --> 05:54:57,378 to do to do that I'm gonna declare 7900 05:54:57,378 --> 05:54:59,638 another class but I'm going to declare 7901 05:54:59,638 --> 05:55:02,340 it down here and I'm going to delete the 7902 05:55:02,340 --> 05:55:04,138 code that we have here 7903 05:55:04,138 --> 05:55:05,700 because we're going to use a different 7904 05:55:05,700 --> 05:55:08,058 class 7905 05:55:08,280 --> 05:55:10,558 and the class is going to be called user 7906 05:55:10,558 --> 05:55:14,400 so it's gonna represent the 7907 05:55:14,400 --> 05:55:17,340 the use and end user object so we type 7908 05:55:17,340 --> 05:55:18,958 here class 7909 05:55:18,958 --> 05:55:22,320 user now we create the Constructor the 7910 05:55:22,320 --> 05:55:24,120 primary Constructor we put parenthesis 7911 05:55:24,120 --> 05:55:26,760 we Define the parameters or the 7912 05:55:26,760 --> 05:55:29,520 properties for uh now I'm going to 7913 05:55:29,520 --> 05:55:31,500 Define directly the properties here so 7914 05:55:31,500 --> 05:55:33,740 we type here 7915 05:55:33,740 --> 05:55:36,660 keyword to have the property declared 7916 05:55:36,660 --> 05:55:39,480 VAR name 7917 05:55:39,480 --> 05:55:41,878 string so it's going to be we're going 7918 05:55:41,878 --> 05:55:43,680 to be store stacks 7919 05:55:43,680 --> 05:55:46,400 VAR 7920 05:55:47,940 --> 05:55:51,557 last name also string 7921 05:55:52,500 --> 05:55:54,480 and uh 7922 05:55:54,480 --> 05:55:58,378 VAR age this is going to be an integer 7923 05:55:58,378 --> 05:56:00,958 so this with this class we're going to 7924 05:56:00,958 --> 05:56:03,058 create some user objects I'm going to 7925 05:56:03,058 --> 05:56:05,638 put curly braces 7926 05:56:05,638 --> 05:56:08,400 and for now we're not gonna put any code 7927 05:56:08,400 --> 05:56:10,558 here so let's create a user so let's 7928 05:56:10,558 --> 05:56:13,218 type here Val 7929 05:56:13,558 --> 05:56:15,780 or let's call it user you can give it 7930 05:56:15,780 --> 05:56:17,520 whatever you want 7931 05:56:17,520 --> 05:56:20,100 so let's create a new object now so we 7932 05:56:20,100 --> 05:56:21,480 put 7933 05:56:21,480 --> 05:56:23,400 you type your user and then for the 7934 05:56:23,400 --> 05:56:25,798 parenthesis because I'm going to put 7935 05:56:25,798 --> 05:56:27,120 we're going to pass the values to the 7936 05:56:27,120 --> 05:56:29,218 Constructor here Sony to pass a name I'm 7937 05:56:29,218 --> 05:56:31,680 gonna type Alex 7938 05:56:31,680 --> 05:56:33,660 last name 7939 05:56:33,660 --> 05:56:36,500 no binka 7940 05:56:36,540 --> 05:56:40,280 and for the age 23 7941 05:56:40,860 --> 05:56:43,980 so now we created a new instance a new 7942 05:56:43,980 --> 05:56:46,620 object using the using the cluster to 7943 05:56:46,620 --> 05:56:48,360 Define below 7944 05:56:48,360 --> 05:56:53,458 but what if I want to to pass to to the 7945 05:56:53,458 --> 05:56:55,138 to the name 7946 05:56:55,138 --> 05:56:57,920 to the name name property only the the 7947 05:56:57,920 --> 05:57:02,340 names which starts with the letter A 7948 05:57:02,340 --> 05:57:04,580 in that case we need to declare this 7949 05:57:04,580 --> 05:57:08,400 property as a parameter first we need to 7950 05:57:08,400 --> 05:57:09,840 declare here the parameter and then to 7951 05:57:09,840 --> 05:57:13,920 declare the property below and before 7952 05:57:13,920 --> 05:57:16,320 before we assign the value that is 7953 05:57:16,320 --> 05:57:18,660 passed to to the property we need to do 7954 05:57:18,660 --> 05:57:22,440 some validation and if that argument 7955 05:57:22,440 --> 05:57:24,298 that is passed to the 7956 05:57:24,298 --> 05:57:28,080 parameter is starting with the ale 7957 05:57:28,080 --> 05:57:30,000 letter then you're going to assign the 7958 05:57:30,000 --> 05:57:32,280 value to the property name otherwise we 7959 05:57:32,280 --> 05:57:34,980 don't do that so to do that we need to 7960 05:57:34,980 --> 05:57:37,740 use initializer blocks because we're 7961 05:57:37,740 --> 05:57:40,320 gonna type more than one line of code 7962 05:57:40,320 --> 05:57:42,718 and the initializer blocks are used in 7963 05:57:42,718 --> 05:57:44,638 conjunction with the primary Constructor 7964 05:57:44,638 --> 05:57:47,160 so when we create a new object like we 7965 05:57:47,160 --> 05:57:49,798 did here the initializer blocks or the 7966 05:57:49,798 --> 05:57:51,600 initializer block is going to be called 7967 05:57:51,600 --> 05:57:54,180 immediately when the object is created 7968 05:57:54,180 --> 05:57:57,718 and to use an initial initializer block 7969 05:57:57,718 --> 05:58:00,420 we just but first let's declare let's 7970 05:58:00,420 --> 05:58:03,298 change this to to a parameter so let's 7971 05:58:03,298 --> 05:58:06,620 delete the VAR keyword in front of 7972 05:58:06,620 --> 05:58:09,058 the variable so now we have only a 7973 05:58:09,058 --> 05:58:11,040 parameter here now let's declare the 7974 05:58:11,040 --> 05:58:13,440 property down here 7975 05:58:13,440 --> 05:58:15,360 and now here we need to Define an 7976 05:58:15,360 --> 05:58:17,520 explicit type declaration so we need to 7977 05:58:17,520 --> 05:58:20,520 put here the type string because as I 7978 05:58:20,520 --> 05:58:21,180 said 7979 05:58:21,180 --> 05:58:23,580 um we're not going to assign the value 7980 05:58:23,580 --> 05:58:26,820 to the property directly here in one 7981 05:58:26,820 --> 05:58:29,580 line of code we're going to assign the 7982 05:58:29,580 --> 05:58:31,920 value to the property inside the 7983 05:58:31,920 --> 05:58:33,958 initializer blocked after we do the 7984 05:58:33,958 --> 05:58:36,718 validation respectively after we check 7985 05:58:36,718 --> 05:58:40,260 to see if the name that is passed to the 7986 05:58:40,260 --> 05:58:42,120 objects that we're going to create with 7987 05:58:42,120 --> 05:58:43,138 the class 7988 05:58:43,138 --> 05:58:46,500 starts with the letter a and if the the 7989 05:58:46,500 --> 05:58:49,080 name if the argument doesn't start with 7990 05:58:49,080 --> 05:58:50,218 the letter A then we're going to assign 7991 05:58:50,218 --> 05:58:52,080 an empty string and we're gonna output 7992 05:58:52,080 --> 05:58:53,878 something to the console so this is why 7993 05:58:53,878 --> 05:58:55,860 we need to provide here the type because 7994 05:58:55,860 --> 05:58:57,120 I'm not gonna 7995 05:58:57,120 --> 05:58:59,280 assign the value directly here so it 7996 05:58:59,280 --> 05:59:02,040 cannot infer what kind of data is going 7997 05:59:02,040 --> 05:59:02,940 to store 7998 05:59:02,940 --> 05:59:04,558 then you press enter 7999 05:59:04,558 --> 05:59:06,600 we type init 8000 05:59:06,600 --> 05:59:08,760 and here we have a suggestion we have 8001 05:59:08,760 --> 05:59:10,620 the unique keyword and then we have the 8002 05:59:10,620 --> 05:59:12,900 curly braces press enter 8003 05:59:12,900 --> 05:59:15,058 and now here we're gonna add our 8004 05:59:15,058 --> 05:59:18,120 validation and here we type if 8005 05:59:18,120 --> 05:59:20,160 parenthesis 8006 05:59:20,160 --> 05:59:23,638 now type name so now we're referring the 8007 05:59:23,638 --> 05:59:26,940 name parameter defined above here so if 8008 05:59:26,940 --> 05:59:29,940 name or the value that is passed to the 8009 05:59:29,940 --> 05:59:31,878 name parameter 8010 05:59:31,878 --> 05:59:34,020 first we're gonna 8011 05:59:34,020 --> 05:59:34,740 um 8012 05:59:34,740 --> 05:59:37,500 lowercase dot name 8013 05:59:37,500 --> 05:59:40,260 and to do that we type here name dot 8014 05:59:40,260 --> 05:59:43,620 lowercase because I want to have both 8015 05:59:43,620 --> 05:59:45,600 cases so if 8016 05:59:45,600 --> 05:59:48,958 if the user passes let's say a name of 8017 05:59:48,958 --> 05:59:52,138 the lowercase letter then we're gonna 8018 05:59:52,138 --> 05:59:54,780 check to see if it starts with OTA but 8019 05:59:54,780 --> 05:59:57,900 if the user passes the the name of the 8020 05:59:57,900 --> 06:00:00,058 an uppercase later we also want to check 8021 06:00:00,058 --> 06:00:03,180 to see if if that letter starts with a 8022 06:00:03,180 --> 06:00:05,820 and to cover both cases we lowercase the 8023 06:00:05,820 --> 06:00:08,820 input we lowercase the name so all the 8024 06:00:08,820 --> 06:00:11,940 lowercase all the letters then we put 8025 06:00:11,940 --> 06:00:13,920 here another Dot 8026 06:00:13,920 --> 06:00:17,218 starts with an input here 8027 06:00:17,218 --> 06:00:18,958 a 8028 06:00:18,958 --> 06:00:21,058 and if that is true 8029 06:00:21,058 --> 06:00:24,000 then we're gonna assign whatever value 8030 06:00:24,000 --> 06:00:25,980 was passed to the name parameter to the 8031 06:00:25,980 --> 06:00:27,298 name property 8032 06:00:27,298 --> 06:00:31,020 and we cannot type here name equals name 8033 06:00:31,020 --> 06:00:33,480 because now it's referring the 8034 06:00:33,480 --> 06:00:36,420 parameter here and to refer the property 8035 06:00:36,420 --> 06:00:38,160 that we Define here we need to use what 8036 06:00:38,160 --> 06:00:40,378 is called the disk keyword so the disk 8037 06:00:40,378 --> 06:00:42,600 keyword is used to refer them 8038 06:00:42,600 --> 06:00:45,780 properties of the class so it are here 8039 06:00:45,780 --> 06:00:47,458 this 8040 06:00:47,458 --> 06:00:50,700 so we're saying this name so now we have 8041 06:00:50,700 --> 06:00:52,320 this name and we have a different we 8042 06:00:52,320 --> 06:00:54,540 have this V variable here 8043 06:00:54,540 --> 06:00:56,760 then put equals 8044 06:00:56,760 --> 06:00:59,760 name so now we assign the value that is 8045 06:00:59,760 --> 06:01:02,520 passed to parameter only if that 8046 06:01:02,520 --> 06:01:05,400 valuable only if that value pass to the 8047 06:01:05,400 --> 06:01:07,378 parameter starts with the letter A then 8048 06:01:07,378 --> 06:01:09,840 we're gonna assign the name to the 8049 06:01:09,840 --> 06:01:11,218 property name 8050 06:01:11,218 --> 06:01:13,740 else if that is not true 8051 06:01:13,740 --> 06:01:15,718 put curly braces 8052 06:01:15,718 --> 06:01:17,760 and now we need to cover the else part 8053 06:01:17,760 --> 06:01:20,040 because if this is not true we also need 8054 06:01:20,040 --> 06:01:22,500 to pass a value to to the name proper 8055 06:01:22,500 --> 06:01:25,020 the name property cannot stay without a 8056 06:01:25,020 --> 06:01:26,878 value so we need to pass those over here 8057 06:01:26,878 --> 06:01:28,798 else we're going to put this 8058 06:01:28,798 --> 06:01:30,718 dot name 8059 06:01:30,718 --> 06:01:33,660 and I'm going to put 8060 06:01:33,660 --> 06:01:37,138 user so I'm going to put the user 8061 06:01:37,138 --> 06:01:41,100 text if the if the the name doesn't 8062 06:01:41,100 --> 06:01:43,320 start with the with the letter A 8063 06:01:43,320 --> 06:01:45,660 and we can also output some text.com so 8064 06:01:45,660 --> 06:01:48,620 we put println here 8065 06:01:48,780 --> 06:01:51,180 and we'll type the name 8066 06:01:51,180 --> 06:01:53,840 doesn't 8067 06:01:53,940 --> 06:01:55,500 start 8068 06:01:55,500 --> 06:01:57,540 with 8069 06:01:57,540 --> 06:02:00,200 the letter 8070 06:02:01,320 --> 06:02:04,320 a or 8071 06:02:04,320 --> 06:02:07,340 capital a 8072 06:02:07,680 --> 06:02:10,500 so now if you create a new object here 8073 06:02:10,500 --> 06:02:15,600 so we type here Val let's say for friend 8074 06:02:15,600 --> 06:02:17,580 we create a new object so we type user 8075 06:02:17,580 --> 06:02:19,680 now we need to pass the values to the 8076 06:02:19,680 --> 06:02:22,558 Constructor so you put here a let's say 8077 06:02:22,558 --> 06:02:24,360 uh 8078 06:02:24,360 --> 06:02:26,900 John 8079 06:02:29,100 --> 06:02:30,958 let's put 8080 06:02:30,958 --> 06:02:32,458 Smith 8081 06:02:32,458 --> 06:02:34,080 let's put 8082 06:02:34,080 --> 06:02:37,740 30. now because the the value that you 8083 06:02:37,740 --> 06:02:39,840 pass here to the parameter name doesn't 8084 06:02:39,840 --> 06:02:43,320 start with a letter a and start the 8085 06:02:43,320 --> 06:02:45,120 letter j 8086 06:02:45,120 --> 06:02:47,580 this value validation inside the 8087 06:02:47,580 --> 06:02:50,100 initializer block is going to be false 8088 06:02:50,100 --> 06:02:52,020 and then it's gonna it's gonna execute 8089 06:02:52,020 --> 06:02:53,458 the else part and it's going to assign 8090 06:02:53,458 --> 06:02:57,420 to the property name the user text and 8091 06:02:57,420 --> 06:02:58,500 it's going to Output the name doesn't 8092 06:02:58,500 --> 06:03:01,020 start with the letter A or a but if that 8093 06:03:01,020 --> 06:03:03,840 is not uh if but if it's true if it 8094 06:03:03,840 --> 06:03:05,340 starts with the letter A then this line 8095 06:03:05,340 --> 06:03:06,840 of code is going to basically the base 8096 06:03:06,840 --> 06:03:10,080 part n is going to be a sign it's going 8097 06:03:10,080 --> 06:03:12,718 to assign the value to the property 8098 06:03:12,718 --> 06:03:16,218 so now if you run the squad 8099 06:03:21,420 --> 06:03:22,980 we get the name doesn't start with the 8100 06:03:22,980 --> 06:03:25,320 letter A or a and it's correct our name 8101 06:03:25,320 --> 06:03:26,940 starts with the letter j 8102 06:03:26,940 --> 06:03:31,039 so if I put here let's say 8103 06:03:31,500 --> 06:03:34,160 Andhra 8104 06:03:35,280 --> 06:03:38,420 let's change this to 8105 06:03:40,920 --> 06:03:43,138 oh let's see here also 8106 06:03:43,138 --> 06:03:45,798 Smith 8107 06:03:46,680 --> 06:03:50,000 now if I run this 8108 06:03:50,100 --> 06:03:53,638 now we have no output because the this 8109 06:03:53,638 --> 06:03:56,700 the initializer block is is called 8110 06:03:56,700 --> 06:03:59,820 and the If part is evalu is to true and 8111 06:03:59,820 --> 06:04:02,940 it's assigning the the name 8112 06:04:02,940 --> 06:04:06,058 Andra to our name property so let's add 8113 06:04:06,058 --> 06:04:08,040 the print and then down here to see that 8114 06:04:08,040 --> 06:04:10,020 that is correct so print 8115 06:04:10,020 --> 06:04:12,120 let's print the value of 8116 06:04:12,120 --> 06:04:14,700 of the property name for our friend 8117 06:04:14,700 --> 06:04:18,020 object and we put here 8118 06:04:18,378 --> 06:04:21,180 and here we type 8119 06:04:21,180 --> 06:04:24,138 dollar sign 8120 06:04:25,020 --> 06:04:30,138 you put curly braces and you type friend 8121 06:04:33,360 --> 06:04:35,218 Dot 8122 06:04:35,218 --> 06:04:37,200 name 8123 06:04:37,200 --> 06:04:41,298 so now if you run this let's close 8124 06:04:45,240 --> 06:04:48,240 get name under let's put comma here 8125 06:04:48,240 --> 06:04:49,920 let's put 8126 06:04:49,920 --> 06:04:52,820 colon here 8127 06:04:53,878 --> 06:04:57,120 so our initializer blocked is called the 8128 06:04:57,120 --> 06:04:59,520 code inside the initializer block this 8129 06:04:59,520 --> 06:05:02,940 execute it and then this if check 8130 06:05:02,940 --> 06:05:04,680 average to true and then and then it 8131 06:05:04,680 --> 06:05:07,020 assigns the name Andra to our property 8132 06:05:07,020 --> 06:05:11,298 name but if we I put here John 8133 06:05:17,400 --> 06:05:19,680 let's type it like this let's say to see 8134 06:05:19,680 --> 06:05:21,240 that it's working because we lowercase 8135 06:05:21,240 --> 06:05:23,840 the letters 8136 06:05:28,440 --> 06:05:30,000 okay the name doesn't start with the 8137 06:05:30,000 --> 06:05:32,878 letter A or a name user so the 8138 06:05:32,878 --> 06:05:34,378 initializer block is executed 8139 06:05:34,378 --> 06:05:36,298 immediately when the object is created 8140 06:05:36,298 --> 06:05:38,280 and 8141 06:05:38,280 --> 06:05:42,058 the code incidential log is called it 8142 06:05:42,058 --> 06:05:43,680 checks to see if it starts at the lower 8143 06:05:43,680 --> 06:05:46,920 it starts with the with the letter A 8144 06:05:46,920 --> 06:05:49,138 and if not then it is assigned to the 8145 06:05:49,138 --> 06:05:51,058 property name user so we have this we 8146 06:05:51,058 --> 06:05:52,260 have user here 8147 06:05:52,260 --> 06:05:54,420 and then it outputs uh 8148 06:05:54,420 --> 06:05:56,458 this 8149 06:05:56,458 --> 06:05:58,558 text to the console so this is how we 8150 06:05:58,558 --> 06:06:00,240 can use the initializer blocks in 8151 06:06:00,240 --> 06:06:02,000 conjunction with the primary Constructor 8152 06:06:02,000 --> 06:06:05,100 and you can also put more initializer 8153 06:06:05,100 --> 06:06:07,740 blocks and the order in which you put 8154 06:06:07,740 --> 06:06:10,558 the initializer blocks matters in the 8155 06:06:10,558 --> 06:06:13,260 sense that the word in which you define 8156 06:06:13,260 --> 06:06:15,360 the initializer blocked is is going to 8157 06:06:15,360 --> 06:06:17,400 be the word in which they are executed 8158 06:06:17,400 --> 06:06:19,680 so the word in which you define them 8159 06:06:19,680 --> 06:06:22,920 it's important but most of the time I 8160 06:06:22,920 --> 06:06:24,780 think you'll you use only one 8161 06:06:24,780 --> 06:06:28,080 initializer block to put your code so 8162 06:06:28,080 --> 06:06:30,120 this is our discussion about initializer 8163 06:06:30,120 --> 06:06:32,100 blocks and in the next video we're going 8164 06:06:32,100 --> 06:06:33,480 to look at um 8165 06:06:33,480 --> 06:06:34,740 because 8166 06:06:34,740 --> 06:06:36,920 we talked about the primary Constructors 8167 06:06:36,920 --> 06:06:40,440 there is a another type of Constructor 8168 06:06:40,440 --> 06:06:42,058 which is called the secondary 8169 06:06:42,058 --> 06:06:44,040 Constructor of this secondary 8170 06:06:44,040 --> 06:06:45,780 Constructors 8171 06:06:45,780 --> 06:06:48,540 and we can also look at the default 8172 06:06:48,540 --> 06:06:51,080 parameters because you can put you can 8173 06:06:51,080 --> 06:06:55,020 we can Define default values to the 8174 06:06:55,020 --> 06:06:57,360 properties also the parameters of the 8175 06:06:57,360 --> 06:06:59,400 Constructor so we're gonna do that we're 8176 06:06:59,400 --> 06:07:02,040 going to do that in the next video 8177 06:07:02,040 --> 06:07:04,378 I'm gonna end this video a little now 8178 06:07:04,378 --> 06:07:06,240 so now it's time to start a discussion 8179 06:07:06,240 --> 06:07:08,638 about secondary Constructors because 8180 06:07:08,638 --> 06:07:11,638 besides our primary Constructor that we 8181 06:07:11,638 --> 06:07:13,920 Define and that we talked about in 8182 06:07:13,920 --> 06:07:15,840 Cotton you can Define multiple 8183 06:07:15,840 --> 06:07:18,000 Constructors and those are called the 8184 06:07:18,000 --> 06:07:19,798 secondary Constructors 8185 06:07:19,798 --> 06:07:21,540 now you may be wondering why you will 8186 06:07:21,540 --> 06:07:23,400 need secondary Constructors 8187 06:07:23,400 --> 06:07:26,878 and to answer that question we need to 8188 06:07:26,878 --> 06:07:30,240 consider the user class and those user 8189 06:07:30,240 --> 06:07:32,340 objects that you can create so let's say 8190 06:07:32,340 --> 06:07:33,958 that an user 8191 06:07:33,958 --> 06:07:37,260 only provides its first name and for the 8192 06:07:37,260 --> 06:07:39,718 property is last name and the age 8193 06:07:39,718 --> 06:07:42,120 doesn't provide any value in that case 8194 06:07:42,120 --> 06:07:44,760 you will need the you need a way to to 8195 06:07:44,760 --> 06:07:46,940 Define some default values for those 8196 06:07:46,940 --> 06:07:49,620 properties in case the user only 8197 06:07:49,620 --> 06:07:51,298 provides the first name 8198 06:07:51,298 --> 06:07:53,458 and uh 8199 06:07:53,458 --> 06:07:57,540 if let's say the next user does provide 8200 06:07:57,540 --> 06:07:59,638 the the first name and the last name and 8201 06:07:59,638 --> 06:08:02,520 the age in that case you're gonna use uh 8202 06:08:02,520 --> 06:08:05,100 the the primary Constructor but if it 8203 06:08:05,100 --> 06:08:07,558 doesn't provide those uh those values 8204 06:08:07,558 --> 06:08:08,940 then you're gonna use the secondary 8205 06:08:08,940 --> 06:08:11,520 Constructor but to make this more clear 8206 06:08:11,520 --> 06:08:14,400 lists uh let's add the code and we talk 8207 06:08:14,400 --> 06:08:17,340 we talk about was while I am writing the 8208 06:08:17,340 --> 06:08:19,200 code so let's delete the initializer 8209 06:08:19,200 --> 06:08:21,360 blocks that we have here because you 8210 06:08:21,360 --> 06:08:23,580 don't need this code 8211 06:08:23,580 --> 06:08:25,740 and let's also put the property now 8212 06:08:25,740 --> 06:08:27,480 let's define the property in the primary 8213 06:08:27,480 --> 06:08:30,240 Constructor so let's just put here a 8214 06:08:30,240 --> 06:08:32,760 VAR the work here in front of the 8215 06:08:32,760 --> 06:08:34,020 variable 8216 06:08:34,020 --> 06:08:38,120 let's also delete this printalian 8217 06:08:39,360 --> 06:08:41,458 and um 8218 06:08:41,458 --> 06:08:44,899 let's delete the values here 8219 06:08:47,040 --> 06:08:49,740 for the last name and the age so now as 8220 06:08:49,740 --> 06:08:51,120 you can see we have an error because 8221 06:08:51,120 --> 06:08:53,940 it's expecting here the 8222 06:08:53,940 --> 06:08:55,798 um it's expecting an argument a value 8223 06:08:55,798 --> 06:08:59,340 for the last name and for the age and to 8224 06:08:59,340 --> 06:09:01,620 solve that problem we just go down here 8225 06:09:01,620 --> 06:09:05,458 and we type Constructor so this is the 8226 06:09:05,458 --> 06:09:07,740 keyword for declaring the secondary 8227 06:09:07,740 --> 06:09:10,280 Constructor 8228 06:09:10,860 --> 06:09:12,958 and we press enter to fill in that for 8229 06:09:12,958 --> 06:09:15,958 us and we put parentheses and now here 8230 06:09:15,958 --> 06:09:17,878 in the parenthesis we Define the 8231 06:09:17,878 --> 06:09:21,298 parameters and I am specifically saying 8232 06:09:21,298 --> 06:09:23,940 parameters because as you will see the 8233 06:09:23,940 --> 06:09:26,820 secondary Constructors cannot declare 8234 06:09:26,820 --> 06:09:28,798 properties they can only declare 8235 06:09:28,798 --> 06:09:30,900 parameters because 8236 06:09:30,900 --> 06:09:33,540 anyway Let's uh so let's define here the 8237 06:09:33,540 --> 06:09:37,860 parameter so I'm gonna type here name 8238 06:09:37,860 --> 06:09:40,200 let's put here the type of this variable 8239 06:09:40,200 --> 06:09:42,298 is going to be a string 8240 06:09:42,298 --> 06:09:45,900 now if you hover over here 8241 06:09:45,900 --> 06:09:47,760 it says primary Constructor called 8242 06:09:47,760 --> 06:09:52,680 aspect insert this call so what all the 8243 06:09:52,680 --> 06:09:55,740 secondary Constructors need to do is 8244 06:09:55,740 --> 06:09:57,780 they need to call the primary 8245 06:09:57,780 --> 06:10:00,600 Constructor because ultimately the 8246 06:10:00,600 --> 06:10:02,520 primary Constructor is the one which 8247 06:10:02,520 --> 06:10:04,860 declares the property the properties for 8248 06:10:04,860 --> 06:10:09,840 the class and stores the values for uh 8249 06:10:09,840 --> 06:10:13,260 of that Pro of that property and to do 8250 06:10:13,260 --> 06:10:15,120 that to put here 8251 06:10:15,120 --> 06:10:17,520 colon 8252 06:10:17,520 --> 06:10:20,520 we type this 8253 06:10:20,520 --> 06:10:23,878 so we put colon this type that this 8254 06:10:23,878 --> 06:10:26,280 keyword then you put parenthesis 8255 06:10:26,280 --> 06:10:27,420 and 8256 06:10:27,420 --> 06:10:31,798 to the disc call we pass the name so 8257 06:10:31,798 --> 06:10:34,878 this name that we defined here 8258 06:10:34,878 --> 06:10:37,860 and for the next two values we're going 8259 06:10:37,860 --> 06:10:39,420 to pass some default values so here I'm 8260 06:10:39,420 --> 06:10:42,360 going to put user because let's say that 8261 06:10:42,360 --> 06:10:45,900 because as I said in the beginning this 8262 06:10:45,900 --> 06:10:48,680 user is gonna not it's not gonna provide 8263 06:10:48,680 --> 06:10:50,940 the last name so let's put your last 8264 06:10:50,940 --> 06:10:53,760 name not user last 8265 06:10:53,760 --> 06:10:55,200 name 8266 06:10:55,200 --> 06:10:59,340 and for the age let's put 8267 06:10:59,340 --> 06:11:01,620 let's put let's say zero 8268 06:11:01,620 --> 06:11:04,080 all right now 8269 06:11:04,080 --> 06:11:06,900 what this is going to do is now if you 8270 06:11:06,900 --> 06:11:09,180 look up here don't have an error so 8271 06:11:09,180 --> 06:11:11,820 let's uh so now we don't have that error 8272 06:11:11,820 --> 06:11:14,400 because now it's using the secondary 8273 06:11:14,400 --> 06:11:17,218 Constructor is passing the value here 8274 06:11:17,218 --> 06:11:20,520 Alex to the secondary Constructor that 8275 06:11:20,520 --> 06:11:22,680 we Define here and here we Define only 8276 06:11:22,680 --> 06:11:24,900 the parameter and the value that is 8277 06:11:24,900 --> 06:11:27,718 passed as parameter then 8278 06:11:27,718 --> 06:11:30,180 is passed to this to the primary 8279 06:11:30,180 --> 06:11:33,420 Constructor because by putting colon and 8280 06:11:33,420 --> 06:11:35,700 these and parenthesis we're calling now 8281 06:11:35,700 --> 06:11:37,500 the primary Constructor 8282 06:11:37,500 --> 06:11:40,020 and is the primary Constructor the one 8283 06:11:40,020 --> 06:11:41,040 which 8284 06:11:41,040 --> 06:11:43,920 ultimately declares the property and 8285 06:11:43,920 --> 06:11:47,100 stores whatever value we send to him in 8286 06:11:47,100 --> 06:11:48,900 this case we send 8287 06:11:48,900 --> 06:11:52,798 the value Alex and for the last name we 8288 06:11:52,798 --> 06:11:56,040 uh we just send the last the text last 8289 06:11:56,040 --> 06:11:58,740 name and for the age you put 0 because 8290 06:11:58,740 --> 06:12:02,100 this is our way to saying that this user 8291 06:12:02,100 --> 06:12:05,580 doesn't want to provide its age so this 8292 06:12:05,580 --> 06:12:08,400 is why you need to call the the primary 8293 06:12:08,400 --> 06:12:10,378 Constructor from the secondary 8294 06:12:10,378 --> 06:12:14,040 Constructor because again the secondary 8295 06:12:14,040 --> 06:12:17,400 the primary Constructor is the one which 8296 06:12:17,400 --> 06:12:19,320 ultimately declares the properties of 8297 06:12:19,320 --> 06:12:22,740 the classes the one which stores the the 8298 06:12:22,740 --> 06:12:25,680 the properties of the class the 8299 06:12:25,680 --> 06:12:28,200 secondary Constructor only 8300 06:12:28,200 --> 06:12:30,958 only takes parameters and then calls the 8301 06:12:30,958 --> 06:12:33,420 primary Constructor so in fact if you 8302 06:12:33,420 --> 06:12:35,400 put here the VAR keyword 8303 06:12:35,400 --> 06:12:37,798 you have an arrow which says 8304 06:12:37,798 --> 06:12:40,080 Varon secondary Constructor parameter is 8305 06:12:40,080 --> 06:12:41,520 not allowed that is because the 8306 06:12:41,520 --> 06:12:43,320 secondary Constructor cannot call it 8307 06:12:43,320 --> 06:12:45,298 cannot declare properties 8308 06:12:45,298 --> 06:12:47,760 only the primary Constructor 8309 06:12:47,760 --> 06:12:50,340 and because as I said you can declare 8310 06:12:50,340 --> 06:12:52,500 multiple second Constructors we can type 8311 06:12:52,500 --> 06:12:53,400 here 8312 06:12:53,400 --> 06:12:55,920 Constructor so we can declare another 8313 06:12:55,920 --> 06:12:58,020 secondary Constructor and here you can 8314 06:12:58,020 --> 06:12:58,860 put 8315 06:12:58,860 --> 06:13:01,580 name 8316 06:13:02,100 --> 06:13:05,218 here we put string 8317 06:13:05,218 --> 06:13:07,980 then we type last name also string so 8318 06:13:07,980 --> 06:13:09,780 this secondary Constructor is only going 8319 06:13:09,780 --> 06:13:10,680 to take 8320 06:13:10,680 --> 06:13:12,240 the 8321 06:13:12,240 --> 06:13:16,378 name and the last name as arguments the 8322 06:13:16,378 --> 06:13:20,280 age is not gonna this uh is gonna not 8323 06:13:20,280 --> 06:13:22,980 gonna take any value for the age now we 8324 06:13:22,980 --> 06:13:25,680 need to put colon 8325 06:13:25,680 --> 06:13:28,500 we type this parenthesis because now 8326 06:13:28,500 --> 06:13:30,180 we're calling the 8327 06:13:30,180 --> 06:13:33,840 primary Constructor and here we we pass 8328 06:13:33,840 --> 06:13:35,520 the name that is passed to this 8329 06:13:35,520 --> 06:13:37,558 secondary Constructor this that we 8330 06:13:37,558 --> 06:13:39,600 Define here 8331 06:13:39,600 --> 06:13:42,557 the last name 8332 06:13:43,920 --> 06:13:45,840 and for the age we're going to put 0 8333 06:13:45,840 --> 06:13:48,480 because this uh 8334 06:13:48,480 --> 06:13:50,780 this second secondary Constructor 8335 06:13:50,780 --> 06:13:52,520 doesn't 8336 06:13:52,520 --> 06:13:56,100 take any value any arguments for its uh 8337 06:13:56,100 --> 06:13:58,200 for its age 8338 06:13:58,200 --> 06:14:01,378 but because as I said ultimately the 8339 06:14:01,378 --> 06:14:03,500 primary Constructor is the one which 8340 06:14:03,500 --> 06:14:07,500 defines the properties we cannot we 8341 06:14:07,500 --> 06:14:08,878 cannot call the prime Constructor and 8342 06:14:08,878 --> 06:14:10,500 not pass any value so we pass this 8343 06:14:10,500 --> 06:14:12,120 default value here 8344 06:14:12,120 --> 06:14:14,100 and uh 8345 06:14:14,100 --> 06:14:16,680 so now if you go up here and let's say 8346 06:14:16,680 --> 06:14:19,638 that I delete the age 8347 06:14:20,218 --> 06:14:23,940 now our code works fine because now what 8348 06:14:23,940 --> 06:14:26,638 is happening here is this the the value 8349 06:14:26,638 --> 06:14:28,378 for the 8350 06:14:28,378 --> 06:14:31,740 for the name and the last name is passed 8351 06:14:31,740 --> 06:14:34,020 to this secondary Constructor this one 8352 06:14:34,020 --> 06:14:35,638 here 8353 06:14:35,638 --> 06:14:39,120 and the secondary Constructor then calls 8354 06:14:39,120 --> 06:14:41,458 this primary Constructor and it's 8355 06:14:41,458 --> 06:14:43,260 passing the two values to the Primal 8356 06:14:43,260 --> 06:14:45,718 structure name respectively this name 8357 06:14:45,718 --> 06:14:50,458 John and last name Smith and for the age 8358 06:14:50,458 --> 06:14:54,540 it passes the default volume zero so 8359 06:14:54,540 --> 06:14:56,520 this is how you can this is why we can 8360 06:14:56,520 --> 06:14:59,218 pass here uh 8361 06:14:59,218 --> 06:15:01,798 this this is why you can avoid here to 8362 06:15:01,798 --> 06:15:04,620 pass let's say uh you can avoid to pass 8363 06:15:04,620 --> 06:15:07,200 the last name and the age for the first 8364 06:15:07,200 --> 06:15:09,780 because it is using the first secondary 8365 06:15:09,780 --> 06:15:11,218 Constructor 8366 06:15:11,218 --> 06:15:13,320 and this object that we created here 8367 06:15:13,320 --> 06:15:16,500 it's using this secondary Constructor 8368 06:15:16,500 --> 06:15:18,840 because here we're passing only the 8369 06:15:18,840 --> 06:15:21,840 first name for the value for the first 8370 06:15:21,840 --> 06:15:23,878 name property and the value for the last 8371 06:15:23,878 --> 06:15:26,218 name property so it's passing them to 8372 06:15:26,218 --> 06:15:28,920 this secondary Constructor which then in 8373 06:15:28,920 --> 06:15:32,218 turn calls the primary Constructor it 8374 06:15:32,218 --> 06:15:33,900 passes them to the primary Constructor 8375 06:15:33,900 --> 06:15:35,780 so it passes the 8376 06:15:35,780 --> 06:15:39,780 the values for the two properties and 8377 06:15:39,780 --> 06:15:42,298 the last one is defaulted because 8378 06:15:42,298 --> 06:15:44,100 okay because you don't provide the value 8379 06:15:44,100 --> 06:15:45,240 for the age 8380 06:15:45,240 --> 06:15:47,280 with Pro with secondary Constructors you 8381 06:15:47,280 --> 06:15:49,558 can also put you can at the end of the 8382 06:15:49,558 --> 06:15:51,298 call of the primary consultor you can 8383 06:15:51,298 --> 06:15:54,180 put curly braces 8384 06:15:54,180 --> 06:15:56,280 to have some code executed when you 8385 06:15:56,280 --> 06:15:58,920 create an object with that specific uh 8386 06:15:58,920 --> 06:16:01,500 secondary Constructor so you can put 8387 06:16:01,500 --> 06:16:03,660 here curly braces and inside the color 8388 06:16:03,660 --> 06:16:06,000 blades you can have some code which is 8389 06:16:06,000 --> 06:16:07,378 going to be executed you want to create 8390 06:16:07,378 --> 06:16:10,020 an object with this let's say secondary 8391 06:16:10,020 --> 06:16:12,020 Constructor 8392 06:16:12,020 --> 06:16:13,920 an object with this secondary 8393 06:16:13,920 --> 06:16:14,940 Constructor 8394 06:16:14,940 --> 06:16:17,100 so it's up to you what the code you put 8395 06:16:17,100 --> 06:16:19,980 there but to illustrate this let's put a 8396 06:16:19,980 --> 06:16:24,180 println statement here in the 8397 06:16:24,180 --> 06:16:25,920 in this secondary Constructor so let's 8398 06:16:25,920 --> 06:16:27,600 type here println and here I'm going to 8399 06:16:27,600 --> 06:16:28,378 type 8400 06:16:28,378 --> 06:16:31,378 second and I'm going to type second 8401 06:16:31,378 --> 06:16:34,138 because uh 8402 06:16:34,138 --> 06:16:36,780 this is the first Constructor this is 8403 06:16:36,780 --> 06:16:38,458 the primary Constructor and this is the 8404 06:16:38,458 --> 06:16:41,160 one which ultimately does all the work 8405 06:16:41,160 --> 06:16:43,200 is the one which stores the value for uh 8406 06:16:43,200 --> 06:16:45,240 the properties 8407 06:16:45,240 --> 06:16:48,058 and this is the third 8408 06:16:48,058 --> 06:16:50,878 Constructor so let's copy this 8409 06:16:50,878 --> 06:16:54,138 let's put here to Third 8410 06:16:55,500 --> 06:16:58,378 just for illustration 8411 06:16:58,378 --> 06:17:01,500 Also let's output in the console the 8412 06:17:01,500 --> 06:17:06,058 value of the the property values for 8413 06:17:06,058 --> 06:17:08,520 these objects so let's type here 8414 06:17:08,520 --> 06:17:10,860 printerland 8415 06:17:10,860 --> 06:17:14,280 and just type here name 8416 06:17:14,280 --> 06:17:19,160 equals let's put dollar sign 8417 06:17:19,860 --> 06:17:22,378 let's put curly braces 8418 06:17:22,378 --> 06:17:25,920 let's use our user 8419 06:17:25,920 --> 06:17:29,100 variable now let's call the properties 8420 06:17:29,100 --> 06:17:32,638 on this public so let's type name 8421 06:17:32,638 --> 06:17:34,920 now let's press Ctrl d 8422 06:17:34,920 --> 06:17:39,679 two times and let's put here now 8423 06:17:40,080 --> 06:17:42,540 last name 8424 06:17:42,540 --> 06:17:45,540 and the age 8425 06:17:45,540 --> 06:17:50,160 and let's also other space between 8426 06:17:50,160 --> 06:17:52,798 the information about the two objects to 8427 06:17:52,798 --> 06:17:54,540 make things more clear so let's put the 8428 06:17:54,540 --> 06:17:56,458 printer and heroes and put a backslash 8429 06:17:56,458 --> 06:17:58,877 in here 8430 06:17:59,940 --> 06:18:02,218 and let's copy the code that we have 8431 06:18:02,218 --> 06:18:05,400 here let's paste it below 8432 06:18:05,400 --> 06:18:08,218 let's change this 8433 06:18:08,218 --> 06:18:11,520 object to our friend object that we 8434 06:18:11,520 --> 06:18:14,280 created just like your friend 8435 06:18:14,280 --> 06:18:16,440 and I'm gonna speed this now a little 8436 06:18:16,440 --> 06:18:18,180 bit because 8437 06:18:18,180 --> 06:18:20,958 it will take some 8438 06:18:22,080 --> 06:18:24,718 now let's output this information in the 8439 06:18:24,718 --> 06:18:27,080 console 8440 06:18:29,340 --> 06:18:32,638 so now what we have is we have 8441 06:18:32,638 --> 06:18:34,080 we have 8442 06:18:34,080 --> 06:18:37,440 second and then third because this first 8443 06:18:37,440 --> 06:18:40,940 object is using the 8444 06:18:41,218 --> 06:18:43,320 the second Constructor 8445 06:18:43,320 --> 06:18:45,718 so this is why we have here a 8446 06:18:45,718 --> 06:18:47,520 second 8447 06:18:47,520 --> 06:18:49,798 and then the next line of code is called 8448 06:18:49,798 --> 06:18:52,740 and we have third because this one this 8449 06:18:52,740 --> 06:18:54,600 object that you have here the front 8450 06:18:54,600 --> 06:18:58,320 object it's using the third Constructor 8451 06:18:58,320 --> 06:19:00,360 this secondary Constructor so this I 8452 06:19:00,360 --> 06:19:02,280 have here third 8453 06:19:02,280 --> 06:19:04,440 then we have the values for the 8454 06:19:04,440 --> 06:19:06,120 properties for the I didn't have the 8455 06:19:06,120 --> 06:19:08,160 values for the properties of the user 8456 06:19:08,160 --> 06:19:10,020 object so you have 8457 06:19:10,020 --> 06:19:12,000 here the output 8458 06:19:12,000 --> 06:19:13,920 down here 8459 06:19:13,920 --> 06:19:15,840 and 8460 06:19:15,840 --> 06:19:18,660 then we scroll down and have name John 8461 06:19:18,660 --> 06:19:20,458 last name Smith age 8462 06:19:20,458 --> 06:19:23,520 zero because we have a 8463 06:19:23,520 --> 06:19:25,260 the property values for the second 8464 06:19:25,260 --> 06:19:27,660 object for this object 8465 06:19:27,660 --> 06:19:30,540 and this is okay this works completely 8466 06:19:30,540 --> 06:19:35,160 fine the thing to have in mind is that 8467 06:19:35,160 --> 06:19:36,480 now we're using the secondary 8468 06:19:36,480 --> 06:19:38,100 Constructors and the secondary 8469 06:19:38,100 --> 06:19:40,500 Constructor then calls the primary 8470 06:19:40,500 --> 06:19:42,120 Constructor and the primary Constructor 8471 06:19:42,120 --> 06:19:44,458 is the one which ultimately stores the 8472 06:19:44,458 --> 06:19:46,798 value source which ultimately declares 8473 06:19:46,798 --> 06:19:49,740 the properties and stored the values 8474 06:19:49,740 --> 06:19:53,100 but one thing to notice in the output is 8475 06:19:53,100 --> 06:19:56,760 that the first object has the name 8476 06:19:56,760 --> 06:20:00,000 Alex and the last name last name and age 8477 06:20:00,000 --> 06:20:03,360 zero that is because the first object is 8478 06:20:03,360 --> 06:20:04,558 only 8479 06:20:04,558 --> 06:20:07,378 taking a value for the first name 8480 06:20:07,378 --> 06:20:09,660 property or for the name property 8481 06:20:09,660 --> 06:20:14,540 and for the last name and the age 8482 06:20:14,580 --> 06:20:16,440 because it's using this secondary 8483 06:20:16,440 --> 06:20:19,378 Constructor is only passing in the first 8484 06:20:19,378 --> 06:20:21,840 name which is Alex that we have here 8485 06:20:21,840 --> 06:20:23,878 and then it's using the default values 8486 06:20:23,878 --> 06:20:26,580 last name and a zero it's passing those 8487 06:20:26,580 --> 06:20:28,440 default default values to the primary 8488 06:20:28,440 --> 06:20:30,120 Constructor so it's calling the primary 8489 06:20:30,120 --> 06:20:32,638 Constructor is passing the value Alex 8490 06:20:32,638 --> 06:20:35,160 and then the default value last name and 8491 06:20:35,160 --> 06:20:37,260 zero this is why I have last name and 0 8492 06:20:37,260 --> 06:20:40,680 here and for the next one we have 8493 06:20:40,680 --> 06:20:44,400 name John and last name Smith this is 8494 06:20:44,400 --> 06:20:47,580 because now this is using um the next 8495 06:20:47,580 --> 06:20:51,680 secondary Constructor and that takes as 8496 06:20:51,680 --> 06:20:54,120 arguments the name and the last name and 8497 06:20:54,120 --> 06:20:55,138 then it's calling the primary 8498 06:20:55,138 --> 06:20:57,240 Constructor it passes the 8499 06:20:57,240 --> 06:20:59,218 first name and the last name and the 8500 06:20:59,218 --> 06:21:01,260 aegi is defaulty so you have zero so 8501 06:21:01,260 --> 06:21:03,240 this way you have here John and Smith 8502 06:21:03,240 --> 06:21:06,058 and then zero so it's important to 8503 06:21:06,058 --> 06:21:09,680 to notice this and to think about how 8504 06:21:09,680 --> 06:21:13,080 this uh to think about this thing 8505 06:21:13,080 --> 06:21:15,600 so this is our discussion about multiple 8506 06:21:15,600 --> 06:21:17,218 Constructors respectively about 8507 06:21:17,218 --> 06:21:20,040 secondary Constructors in this video you 8508 06:21:20,040 --> 06:21:21,480 saw how you can 8509 06:21:21,480 --> 06:21:27,420 pass a different number of values to our 8510 06:21:27,420 --> 06:21:30,420 properties ultimately you pass all the 8511 06:21:30,420 --> 06:21:32,878 values to the properties by calling the 8512 06:21:32,878 --> 06:21:34,320 primary Constructor and providing 8513 06:21:34,320 --> 06:21:37,320 default values because in this way you 8514 06:21:37,320 --> 06:21:42,180 can satisfy all the possible cases so if 8515 06:21:42,180 --> 06:21:45,180 in our case one user only provides the 8516 06:21:45,180 --> 06:21:46,740 name and the last name is not provided 8517 06:21:46,740 --> 06:21:49,260 and the age is going to use the this 8518 06:21:49,260 --> 06:21:50,878 secondary Constructor and it's going to 8519 06:21:50,878 --> 06:21:52,620 pass the last name as default and the 8520 06:21:52,620 --> 06:21:57,000 zero and this is uh in a way 8521 06:21:57,000 --> 06:22:00,320 similar to overloading functions but is 8522 06:22:00,320 --> 06:22:03,480 not the same because what you're doing 8523 06:22:03,480 --> 06:22:07,558 here is overloading the constructors and 8524 06:22:07,558 --> 06:22:10,320 this in this way you can you can think 8525 06:22:10,320 --> 06:22:11,580 about 8526 06:22:11,580 --> 06:22:14,580 why why we're overloading the 8527 06:22:14,580 --> 06:22:17,400 constructors and that is because 8528 06:22:17,400 --> 06:22:20,580 if we didn't have secondary Constructors 8529 06:22:20,580 --> 06:22:24,240 we would have to create another class 8530 06:22:24,240 --> 06:22:27,660 with a different number of parameters 8531 06:22:27,660 --> 06:22:30,138 or the different number of 8532 06:22:30,138 --> 06:22:33,958 properties and we will need to to create 8533 06:22:33,958 --> 06:22:37,860 each object with that specific class 8534 06:22:37,860 --> 06:22:41,280 let's say that we create this object 8535 06:22:41,280 --> 06:22:43,500 which only takes a value for its 8536 06:22:43,500 --> 06:22:45,660 parameter name then we have to create a 8537 06:22:45,660 --> 06:22:47,700 class which we're going to Define only 8538 06:22:47,700 --> 06:22:50,160 the name property and it's gonna then 8539 06:22:50,160 --> 06:22:52,020 we're gonna create that obviously with 8540 06:22:52,020 --> 06:22:53,218 that class 8541 06:22:53,218 --> 06:22:58,260 and if an object if an object 8542 06:22:58,260 --> 06:23:01,920 let's say if an user creates uh if your 8543 06:23:01,920 --> 06:23:03,020 user doesn't 8544 06:23:03,020 --> 06:23:06,540 providing the the name and the last name 8545 06:23:06,540 --> 06:23:08,160 but now the age you have to create 8546 06:23:08,160 --> 06:23:10,860 another class with only the name and the 8547 06:23:10,860 --> 06:23:14,280 last name defined as its properties and 8548 06:23:14,280 --> 06:23:17,280 you can see that this this uh is uh this 8549 06:23:17,280 --> 06:23:20,160 will generate uh duplicated code and so 8550 06:23:20,160 --> 06:23:22,860 on and we can solve all of this by using 8551 06:23:22,860 --> 06:23:25,440 secondary Constructors and 8552 06:23:25,440 --> 06:23:27,240 calling the 8553 06:23:27,240 --> 06:23:30,600 primary Constructor so see you on the 8554 06:23:30,600 --> 06:23:33,260 next video and because quoting is about 8555 06:23:33,260 --> 06:23:35,700 conciseness in the next video we're 8556 06:23:35,700 --> 06:23:37,200 going to look at how we can do the same 8557 06:23:37,200 --> 06:23:39,058 thing that we have here with default 8558 06:23:39,058 --> 06:23:41,280 parameters because 8559 06:23:41,280 --> 06:23:42,780 um 8560 06:23:42,780 --> 06:23:45,840 kotlin supports for its properties for 8561 06:23:45,840 --> 06:23:47,400 its parameters in the constructed 8562 06:23:47,400 --> 06:23:49,200 default values so the same thing that we 8563 06:23:49,200 --> 06:23:51,240 have here can be achieved by providing 8564 06:23:51,240 --> 06:23:54,240 default values to the properties so see 8565 06:23:54,240 --> 06:23:56,160 in the next video 8566 06:23:56,160 --> 06:23:57,600 so now it's time to look at default 8567 06:23:57,600 --> 06:24:00,540 values for our properties and first 8568 06:24:00,540 --> 06:24:04,638 let's delete the secondary Constructors 8569 06:24:07,138 --> 06:24:09,540 and now I have a 8570 06:24:09,540 --> 06:24:12,480 error series because it expects the 8571 06:24:12,480 --> 06:24:13,520 value 8572 06:24:13,520 --> 06:24:15,298 expecting the values for the next 8573 06:24:15,298 --> 06:24:16,620 properties here 8574 06:24:16,620 --> 06:24:18,360 and um 8575 06:24:18,360 --> 06:24:21,000 let's also change this to 8576 06:24:21,000 --> 06:24:23,878 to first name because I refer to it to 8577 06:24:23,878 --> 06:24:26,638 the first name but it was called This is 8578 06:24:26,638 --> 06:24:28,620 it is called name so let's call it first 8579 06:24:28,620 --> 06:24:31,260 name so go right click on it 8580 06:24:31,260 --> 06:24:34,378 go to rename and then to refactor 8581 06:24:34,378 --> 06:24:37,440 to change it in all the places and let's 8582 06:24:37,440 --> 06:24:39,058 call it 8583 06:24:39,058 --> 06:24:41,400 first name and as you can see now let's 8584 06:24:41,400 --> 06:24:42,840 change it in all the place then press 8585 06:24:42,840 --> 06:24:45,080 enter 8586 06:24:45,120 --> 06:24:47,580 so now we have the first name and the 8587 06:24:47,580 --> 06:24:51,420 default properties are default values to 8588 06:24:51,420 --> 06:24:54,180 our properties are basically they are 8589 06:24:54,180 --> 06:24:55,920 similar to default values that we 8590 06:24:55,920 --> 06:24:59,458 provide to our parameters when we talked 8591 06:24:59,458 --> 06:25:01,680 about functions 8592 06:25:01,680 --> 06:25:04,440 then what we do is uh 8593 06:25:04,440 --> 06:25:06,718 let's say that I wanted to provide the 8594 06:25:06,718 --> 06:25:08,400 default value for the last name we just 8595 06:25:08,400 --> 06:25:11,760 put equals so inside the primary 8596 06:25:11,760 --> 06:25:13,860 Constructor we put equals and here we 8597 06:25:13,860 --> 06:25:16,020 put let's say 8598 06:25:16,020 --> 06:25:20,580 last name so now this has the last name 8599 06:25:20,580 --> 06:25:22,980 as a default value if a value is not 8600 06:25:22,980 --> 06:25:25,320 provided for the last name and here you 8601 06:25:25,320 --> 06:25:28,458 can put also equals 8602 06:25:29,700 --> 06:25:32,760 n equals zero and now the error as you 8603 06:25:32,760 --> 06:25:35,160 can see disappeared from both of our 8604 06:25:35,160 --> 06:25:37,740 objects because now 8605 06:25:37,740 --> 06:25:39,900 what is doing is when we don't provide 8606 06:25:39,900 --> 06:25:41,340 the value for the 8607 06:25:41,340 --> 06:25:44,700 last name and for the age then the 8608 06:25:44,700 --> 06:25:46,558 default values that we provide here is 8609 06:25:46,558 --> 06:25:48,000 going to be are going to be used as the 8610 06:25:48,000 --> 06:25:51,180 values for the properties so this is a 8611 06:25:51,180 --> 06:25:54,480 way of achieving the same thing that we 8612 06:25:54,480 --> 06:25:56,340 achieved previously with the secondary 8613 06:25:56,340 --> 06:25:59,040 Constructor but this is more concise 8614 06:25:59,040 --> 06:26:01,740 and the question is when to use one on 8615 06:26:01,740 --> 06:26:04,020 one or the other is 8616 06:26:04,020 --> 06:26:06,420 the um the answer may be if you want to 8617 06:26:06,420 --> 06:26:09,718 execute some code when an object is 8618 06:26:09,718 --> 06:26:11,940 created let's say 8619 06:26:11,940 --> 06:26:14,458 with one of the 8620 06:26:14,458 --> 06:26:16,378 let's say if you wanted to if you want 8621 06:26:16,378 --> 06:26:18,900 to exit some code when you create an 8622 06:26:18,900 --> 06:26:20,280 object then you can use secondary 8623 06:26:20,280 --> 06:26:22,200 Constructor because as I said secondary 8624 06:26:22,200 --> 06:26:24,200 Constructors can have a block of code 8625 06:26:24,200 --> 06:26:28,320 when uh which can be executed but the 8626 06:26:28,320 --> 06:26:31,378 this uh the default because primary 8627 06:26:31,378 --> 06:26:33,718 Constructor does not don't don't have a 8628 06:26:33,718 --> 06:26:36,540 don't have a block of code they you 8629 06:26:36,540 --> 06:26:38,780 cannot do that maybe you could use a 8630 06:26:38,780 --> 06:26:40,798 initializer blocks 8631 06:26:40,798 --> 06:26:46,020 but that is uh open to discussion 8632 06:26:46,020 --> 06:26:48,298 so this is how you can provide the four 8633 06:26:48,298 --> 06:26:49,860 values to 8634 06:26:49,860 --> 06:26:52,138 to the primary Constructor 8635 06:26:52,138 --> 06:26:54,900 in our code was fine it's 8636 06:26:54,900 --> 06:26:58,798 In This Very concise very beautiful and 8637 06:26:58,798 --> 06:27:00,660 the next thing that you can do is that 8638 06:27:00,660 --> 06:27:04,320 you can also use named arguments so you 8639 06:27:04,320 --> 06:27:06,660 can use the because here you use the 8640 06:27:06,660 --> 06:27:09,480 first name and the last name and if I um 8641 06:27:09,480 --> 06:27:13,740 because those are those are both strings 8642 06:27:13,740 --> 06:27:16,260 here you cannot see the distinction but 8643 06:27:16,260 --> 06:27:20,638 if I put here uh now I can use the name 8644 06:27:20,638 --> 06:27:24,557 argument so I can put your first name 8645 06:27:27,540 --> 06:27:30,540 equals so it's similar to the way we 8646 06:27:30,540 --> 06:27:32,638 have name arguments in our function and 8647 06:27:32,638 --> 06:27:35,218 here you can put the text 8648 06:27:35,218 --> 06:27:36,900 John 8649 06:27:36,900 --> 06:27:40,020 also here and this uh 8650 06:27:40,020 --> 06:27:42,480 with this thing you can you can change 8651 06:27:42,480 --> 06:27:44,520 the order in which you need to define 8652 06:27:44,520 --> 06:27:46,558 the values for the parameter so let's 8653 06:27:46,558 --> 06:27:49,458 put your last name 8654 06:27:49,620 --> 06:27:51,240 but we'll say this better let's create 8655 06:27:51,240 --> 06:27:53,040 another object 8656 06:27:53,040 --> 06:27:54,540 is 8657 06:27:54,540 --> 06:27:56,878 user 2 8658 06:27:56,878 --> 06:28:00,378 let's type user 8659 06:28:00,420 --> 06:28:03,859 and let's put here you wanna 8660 06:28:05,520 --> 06:28:08,940 but let's use the name arrowman so let's 8661 06:28:08,940 --> 06:28:11,100 put first the age so we're going to 8662 06:28:11,100 --> 06:28:13,378 provide the value for the age let's say 8663 06:28:13,378 --> 06:28:14,280 um 8664 06:28:14,280 --> 06:28:16,260 19. 8665 06:28:16,260 --> 06:28:18,540 and um 8666 06:28:18,540 --> 06:28:21,958 Let's uh let's type first name so here 8667 06:28:21,958 --> 06:28:24,718 let's put um 8668 06:28:24,718 --> 06:28:28,200 IWANNA because it's a string 8669 06:28:28,200 --> 06:28:31,340 quotation marks 8670 06:28:32,820 --> 06:28:36,920 and for the last name let's say 8671 06:28:39,540 --> 06:28:41,638 let's type also the name argument last 8672 06:28:41,638 --> 06:28:43,500 name and for the last name let's put 8673 06:28:43,500 --> 06:28:45,840 here um 8674 06:28:45,840 --> 06:28:47,520 Dobby 8675 06:28:47,520 --> 06:28:50,180 let's say 8676 06:28:50,820 --> 06:28:54,620 right so what we did here is that 8677 06:28:54,620 --> 06:28:58,440 in the primary Constructor the order for 8678 06:28:58,440 --> 06:29:01,620 for the properties very specific we have 8679 06:29:01,620 --> 06:29:03,058 the first name and the last name and 8680 06:29:03,058 --> 06:29:04,980 then we have the age but with the name 8681 06:29:04,980 --> 06:29:07,680 arguments we first provided the age 8682 06:29:07,680 --> 06:29:11,040 so we provided a value for the age 8683 06:29:11,040 --> 06:29:13,558 product which is the last one so and 8684 06:29:13,558 --> 06:29:15,718 then you provide the value for the first 8685 06:29:15,718 --> 06:29:19,138 name and with the name arguments similar 8686 06:29:19,138 --> 06:29:23,340 to the way used in our functions you can 8687 06:29:23,340 --> 06:29:25,740 pass the values to the properties in 8688 06:29:25,740 --> 06:29:27,480 which order you want irrespective of the 8689 06:29:27,480 --> 06:29:30,180 word in which they defined here so this 8690 06:29:30,180 --> 06:29:33,540 is very powerful if you 8691 06:29:33,540 --> 06:29:36,058 if you ask me it's very interesting 8692 06:29:36,058 --> 06:29:39,360 because you can pass the values to the 8693 06:29:39,360 --> 06:29:41,218 properties irrespective of the world in 8694 06:29:41,218 --> 06:29:43,440 which they are defined 8695 06:29:43,440 --> 06:29:46,218 so this is our discussion about default 8696 06:29:46,218 --> 06:29:51,240 parameters and the name argument in 8697 06:29:51,240 --> 06:29:53,940 with the primary Constructor and you 8698 06:29:53,940 --> 06:29:55,740 cannot use default parameters name 8699 06:29:55,740 --> 06:29:57,058 arguments with the secondary 8700 06:29:57,058 --> 06:29:59,520 Constructors they can only be used the 8701 06:29:59,520 --> 06:30:00,958 primary Constructor 8702 06:30:00,958 --> 06:30:05,057 so see you in the next video 8703 06:30:05,638 --> 06:30:08,400 but uh before I end the video Let's 8704 06:30:08,400 --> 06:30:10,500 actually run this code to see that it 8705 06:30:10,500 --> 06:30:14,520 works in the same way it was working in 8706 06:30:14,520 --> 06:30:17,100 previously when we use the secondary 8707 06:30:17,100 --> 06:30:18,660 Constructors 8708 06:30:18,660 --> 06:30:21,798 so let's run this code 8709 06:30:23,940 --> 06:30:26,340 so you have name Alex last name last 8710 06:30:26,340 --> 06:30:28,740 name because it's using this default 8711 06:30:28,740 --> 06:30:31,138 value that defined here then we have h0 8712 06:30:31,138 --> 06:30:33,360 because it's using this value of defined 8713 06:30:33,360 --> 06:30:34,860 here so this is 8714 06:30:34,860 --> 06:30:36,718 this object 8715 06:30:36,718 --> 06:30:40,020 and then we have name John last name 8716 06:30:40,020 --> 06:30:42,660 Smith a0 because now it's using this 8717 06:30:42,660 --> 06:30:44,458 default value so the same thing can be 8718 06:30:44,458 --> 06:30:46,200 achieved using um 8719 06:30:46,200 --> 06:30:49,920 default parameters and we didn't 8720 06:30:49,920 --> 06:30:52,680 output the values for the 8721 06:30:52,680 --> 06:30:55,378 for the second for the third object that 8722 06:30:55,378 --> 06:30:58,020 you created here in user 2 but you can 8723 06:30:58,020 --> 06:30:59,940 do that as a 8724 06:30:59,940 --> 06:31:03,240 as a challenge for if you want you can 8725 06:31:03,240 --> 06:31:07,500 output the values for this object so see 8726 06:31:07,500 --> 06:31:08,878 you in the next video 8727 06:31:08,878 --> 06:31:10,440 so now it's time to start a discussion 8728 06:31:10,440 --> 06:31:14,160 about Getters and Setters because so far 8729 06:31:14,160 --> 06:31:16,920 enough videos even though we didn't see 8730 06:31:16,920 --> 06:31:20,218 explicitly the Getters and Setters here 8731 06:31:20,218 --> 06:31:23,160 we've used the Getters and Setters every 8732 06:31:23,160 --> 06:31:25,500 time we got the value of a property and 8733 06:31:25,500 --> 06:31:28,138 every time we assign a new value to a 8734 06:31:28,138 --> 06:31:29,458 property 8735 06:31:29,458 --> 06:31:31,020 and um 8736 06:31:31,020 --> 06:31:33,360 the getter and Setter in kotlin are 8737 06:31:33,360 --> 06:31:35,638 implicit so they they are declared they 8738 06:31:35,638 --> 06:31:38,580 are Auto auto generated by default by 8739 06:31:38,580 --> 06:31:41,878 color by cutling for you so every time 8740 06:31:41,878 --> 06:31:44,638 you and let's say you get the value of 8741 06:31:44,638 --> 06:31:46,500 an object let's say let's get the value 8742 06:31:46,500 --> 06:31:48,180 of the user that's first name what 8743 06:31:48,180 --> 06:31:51,718 actually happens here is not that we're 8744 06:31:51,718 --> 06:31:53,700 getting the value of the first name 8745 06:31:53,700 --> 06:31:55,860 directly even though if you look at the 8746 06:31:55,860 --> 06:31:58,440 code this what it seems to be what has 8747 06:31:58,440 --> 06:32:00,600 happened is that every time we get the 8748 06:32:00,600 --> 06:32:04,440 value of of a property that value is 8749 06:32:04,440 --> 06:32:06,480 returned by the getter so you never 8750 06:32:06,480 --> 06:32:09,058 access the property directly and you 8751 06:32:09,058 --> 06:32:13,860 never never does change the the property 8752 06:32:13,860 --> 06:32:16,200 of uh the value of a property directly 8753 06:32:16,200 --> 06:32:18,480 you every you every time you you are 8754 06:32:18,480 --> 06:32:20,520 using the Getters and setters 8755 06:32:20,520 --> 06:32:22,620 and now this discussion is touching on 8756 06:32:22,620 --> 06:32:25,680 on a very important Concept in quadrant 8757 06:32:25,680 --> 06:32:27,900 programming and that is encapsulation so 8758 06:32:27,900 --> 06:32:31,558 encapsulation basically means to 8759 06:32:31,558 --> 06:32:35,820 to protect to hide the inner workings of 8760 06:32:35,820 --> 06:32:39,058 the class from being accessed outside 8761 06:32:39,058 --> 06:32:41,520 and if you look at this syntax that we 8762 06:32:41,520 --> 06:32:43,798 have here it seems that we actually 8763 06:32:43,798 --> 06:32:47,760 accessing the values the properties 8764 06:32:47,760 --> 06:32:49,920 directly but as I said that is not the 8765 06:32:49,920 --> 06:32:51,718 case because we're always using the 8766 06:32:51,718 --> 06:32:55,620 getter and the setter and because we 8767 06:32:55,620 --> 06:32:58,500 always use use the Getters and the 8768 06:32:58,500 --> 06:33:00,420 Setters the getter to get the value and 8769 06:33:00,420 --> 06:33:02,520 the set to to change the value of 8770 06:33:02,520 --> 06:33:03,600 property 8771 06:33:03,600 --> 06:33:07,740 that means that your data your 8772 06:33:07,740 --> 06:33:10,280 properties are never accessed directly 8773 06:33:10,280 --> 06:33:15,900 thus the laws of encapsulation are not 8774 06:33:15,900 --> 06:33:19,260 broken the the in other words you you 8775 06:33:19,260 --> 06:33:21,958 never allow somebody outside of the 8776 06:33:21,958 --> 06:33:24,000 class to access directly that property 8777 06:33:24,000 --> 06:33:26,520 so this is why uh 8778 06:33:26,520 --> 06:33:29,520 this is why we have Getters and Setters 8779 06:33:29,520 --> 06:33:32,040 and this is why the Getters and Setters 8780 06:33:32,040 --> 06:33:35,638 in Kathleen are implicit so they are not 8781 06:33:35,638 --> 06:33:37,680 even declared explicitly here we don't 8782 06:33:37,680 --> 06:33:38,700 see any 8783 06:33:38,700 --> 06:33:41,458 code which it will imply that we have a 8784 06:33:41,458 --> 06:33:44,458 get a Setter here because they are 8785 06:33:44,458 --> 06:33:46,980 automatically and by default generated 8786 06:33:46,980 --> 06:33:48,900 every time you define a property so 8787 06:33:48,900 --> 06:33:51,120 every time you define a property either 8788 06:33:51,120 --> 06:33:53,400 in the primary Constructor or 8789 06:33:53,400 --> 06:33:55,860 inside the class the 8790 06:33:55,860 --> 06:33:58,378 Getters and the get the getter and 8791 06:33:58,378 --> 06:34:01,980 Setters automatically generated for you 8792 06:34:01,980 --> 06:34:04,798 but uh let's say that you want to 8793 06:34:04,798 --> 06:34:07,260 override the getter and Setter to 8794 06:34:07,260 --> 06:34:10,080 provide some additional 8795 06:34:10,080 --> 06:34:12,660 additional code to the code that is by 8796 06:34:12,660 --> 06:34:14,638 the by default provided you can do that 8797 06:34:14,638 --> 06:34:16,860 by changing let's say 8798 06:34:16,860 --> 06:34:20,100 this property to a parameter 8799 06:34:20,100 --> 06:34:22,860 and to override the getter in Setter and 8800 06:34:22,860 --> 06:34:25,200 to see how the Getters and Setters 8801 06:34:25,200 --> 06:34:27,900 actually look under the covers 8802 06:34:27,900 --> 06:34:30,000 just type here VAR 8803 06:34:30,000 --> 06:34:33,480 we Define the property first name 8804 06:34:33,480 --> 06:34:35,280 we assign the first name parameter 8805 06:34:35,280 --> 06:34:38,520 property now the Getters and Setter need 8806 06:34:38,520 --> 06:34:41,120 to need to be overrated 8807 06:34:41,120 --> 06:34:44,280 immediately after the with after we 8808 06:34:44,280 --> 06:34:46,320 Define the property so we cannot have 8809 06:34:46,320 --> 06:34:48,718 some code here and then to override you 8810 06:34:48,718 --> 06:34:51,240 can to override the getter and Setter so 8811 06:34:51,240 --> 06:34:53,160 we need to 8812 06:34:53,160 --> 06:34:54,718 we need we need to Define them 8813 06:34:54,718 --> 06:34:57,000 immediately after we we Define the 8814 06:34:57,000 --> 06:35:00,360 property so we type here get 8815 06:35:00,360 --> 06:35:03,000 and as an attempting you see that I have 8816 06:35:03,000 --> 06:35:04,620 some suggestions then you go down here 8817 06:35:04,620 --> 06:35:06,540 you and you can choose you choose this 8818 06:35:06,540 --> 06:35:07,920 one 8819 06:35:07,920 --> 06:35:10,138 so you have parentheses curl braces and 8820 06:35:10,138 --> 06:35:14,600 inside the curly braces you press enter 8821 06:35:14,700 --> 06:35:17,580 here you type return 8822 06:35:17,580 --> 06:35:19,080 field I'm going to explain immediately 8823 06:35:19,080 --> 06:35:20,638 what is this 8824 06:35:20,638 --> 06:35:23,340 and now we need to override the setter 8825 06:35:23,340 --> 06:35:25,620 and to do that to just type set and I 8826 06:35:25,620 --> 06:35:27,120 have some suggestions 8827 06:35:27,120 --> 06:35:30,900 we also go down here we choose this one 8828 06:35:30,900 --> 06:35:33,540 press enter and hero type 8829 06:35:33,540 --> 06:35:36,420 field again this 8830 06:35:36,420 --> 06:35:39,298 under this identifier and input equals 8831 06:35:39,298 --> 06:35:40,378 to value 8832 06:35:40,378 --> 06:35:44,340 so this is the implicit code that is 8833 06:35:44,340 --> 06:35:46,740 generated that is auto-generated by 8834 06:35:46,740 --> 06:35:49,558 kotlin for you every time you you you 8835 06:35:49,558 --> 06:35:51,718 define a property in your class either 8836 06:35:51,718 --> 06:35:53,520 in the as I said in the primary sector 8837 06:35:53,520 --> 06:35:55,500 or inside the class 8838 06:35:55,500 --> 06:35:59,040 so because in fact if you look here we 8839 06:35:59,040 --> 06:36:00,660 have some underlines and if you hover 8840 06:36:00,660 --> 06:36:02,458 over here it says redundant getter 8841 06:36:02,458 --> 06:36:06,058 remove redundant getter because uh and 8842 06:36:06,058 --> 06:36:08,058 also for the set it under setup because 8843 06:36:08,058 --> 06:36:10,458 they are redundant because they are 8844 06:36:10,458 --> 06:36:13,440 implicitly again Auto generated by 8845 06:36:13,440 --> 06:36:16,500 cotton for you so this quad is redundant 8846 06:36:16,500 --> 06:36:17,700 here you don't need this code because 8847 06:36:17,700 --> 06:36:19,378 Scotland already provides this code 8848 06:36:19,378 --> 06:36:21,660 under the covers for you but if you're 8849 06:36:21,660 --> 06:36:24,540 not such satisfied with this quad with 8850 06:36:24,540 --> 06:36:26,700 this implicit code that is uh Auto 8851 06:36:26,700 --> 06:36:29,638 generated by cotton can you can um you 8852 06:36:29,638 --> 06:36:31,680 can override them like we did here and 8853 06:36:31,680 --> 06:36:33,660 provide some additional code so we can 8854 06:36:33,660 --> 06:36:36,180 put here let's say some text 8855 06:36:36,180 --> 06:36:38,580 so so that every time we get the value 8856 06:36:38,580 --> 06:36:40,980 of our 8857 06:36:40,980 --> 06:36:43,740 we get the value of our first name 8858 06:36:43,740 --> 06:36:45,480 property you're gonna have a pretty 8859 06:36:45,480 --> 06:36:47,340 pretty prefix and let's put here a first 8860 06:36:47,340 --> 06:36:48,958 name 8861 06:36:48,958 --> 06:36:51,958 let's put the colon dollar sign 8862 06:36:51,958 --> 06:36:53,760 and field we're gonna play I'm gonna 8863 06:36:53,760 --> 06:36:55,378 explain immediately what this field is 8864 06:36:55,378 --> 06:36:57,958 but let's say first however 8865 06:36:57,958 --> 06:37:00,780 how this works and here let's put a 8866 06:37:00,780 --> 06:37:02,760 printerland to print a value that is 8867 06:37:02,760 --> 06:37:04,620 passed to the setter and then assigned 8868 06:37:04,620 --> 06:37:06,240 to the first name property so let's put 8869 06:37:06,240 --> 06:37:09,798 here dollar sign 8870 06:37:11,458 --> 06:37:13,860 value was 8871 06:37:13,860 --> 06:37:15,900 assigned 8872 06:37:15,900 --> 06:37:19,260 to First 8873 06:37:21,120 --> 06:37:23,298 foreign 8874 06:37:30,480 --> 06:37:33,900 now I have name so we have our text that 8875 06:37:33,900 --> 06:37:36,480 we have here name and then have first 8876 06:37:36,480 --> 06:37:38,340 name Alex let's actually delete this 8877 06:37:38,340 --> 06:37:40,680 text to make things more clear 8878 06:37:40,680 --> 06:37:43,700 let's delete this text 8879 06:37:43,740 --> 06:37:46,700 let's delete this 8880 06:37:50,340 --> 06:37:53,600 now let's run this code again 8881 06:37:56,280 --> 06:37:59,218 so what we have now is that every time 8882 06:37:59,218 --> 06:38:01,680 we get the value of our property name we 8883 06:38:01,680 --> 06:38:03,958 have we have that prefix first name that 8884 06:38:03,958 --> 06:38:07,020 we have uh overwritten here in the in 8885 06:38:07,020 --> 06:38:09,840 the getter so the getter is called as I 8886 06:38:09,840 --> 06:38:12,360 said the value is returned by the getter 8887 06:38:12,360 --> 06:38:14,700 so together is called it Returns the 8888 06:38:14,700 --> 06:38:16,558 value of the first name property so this 8889 06:38:16,558 --> 06:38:19,500 field is is the first name property but 8890 06:38:19,500 --> 06:38:22,080 is uh it has this identifier field 8891 06:38:22,080 --> 06:38:24,840 because of uh of an important reason 8892 06:38:24,840 --> 06:38:28,020 which I'm gonna explain immediately so 8893 06:38:28,020 --> 06:38:30,660 it Returns the value of the first name 8894 06:38:30,660 --> 06:38:31,740 property 8895 06:38:31,740 --> 06:38:34,558 with the prefix first name and we have 8896 06:38:34,558 --> 06:38:37,320 first name the text that we have here 8897 06:38:37,320 --> 06:38:39,958 then I have the value of the first name 8898 06:38:39,958 --> 06:38:42,660 property we have Alex in our case 8899 06:38:42,660 --> 06:38:45,840 because our object has the name Alex as 8900 06:38:45,840 --> 06:38:48,240 the property name Alex 8901 06:38:48,240 --> 06:38:50,580 then for the 8902 06:38:50,580 --> 06:38:52,620 for the setter we don't have any code 8903 06:38:52,620 --> 06:38:55,500 here also we have the first name prefix 8904 06:38:55,500 --> 06:38:58,138 for John so our our every time we get uh 8905 06:38:58,138 --> 06:39:02,900 we get the first name property by 8906 06:39:02,900 --> 06:39:06,420 of our objects created by by using the 8907 06:39:06,420 --> 06:39:09,540 user class it gets that value it returns 8908 06:39:09,540 --> 06:39:11,700 that value through the getter and the 8909 06:39:11,700 --> 06:39:14,400 setter we don't have value was assigned 8910 06:39:14,400 --> 06:39:15,420 to first name property because the 8911 06:39:15,420 --> 06:39:19,080 setter is called only one we change the 8912 06:39:19,080 --> 06:39:22,798 value of uh of uh of our properties and 8913 06:39:22,798 --> 06:39:25,378 to do that we type user like we did in 8914 06:39:25,378 --> 06:39:27,180 our first video about objects or another 8915 06:39:27,180 --> 06:39:28,700 programming 8916 06:39:28,700 --> 06:39:31,138 DOT first name 8917 06:39:31,138 --> 06:39:34,020 equals let's say 8918 06:39:34,020 --> 06:39:36,558 oh 8919 06:39:36,780 --> 06:39:39,540 let's put here 8920 06:39:39,540 --> 06:39:42,138 Vlad 8921 06:39:42,420 --> 06:39:44,638 so now if I run this code 8922 06:39:44,638 --> 06:39:46,200 now the setter is going to be called 8923 06:39:46,200 --> 06:39:47,760 because the setter is called as I said 8924 06:39:47,760 --> 06:39:50,400 only when it we change the value of our 8925 06:39:50,400 --> 06:39:54,000 of our of of our property 8926 06:39:54,000 --> 06:39:56,878 so we get uh 8927 06:39:56,878 --> 06:39:59,160 get Vlad was assigned to first name 8928 06:39:59,160 --> 06:40:01,440 property so the code inside the setter 8929 06:40:01,440 --> 06:40:02,940 was called 8930 06:40:02,940 --> 06:40:07,378 because this code that we type here is 8931 06:40:07,378 --> 06:40:10,200 calling the setter so our code only only 8932 06:40:10,200 --> 06:40:12,900 always goes to the setter and the code 8933 06:40:12,900 --> 06:40:15,240 that we have here user that first name 8934 06:40:15,240 --> 06:40:18,058 Vlad what it's actually doing is 8935 06:40:18,058 --> 06:40:21,298 passing that value Vlad to 8936 06:40:21,298 --> 06:40:25,500 here so this for this uh this parameter 8937 06:40:25,500 --> 06:40:28,320 it that we have here is receiving the 8938 06:40:28,320 --> 06:40:31,558 value of Vlad and then that value is 8939 06:40:31,558 --> 06:40:34,320 assigned to first name property again we 8940 06:40:34,320 --> 06:40:36,360 have this field the identifier for a 8941 06:40:36,360 --> 06:40:37,920 reason and the reason is going to be 8942 06:40:37,920 --> 06:40:39,718 explained immediately 8943 06:40:39,718 --> 06:40:42,180 so uh 8944 06:40:42,180 --> 06:40:46,080 the Getters and Setters are working well 8945 06:40:46,080 --> 06:40:49,440 and they are as they said again the use 8946 06:40:49,440 --> 06:40:52,260 always when we get or 8947 06:40:52,260 --> 06:40:55,860 assign a new value to our properties 8948 06:40:55,860 --> 06:40:58,920 now what is this field identifier why I 8949 06:40:58,920 --> 06:41:00,298 didn't 8950 06:41:00,298 --> 06:41:02,218 type here uh 8951 06:41:02,218 --> 06:41:07,200 first name and here I didn't put uh 8952 06:41:07,200 --> 06:41:09,718 first name equals to Value so whatever 8953 06:41:09,718 --> 06:41:11,820 value is passed to the setter then that 8954 06:41:11,820 --> 06:41:13,740 value is assigned to the first name 8955 06:41:13,740 --> 06:41:15,900 property so why 8956 06:41:15,900 --> 06:41:18,298 why don't we don't do that 8957 06:41:18,298 --> 06:41:22,320 and um the reason uh for that is that 8958 06:41:22,320 --> 06:41:25,280 because 8959 06:41:26,580 --> 06:41:30,360 this syntax user.first name user first 8960 06:41:30,360 --> 06:41:31,500 name 8961 06:41:31,500 --> 06:41:33,900 user.first name equals Vlad is calling 8962 06:41:33,900 --> 06:41:35,280 the setter 8963 06:41:35,280 --> 06:41:38,820 if we put here first name equals value 8964 06:41:38,820 --> 06:41:41,520 so first name equals Alex it will be 8965 06:41:41,520 --> 06:41:45,240 equivalent to to call the setter again 8966 06:41:45,240 --> 06:41:47,700 so this line of code that we have here 8967 06:41:47,700 --> 06:41:49,680 it will call the setter again it will 8968 06:41:49,680 --> 06:41:51,718 pass the value to the center and then it 8969 06:41:51,718 --> 06:41:53,940 will assign the value again to the first 8970 06:41:53,940 --> 06:41:56,760 name so it will call the Center inside 8971 06:41:56,760 --> 06:41:59,160 the setter forever it will be it will 8972 06:41:59,160 --> 06:42:01,920 generate an error and to avoid the that 8973 06:42:01,920 --> 06:42:04,500 recursive call that infinite call we 8974 06:42:04,500 --> 06:42:07,500 need to use this field identifier so the 8975 06:42:07,500 --> 06:42:09,420 field in an identifier is the first name 8976 06:42:09,420 --> 06:42:12,440 property but it has this 8977 06:42:12,440 --> 06:42:15,378 special 8978 06:42:15,378 --> 06:42:19,680 use that it bypasses that error that it 8979 06:42:19,680 --> 06:42:21,540 will be generated if you use the first 8980 06:42:21,540 --> 06:42:22,860 name here 8981 06:42:22,860 --> 06:42:27,360 so in fact let's um 8982 06:42:27,360 --> 06:42:31,020 let's put here a first name to see 8983 06:42:31,020 --> 06:42:34,020 so if we put here first name 8984 06:42:34,020 --> 06:42:36,298 instead of field 8985 06:42:36,298 --> 06:42:38,878 and we have an error here 8986 06:42:38,878 --> 06:42:41,040 let's put here a 8987 06:42:41,040 --> 06:42:42,180 field 8988 06:42:42,180 --> 06:42:44,280 because 8989 06:42:44,280 --> 06:42:47,280 I want uh I want the error from the 8990 06:42:47,280 --> 06:42:49,138 setter to to get in the console not 8991 06:42:49,138 --> 06:42:51,540 there or from this thing here so now if 8992 06:42:51,540 --> 06:42:52,860 you run the squad 8993 06:42:52,860 --> 06:42:55,080 as I said that will generate a recursive 8994 06:42:55,080 --> 06:42:57,480 call it will call the getter over and 8995 06:42:57,480 --> 06:42:59,660 over 8996 06:43:00,540 --> 06:43:03,980 so get the error 8997 06:43:04,320 --> 06:43:07,320 so you see we have Vlad was assigned to 8998 06:43:07,320 --> 06:43:08,820 first name property Vlad was assigned to 8999 06:43:08,820 --> 06:43:11,160 first name property vad and so on that 9000 06:43:11,160 --> 06:43:12,080 is because 9001 06:43:12,080 --> 06:43:14,280 the setter 9002 06:43:14,280 --> 06:43:18,058 it's calling the setter forever 9003 06:43:18,058 --> 06:43:20,878 it's a recourse of call because this 9004 06:43:20,878 --> 06:43:22,740 line of code here it's calling the 9005 06:43:22,740 --> 06:43:25,860 setter it's it's a assigning the value 9006 06:43:25,860 --> 06:43:28,740 to the sending the value to the seta 9007 06:43:28,740 --> 06:43:30,958 then it's that values assigned again to 9008 06:43:30,958 --> 06:43:33,058 the first name property which in turn 9009 06:43:33,058 --> 06:43:35,638 close again the setter and so on so this 9010 06:43:35,638 --> 06:43:38,400 field the identifier is used because it 9011 06:43:38,400 --> 06:43:40,700 has this characteristic of 9012 06:43:40,700 --> 06:43:43,138 bypassing that error this is why we're 9013 06:43:43,138 --> 06:43:45,240 using the field instead of the first 9014 06:43:45,240 --> 06:43:46,500 name 9015 06:43:46,500 --> 06:43:50,280 property here and it's basically the 9016 06:43:50,280 --> 06:43:52,680 first name property but it has this 9017 06:43:52,680 --> 06:43:54,798 specific characteristics which 9018 06:43:54,798 --> 06:43:57,718 characteristics which bypasses the 9019 06:43:57,718 --> 06:44:01,558 recursive call which we got when we've 9020 06:44:01,558 --> 06:44:03,780 used the first name property 9021 06:44:03,780 --> 06:44:07,100 now if you run the code 9022 06:44:10,200 --> 06:44:12,780 now I have no error because the recourse 9023 06:44:12,780 --> 06:44:14,040 of call is not happening because you're 9024 06:44:14,040 --> 06:44:15,660 using the field which bypasses that 9025 06:44:15,660 --> 06:44:16,500 error 9026 06:44:16,500 --> 06:44:18,660 so this is why we're using uh and we're 9027 06:44:18,660 --> 06:44:21,180 having an encodone has this thing filled 9028 06:44:21,180 --> 06:44:23,040 and this the field can only be used 9029 06:44:23,040 --> 06:44:26,520 inside the getter or the setter not uh 9030 06:44:26,520 --> 06:44:29,400 not anywhere else also either getter or 9031 06:44:29,400 --> 06:44:30,718 the setter 9032 06:44:30,718 --> 06:44:33,058 and the same is true about the getter 9033 06:44:33,058 --> 06:44:34,200 this is why you need to use the field 9034 06:44:34,200 --> 06:44:36,360 identifier inside the getter also 9035 06:44:36,360 --> 06:44:39,360 because it will generate the recursive 9036 06:44:39,360 --> 06:44:41,280 call again 9037 06:44:41,280 --> 06:44:45,840 now to to illustrate better because you 9038 06:44:45,840 --> 06:44:47,340 maybe find the 9039 06:44:47,340 --> 06:44:49,558 confusing this discussion about Getters 9040 06:44:49,558 --> 06:44:52,860 and Setters what they really 9041 06:44:52,860 --> 06:44:56,280 are they are equivalent to having two 9042 06:44:56,280 --> 06:44:58,260 functions which 9043 06:44:58,260 --> 06:45:01,138 update the first name property and 9044 06:45:01,138 --> 06:45:02,760 return the first name property so they 9045 06:45:02,760 --> 06:45:04,378 they are equivalent to having 9046 06:45:04,378 --> 06:45:07,200 a function called Set uh 9047 06:45:07,200 --> 06:45:09,298 first name 9048 06:45:09,298 --> 06:45:11,638 and this function defines a parameter 9049 06:45:11,638 --> 06:45:14,580 called let's call it new value 9050 06:45:14,580 --> 06:45:18,200 is going to be of type string 9051 06:45:18,718 --> 06:45:21,180 let's put curly braces and here we type 9052 06:45:21,180 --> 06:45:24,900 this DOT first name 9053 06:45:24,900 --> 06:45:26,638 equals 9054 06:45:26,638 --> 06:45:28,798 new value 9055 06:45:28,798 --> 06:45:32,218 and the geta equivalent to having a 9056 06:45:32,218 --> 06:45:33,718 function 9057 06:45:33,718 --> 06:45:34,860 called 9058 06:45:34,860 --> 06:45:36,120 get 9059 06:45:36,120 --> 06:45:38,520 first name 9060 06:45:38,520 --> 06:45:39,900 and this is not going to take any 9061 06:45:39,900 --> 06:45:42,600 parameter is going is we're going to say 9062 06:45:42,600 --> 06:45:44,458 say explicit here that it's going to 9063 06:45:44,458 --> 06:45:46,558 return a string because it's gonna only 9064 06:45:46,558 --> 06:45:48,840 return a value 9065 06:45:48,840 --> 06:45:51,240 and this is going to return the first 9066 06:45:51,240 --> 06:45:53,958 name property 9067 06:45:54,480 --> 06:45:57,480 so we put here this 9068 06:45:57,480 --> 06:46:02,100 that first name so the getter and Setter 9069 06:46:02,100 --> 06:46:05,660 that we have here are similar this is uh 9070 06:46:05,660 --> 06:46:09,000 illustrative for you to have in mind to 9071 06:46:09,000 --> 06:46:12,240 having two functions which are setting 9072 06:46:12,240 --> 06:46:14,040 the first name property or updating the 9073 06:46:14,040 --> 06:46:16,680 first name property and are returning 9074 06:46:16,680 --> 06:46:20,240 let's change this to First 9075 06:46:20,280 --> 06:46:23,280 and are returning the first uh the value 9076 06:46:23,280 --> 06:46:25,740 of the first name property but because 9077 06:46:25,740 --> 06:46:28,920 constantly and it's about conciseness it 9078 06:46:28,920 --> 06:46:32,520 has this special syntax and they are 9079 06:46:32,520 --> 06:46:35,340 automatically generated for you because 9080 06:46:35,340 --> 06:46:37,700 uh as I said accordingly it's about 9081 06:46:37,700 --> 06:46:40,620 conciseness so you don't need to declare 9082 06:46:40,620 --> 06:46:42,900 those things every time you create a 9083 06:46:42,900 --> 06:46:46,138 class so let's delete this so have in 9084 06:46:46,138 --> 06:46:49,620 mind that under the covers is we have 9085 06:46:49,620 --> 06:46:52,200 something like this so let's delete this 9086 06:46:52,200 --> 06:46:54,660 now the arrow disappears 9087 06:46:54,660 --> 06:46:57,240 also you can have a shorter way of 9088 06:46:57,240 --> 06:46:58,620 writing the centering letter if you 9089 06:46:58,620 --> 06:47:01,200 don't provide some additional code so if 9090 06:47:01,200 --> 06:47:03,540 I delete this 9091 06:47:03,540 --> 06:47:06,200 and this 9092 06:47:06,298 --> 06:47:11,600 I can put here get equals field 9093 06:47:11,638 --> 06:47:14,660 and um 9094 06:47:16,580 --> 06:47:19,978 yes this is this okay because this is 9095 06:47:19,978 --> 06:47:22,920 like having a single line a single 9096 06:47:22,920 --> 06:47:24,600 function a single line expression 9097 06:47:24,600 --> 06:47:26,700 function because we're returning the 9098 06:47:26,700 --> 06:47:30,600 value to the function get so you can 9099 06:47:30,600 --> 06:47:33,478 write it like this because or I I show 9100 06:47:33,478 --> 06:47:35,340 you this photo because maybe you'll see 9101 06:47:35,340 --> 06:47:38,160 a written in this way and you may be 9102 06:47:38,160 --> 06:47:40,920 wondering why that's because this is 9103 06:47:40,920 --> 06:47:44,760 like having a a single body expression 9104 06:47:44,760 --> 06:47:46,080 function 9105 06:47:46,080 --> 06:47:47,820 so you can write it like this if you 9106 06:47:47,820 --> 06:47:50,340 want but again if you hover over this is 9107 06:47:50,340 --> 06:47:51,540 still already done let's so let's press 9108 06:47:51,540 --> 06:47:54,660 Ctrl G to have the previous code 9109 06:47:54,660 --> 06:47:56,878 so this is our discussion about Getters 9110 06:47:56,878 --> 06:47:59,760 and Setters and see you on the next 9111 06:47:59,760 --> 06:48:00,840 video 9112 06:48:00,840 --> 06:48:02,400 so now it's time to start a discussion 9113 06:48:02,400 --> 06:48:04,920 about the late init keyword and how we 9114 06:48:04,920 --> 06:48:07,740 can use the latent keyword with the our 9115 06:48:07,740 --> 06:48:10,020 properties but first let's delete the 9116 06:48:10,020 --> 06:48:11,100 code that we have from the previous 9117 06:48:11,100 --> 06:48:13,260 video because don't need this code let's 9118 06:48:13,260 --> 06:48:15,180 also delete this code 9119 06:48:15,180 --> 06:48:18,420 let's delete those two objects 9120 06:48:18,420 --> 06:48:19,860 that's 9121 06:48:19,860 --> 06:48:22,798 and lazil is property let's declare it 9122 06:48:22,798 --> 06:48:25,620 here so let's put the VAR keyword 9123 06:48:25,620 --> 06:48:27,780 and let's also delete the default values 9124 06:48:27,780 --> 06:48:30,020 here 9125 06:48:34,200 --> 06:48:36,298 and let's press Ctrl alt form on the 9126 06:48:36,298 --> 06:48:37,620 code 9127 06:48:37,620 --> 06:48:39,240 now we need to provide the values here 9128 06:48:39,240 --> 06:48:41,160 because we don't have default values for 9129 06:48:41,160 --> 06:48:43,558 the last name property and the age so 9130 06:48:43,558 --> 06:48:45,958 I'm going to type here 9131 06:48:45,958 --> 06:48:48,420 dobbyn let's say 9132 06:48:48,420 --> 06:48:50,940 age 23. 9133 06:48:50,940 --> 06:48:52,440 now 9134 06:48:52,440 --> 06:48:54,600 as you already know if you declare 9135 06:48:54,600 --> 06:48:57,000 inside the class 9136 06:48:57,000 --> 06:48:59,400 a property let's say that that property 9137 06:48:59,400 --> 06:49:01,740 is called favorite movie and it's going 9138 06:49:01,740 --> 06:49:05,520 to store the favorite movie of the user 9139 06:49:05,520 --> 06:49:07,458 you cannot let this variable 9140 06:49:07,458 --> 06:49:09,958 uninitialize so let's say that is going 9141 06:49:09,958 --> 06:49:11,520 to be of type string 9142 06:49:11,520 --> 06:49:12,958 so you cannot let this variable 9143 06:49:12,958 --> 06:49:14,878 uninitialize you need to provide a value 9144 06:49:14,878 --> 06:49:17,940 either by assigning directly 9145 06:49:17,940 --> 06:49:21,240 a literal value here or by receiving the 9146 06:49:21,240 --> 06:49:25,020 value through through a parameter 9147 06:49:25,020 --> 06:49:26,700 defined in the Constructor 9148 06:49:26,700 --> 06:49:29,940 but um that is not completely true 9149 06:49:29,940 --> 06:49:31,920 because 9150 06:49:31,920 --> 06:49:35,160 sometimes you'll want to to have a 9151 06:49:35,160 --> 06:49:37,558 property declared inside the class but 9152 06:49:37,558 --> 06:49:40,020 you don't have to assign a Twitter 9153 06:49:40,020 --> 06:49:42,840 valued uh right away so you want to 9154 06:49:42,840 --> 06:49:45,200 assign the value to the property later 9155 06:49:45,200 --> 06:49:47,580 and to be able to do that you need to 9156 06:49:47,580 --> 06:49:50,580 use the late init keyword and to use the 9157 06:49:50,580 --> 06:49:52,620 late neat keyword you just type here 9158 06:49:52,620 --> 06:49:54,718 late it and you have a suggestion later 9159 06:49:54,718 --> 06:49:55,620 need 9160 06:49:55,620 --> 06:49:58,680 and later need search for initialize 9161 06:49:58,680 --> 06:50:01,500 letter later so what we're saying here 9162 06:50:01,500 --> 06:50:02,540 is 9163 06:50:02,540 --> 06:50:06,660 initialize let later this property 9164 06:50:06,660 --> 06:50:09,058 favorite movies so you say in the to the 9165 06:50:09,058 --> 06:50:11,400 compiler hey I'm gonna 9166 06:50:11,400 --> 06:50:13,798 initialize this uh property favorite 9167 06:50:13,798 --> 06:50:16,138 movie but I'm gonna initialize it later 9168 06:50:16,138 --> 06:50:19,740 so believe me and uh 9169 06:50:19,740 --> 06:50:21,718 to do that you just 9170 06:50:21,718 --> 06:50:23,940 type user 9171 06:50:23,940 --> 06:50:27,120 that so you whatever object you create 9172 06:50:27,120 --> 06:50:29,100 with this class and you have a defined 9173 06:50:29,100 --> 06:50:31,620 latent property 9174 06:50:31,620 --> 06:50:33,360 favorite movie 9175 06:50:33,360 --> 06:50:35,400 and you put equals and then you put 9176 06:50:35,400 --> 06:50:39,558 let's say Interstellar here 9177 06:50:42,120 --> 06:50:45,120 so now we've initialized our favorite 9178 06:50:45,120 --> 06:50:47,760 movie property but we initialize the its 9179 06:50:47,760 --> 06:50:49,558 value later so we didn't provide the 9180 06:50:49,558 --> 06:50:51,900 value here directly inside the class 9181 06:50:51,900 --> 06:50:54,840 either by as I said by providing a 9182 06:50:54,840 --> 06:50:56,340 literal value or by 9183 06:50:56,340 --> 06:50:59,280 providing a value through the parameter 9184 06:50:59,280 --> 06:51:02,218 defined in the primary Constructor so 9185 06:51:02,218 --> 06:51:04,320 this is how you can use the 9186 06:51:04,320 --> 06:51:08,478 latent keyword if you want to 9187 06:51:08,700 --> 06:51:11,040 assign a value to a property later so 9188 06:51:11,040 --> 06:51:12,180 you don't want to assign the value 9189 06:51:12,180 --> 06:51:14,638 directly or or you don't want to cite 9190 06:51:14,638 --> 06:51:17,420 the the value more correctly said 9191 06:51:17,420 --> 06:51:19,680 right away so you want to assign the 9192 06:51:19,680 --> 06:51:22,020 value later you just put the latest 9193 06:51:22,020 --> 06:51:23,760 keyword in front of the property and 9194 06:51:23,760 --> 06:51:26,218 then you declare the property as 9195 06:51:26,218 --> 06:51:28,680 you the way you declare a simple 9196 06:51:28,680 --> 06:51:29,760 variable 9197 06:51:29,760 --> 06:51:32,718 right and if you don't initialize this 9198 06:51:32,718 --> 06:51:36,660 this property and you want a new output 9199 06:51:36,660 --> 06:51:38,638 the value in the console so let's say 9200 06:51:38,638 --> 06:51:41,040 that I put here a printerland 9201 06:51:41,040 --> 06:51:43,558 and I type user that 9202 06:51:43,558 --> 06:51:45,000 favorite movie 9203 06:51:45,000 --> 06:51:48,680 and I don't initialize it 9204 06:51:52,020 --> 06:51:55,740 and if I run this code now this 9205 06:51:55,740 --> 06:51:58,138 favorite movie property is not 9206 06:51:58,138 --> 06:52:00,840 initialized doesn't have a value 9207 06:52:00,840 --> 06:52:04,080 we're gonna have an error 9208 06:52:04,080 --> 06:52:06,958 and the error is uh 9209 06:52:06,958 --> 06:52:10,100 it's very specific 9210 06:52:12,000 --> 06:52:14,180 so you have exception thread main 9211 06:52:14,180 --> 06:52:16,978 kotlin unofficialized projects access 9212 06:52:16,978 --> 06:52:19,680 exception we have and then it says later 9213 06:52:19,680 --> 06:52:21,540 need property favorite movie has not 9214 06:52:21,540 --> 06:52:23,218 been initialized so it's very specific 9215 06:52:23,218 --> 06:52:25,500 in saying that hey you didn't you said 9216 06:52:25,500 --> 06:52:27,478 that you're going to initialize that 9217 06:52:27,478 --> 06:52:30,420 property but you didn't and uh down here 9218 06:52:30,420 --> 06:52:33,600 you try to to Output in the console the 9219 06:52:33,600 --> 06:52:35,760 value of the favorite movie but we don't 9220 06:52:35,760 --> 06:52:38,458 have provided a value and it's better to 9221 06:52:38,458 --> 06:52:41,160 have this uh this is called an exception 9222 06:52:41,160 --> 06:52:43,378 so it's better to have this energy Pro 9223 06:52:43,378 --> 06:52:46,500 and initialize property access exception 9224 06:52:46,500 --> 06:52:49,680 then having a null pointer pointer 9225 06:52:49,680 --> 06:52:52,080 exception because 9226 06:52:52,080 --> 06:52:54,840 this is why kotlin actually enforces 9227 06:52:54,840 --> 06:52:58,218 this behavior of uh 9228 06:52:58,218 --> 06:53:02,040 assigning values to the variable to the 9229 06:53:02,040 --> 06:53:04,558 variables so it Cotillion forces to 9230 06:53:04,558 --> 06:53:07,680 assign values to the variables and not 9231 06:53:07,680 --> 06:53:10,558 let them uninitialize because if you 9232 06:53:10,558 --> 06:53:12,600 don't initialize 9233 06:53:12,600 --> 06:53:16,080 uh you don't provide the value to a bear 9234 06:53:16,080 --> 06:53:18,958 to a variable or two property then when 9235 06:53:18,958 --> 06:53:21,840 you try to access that variable or that 9236 06:53:21,840 --> 06:53:23,878 property and let's say that you have an 9237 06:53:23,878 --> 06:53:24,798 app 9238 06:53:24,798 --> 06:53:28,740 that is gonna throw which is going to 9239 06:53:28,740 --> 06:53:30,120 throw what is called the null pointer 9240 06:53:30,120 --> 06:53:32,940 exception and that is very bad because 9241 06:53:32,940 --> 06:53:35,240 those kind of excursions are called only 9242 06:53:35,240 --> 06:53:38,400 at runtime and not at compile time so 9243 06:53:38,400 --> 06:53:42,718 your app can work let's say completely 9244 06:53:42,718 --> 06:53:46,620 well and when you press a button I don't 9245 06:53:46,620 --> 06:53:52,440 know to to see the the favorite movie of 9246 06:53:52,440 --> 06:53:55,860 a user then you get an error and adopt 9247 06:53:55,860 --> 06:53:58,860 crashes so this is why you need to avoid 9248 06:53:58,860 --> 06:54:02,400 those null pointer exceptions and kotlin 9249 06:54:02,400 --> 06:54:04,200 as I said enforces this behavior of 9250 06:54:04,200 --> 06:54:05,820 providing um 9251 06:54:05,820 --> 06:54:08,280 values always to the variables to the 9252 06:54:08,280 --> 06:54:09,718 end to the properties 9253 06:54:09,718 --> 06:54:12,420 and if you don't want to provide the 9254 06:54:12,420 --> 06:54:14,580 value to the 9255 06:54:14,580 --> 06:54:16,920 property then you can use this late on 9256 06:54:16,920 --> 06:54:20,280 it and it will show this uh different uh 9257 06:54:20,280 --> 06:54:22,320 exception called latent property 9258 06:54:22,320 --> 06:54:24,120 favorite movie has not finished so it's 9259 06:54:24,120 --> 06:54:28,020 it's a different exception than the null 9260 06:54:28,020 --> 06:54:30,540 pointer exception but with the latent 9261 06:54:30,540 --> 06:54:33,120 keyword we can still have as I said that 9262 06:54:33,120 --> 06:54:34,740 behavior of not initializing the 9263 06:54:34,740 --> 06:54:37,080 variable but you still need to 9264 06:54:37,080 --> 06:54:39,600 initialize it later because this one is 9265 06:54:39,600 --> 06:54:41,580 called late init 9266 06:54:41,580 --> 06:54:45,120 but this is not uh saying that the 9267 06:54:45,120 --> 06:54:46,920 latent 9268 06:54:46,920 --> 06:54:51,000 error that is shown here is not also uh 9269 06:54:51,000 --> 06:54:53,458 having the same behavior like the null 9270 06:54:53,458 --> 06:54:54,718 pointer exception that I talked about 9271 06:54:54,718 --> 06:54:58,620 because the late init error that is that 9272 06:54:58,620 --> 06:54:59,820 we have here 9273 06:54:59,820 --> 06:55:02,700 if you have an app it will uh also 9274 06:55:02,700 --> 06:55:05,340 Crusher up but the later neat keyword 9275 06:55:05,340 --> 06:55:08,160 allows us to not provide as I said the 9276 06:55:08,160 --> 06:55:11,160 value literally a little value here or a 9277 06:55:11,160 --> 06:55:12,740 value through the 9278 06:55:12,740 --> 06:55:15,298 constructed by the by assigning the 9279 06:55:15,298 --> 06:55:16,798 parameter here with the value pass of 9280 06:55:16,798 --> 06:55:18,000 the parameter 9281 06:55:18,000 --> 06:55:20,000 and the 9282 06:55:20,000 --> 06:55:24,020 is uh is our 9283 06:55:24,020 --> 06:55:26,580 responsibility to initialize that 9284 06:55:26,580 --> 06:55:28,920 variable later so that you don't get 9285 06:55:28,920 --> 06:55:31,680 into the same problems that we talked 9286 06:55:31,680 --> 06:55:34,978 about so we don't get this error so it's 9287 06:55:34,978 --> 06:55:36,718 up to you to 9288 06:55:36,718 --> 06:55:40,378 to declare a a property as late and 9289 06:55:40,378 --> 06:55:44,820 eaten then provide a value later because 9290 06:55:44,820 --> 06:55:47,580 if you don't it will also Crush you up 9291 06:55:47,580 --> 06:55:51,660 when you try to access uh by uh I don't 9292 06:55:51,660 --> 06:55:54,298 know in Europe somehow you want to 9293 06:55:54,298 --> 06:55:57,058 access the value of that property 9294 06:55:57,058 --> 06:55:59,940 so I always have a have a decent mind 9295 06:55:59,940 --> 06:56:01,260 that you need to initialize that 9296 06:56:01,260 --> 06:56:04,080 variable later 9297 06:56:04,080 --> 06:56:06,540 also I should say that the latent 9298 06:56:06,540 --> 06:56:08,400 keyword only works with 9299 06:56:08,400 --> 06:56:09,958 classes 9300 06:56:09,958 --> 06:56:12,900 and it does not work with primitive type 9301 06:56:12,900 --> 06:56:15,180 so even though the as I said the 9302 06:56:15,180 --> 06:56:17,340 integers and all the data types that we 9303 06:56:17,340 --> 06:56:19,680 looked at and also the 9304 06:56:19,680 --> 06:56:22,978 Boolean and Char are classes 9305 06:56:22,978 --> 06:56:25,440 we cannot use latent adhered with them 9306 06:56:25,440 --> 06:56:27,718 because if I put here in you can see 9307 06:56:27,718 --> 06:56:29,580 that I have an error 9308 06:56:29,580 --> 06:56:31,798 that it says latony modifiers not 9309 06:56:31,798 --> 06:56:33,660 allowed on properties of primitive types 9310 06:56:33,660 --> 06:56:37,740 because uh I'm not gonna get into much 9311 06:56:37,740 --> 06:56:40,260 of details but all we need to know to 9312 06:56:40,260 --> 06:56:41,760 know is that 9313 06:56:41,760 --> 06:56:43,860 when 9314 06:56:43,860 --> 06:56:46,978 the int class and all the Primitive 9315 06:56:46,978 --> 06:56:52,138 types which are classes uh at least 9316 06:56:52,138 --> 06:56:55,860 at the surface when they are compiled I 9317 06:56:55,860 --> 06:56:58,138 think they are compiled into primitive 9318 06:56:58,138 --> 06:56:59,580 types so they are not compiled into 9319 06:56:59,580 --> 06:57:02,760 classes so this I think is the reason 9320 06:57:02,760 --> 06:57:06,420 that you don't you can't use this latent 9321 06:57:06,420 --> 06:57:09,120 in the keyword but for other classes 9322 06:57:09,120 --> 06:57:12,478 because a string is is a data type but 9323 06:57:12,478 --> 06:57:14,760 it's a special data type as I said you 9324 06:57:14,760 --> 06:57:18,180 know in a previous previous video and 9325 06:57:18,180 --> 06:57:20,340 because of that you can use the latent 9326 06:57:20,340 --> 06:57:21,600 keyword and you can use the latent 9327 06:57:21,600 --> 06:57:23,400 latent keyword 9328 06:57:23,400 --> 06:57:25,260 with other classes so I can put here 9329 06:57:25,260 --> 06:57:27,180 user if you want 9330 06:57:27,180 --> 06:57:29,638 and it works because users is also class 9331 06:57:29,638 --> 06:57:32,340 is this class that we Define here 9332 06:57:32,340 --> 06:57:37,080 so it's good to to to remember this 9333 06:57:37,080 --> 06:57:39,840 now I'm going to end the video and see 9334 06:57:39,840 --> 06:57:41,940 you in the next video but first let's uh 9335 06:57:41,940 --> 06:57:44,280 press Ctrl G to have the code that you 9336 06:57:44,280 --> 06:57:46,878 had previously 9337 06:57:47,580 --> 06:57:51,298 so uh now I'm gonna end in the video and 9338 06:57:51,298 --> 06:57:52,620 see you in the next video 9339 06:57:52,620 --> 06:57:54,240 so now it's time to start a discussion 9340 06:57:54,240 --> 06:57:56,520 about companion object but to do that 9341 06:57:56,520 --> 06:57:59,820 I'm gonna delete the class that we have 9342 06:57:59,820 --> 06:58:01,440 here 9343 06:58:01,440 --> 06:58:03,058 and I'm going to paste it inside our 9344 06:58:03,058 --> 06:58:05,458 classes file because we're gonna need 9345 06:58:05,458 --> 06:58:07,978 this class later in our videos 9346 06:58:07,978 --> 06:58:09,478 and I'm gonna also delete this code 9347 06:58:09,478 --> 06:58:11,218 because you don't need this code 9348 06:58:11,218 --> 06:58:13,558 and let's say that 9349 06:58:13,558 --> 06:58:17,660 let's create the class called calculator 9350 06:58:20,760 --> 06:58:24,600 is not going to Define any properties 9351 06:58:24,600 --> 06:58:26,160 you know it's going to have a simple 9352 06:58:26,160 --> 06:58:28,620 function called sum which is going to 9353 06:58:28,620 --> 06:58:30,540 sum two numbers 9354 06:58:30,540 --> 06:58:32,160 so we're going to Define two parameters 9355 06:58:32,160 --> 06:58:34,440 here a 9356 06:58:34,440 --> 06:58:38,040 it's going to be an INT a b 9357 06:58:38,040 --> 06:58:40,080 also on it 9358 06:58:40,080 --> 06:58:42,298 n is going to return the sound so we put 9359 06:58:42,298 --> 06:58:45,020 colon at the end of our 9360 06:58:45,020 --> 06:58:47,520 parenthesis or function we put int 9361 06:58:47,520 --> 06:58:49,500 because this is what you're going to 9362 06:58:49,500 --> 06:58:50,580 return 9363 06:58:50,580 --> 06:58:54,840 and we put calibration type return a 9364 06:58:54,840 --> 06:58:57,420 plus b or you can put this in a single 9365 06:58:57,420 --> 06:59:00,600 body expression function so you can put 9366 06:59:00,600 --> 06:59:04,100 the code like this if you want 9367 06:59:05,100 --> 06:59:07,260 so we can put here uh you can also 9368 06:59:07,260 --> 06:59:08,760 delete a type you can put here equals 9369 06:59:08,760 --> 06:59:11,580 and this will work but I'm gonna 9370 06:59:11,580 --> 06:59:13,620 keep it like this 9371 06:59:13,620 --> 06:59:17,520 now if you want to use this sum function 9372 06:59:17,520 --> 06:59:19,920 that is defined in this uh 9373 06:59:19,920 --> 06:59:22,138 class calculator first you need to 9374 06:59:22,138 --> 06:59:24,360 create an object without class so we 9375 06:59:24,360 --> 06:59:26,520 type here Val I'm going to call it 9376 06:59:26,520 --> 06:59:28,820 calculator 9377 06:59:29,760 --> 06:59:32,160 and I'm gonna create here a new object 9378 06:59:32,160 --> 06:59:34,458 calculator 9379 06:59:35,458 --> 06:59:37,558 Constructor doesn't have any properties 9380 06:59:37,558 --> 06:59:41,040 any parameters and if you want to 9381 06:59:41,040 --> 06:59:43,500 to call that some function we need to 9382 06:59:43,500 --> 06:59:46,458 use our calculator 9383 06:59:46,620 --> 06:59:49,500 variable or I'll confrater object or 9384 06:59:49,500 --> 06:59:53,040 reference and we put dot like we did in 9385 06:59:53,040 --> 06:59:55,138 our research so use the dot notation so 9386 06:59:55,138 --> 06:59:57,478 now we're calling the sum function on 9387 06:59:57,478 --> 06:59:59,280 our calculator object that we created 9388 06:59:59,280 --> 07:00:02,160 online too let me put when we call our 9389 07:00:02,160 --> 07:00:04,260 function sum inside the buttons you pass 9390 07:00:04,260 --> 07:00:08,120 two numbers let's say 5 and 10. 9391 07:00:08,160 --> 07:00:09,718 and uh 9392 07:00:09,718 --> 07:00:11,760 let's capture this value that is 9393 07:00:11,760 --> 07:00:13,080 returned by the 9394 07:00:13,080 --> 07:00:15,120 by the function in a variable so let's 9395 07:00:15,120 --> 07:00:17,600 put here 9396 07:00:17,638 --> 07:00:20,478 Val result 9397 07:00:26,638 --> 07:00:28,320 because that is going to return the sum 9398 07:00:28,320 --> 07:00:30,660 of 5 and 10. of course that you can put 9399 07:00:30,660 --> 07:00:32,878 that in a printerland 9400 07:00:32,878 --> 07:00:35,820 statement if one but I put it in this 9401 07:00:35,820 --> 07:00:39,120 elaborate way to to make 9402 07:00:39,120 --> 07:00:41,520 more clear what you're doing and here 9403 07:00:41,520 --> 07:00:44,718 I'm going to type println 9404 07:00:44,940 --> 07:00:46,740 result 9405 07:00:46,740 --> 07:00:49,020 so now if I run the squad 9406 07:00:49,020 --> 07:00:52,260 you're gonna see 15 in output line in 9407 07:00:52,260 --> 07:00:54,739 the console 9408 07:00:55,260 --> 07:00:57,360 so let's increase this 9409 07:00:57,360 --> 07:01:00,958 so we have 15. and the way we did it is 9410 07:01:00,958 --> 07:01:02,878 we created an object like we did so many 9411 07:01:02,878 --> 07:01:05,700 times then we want that object use the 9412 07:01:05,700 --> 07:01:07,978 dot notation to curl that function on 9413 07:01:07,978 --> 07:01:10,378 that specific object on this object and 9414 07:01:10,378 --> 07:01:13,200 that function takes as arguments as easy 9415 07:01:13,200 --> 07:01:14,340 as 9416 07:01:14,340 --> 07:01:18,298 values to its parameters to integers we 9417 07:01:18,298 --> 07:01:20,340 pass 5 and 10 and then it returns as 9418 07:01:20,340 --> 07:01:23,280 well and we retain we 9419 07:01:23,280 --> 07:01:24,840 we capture that value in the result 9420 07:01:24,840 --> 07:01:26,520 variable and then we want to put that 9421 07:01:26,520 --> 07:01:30,058 value in the console and get 15. but um 9422 07:01:30,058 --> 07:01:32,760 if you think about the way we did this 9423 07:01:32,760 --> 07:01:36,360 thing is that the only way we can call 9424 07:01:36,360 --> 07:01:38,218 the sum function is by creating an 9425 07:01:38,218 --> 07:01:40,558 object and then calling the function on 9426 07:01:40,558 --> 07:01:43,798 that object but it doesn't make a lot of 9427 07:01:43,798 --> 07:01:47,458 sense if you have a calculator class 9428 07:01:47,458 --> 07:01:48,900 to call 9429 07:01:48,900 --> 07:01:51,540 to create every time through our code 9430 07:01:51,540 --> 07:01:53,100 every time you need to use the sound 9431 07:01:53,100 --> 07:01:54,958 function to create an object throughout 9432 07:01:54,958 --> 07:01:57,240 your code a better way it would be if 9433 07:01:57,240 --> 07:02:00,240 you could call that sum function 9434 07:02:00,240 --> 07:02:03,360 somehow without declaring uh without 9435 07:02:03,360 --> 07:02:05,638 creating an object and then using the 9436 07:02:05,638 --> 07:02:08,878 the object to call the function and to 9437 07:02:08,878 --> 07:02:10,740 you to do that we need to use what is 9438 07:02:10,740 --> 07:02:12,900 called a companion object and to use a 9439 07:02:12,900 --> 07:02:14,820 component object we go inside our class 9440 07:02:14,820 --> 07:02:18,780 we type companion object so we can press 9441 07:02:18,780 --> 07:02:21,298 enter to fill in a component object for 9442 07:02:21,298 --> 07:02:23,218 us or you can type that then we put 9443 07:02:23,218 --> 07:02:25,500 curly braces and inside the curl brace 9444 07:02:25,500 --> 07:02:27,540 you put our codes and in this case we're 9445 07:02:27,540 --> 07:02:30,680 going to put our sum function 9446 07:02:31,680 --> 07:02:34,340 now 9447 07:02:34,860 --> 07:02:36,660 you you're going to see something now we 9448 07:02:36,660 --> 07:02:38,400 have an error here 9449 07:02:38,400 --> 07:02:40,378 now this 9450 07:02:40,378 --> 07:02:43,260 function this sum function doesn't 9451 07:02:43,260 --> 07:02:46,920 belong to an object to an instance that 9452 07:02:46,920 --> 07:02:49,218 is created with the class calculator 9453 07:02:49,218 --> 07:02:53,400 instead it belongs to the class itself 9454 07:02:53,400 --> 07:02:56,160 and not to the not to any object that we 9455 07:02:56,160 --> 07:02:56,878 create 9456 07:02:56,878 --> 07:03:00,420 those the variables and the functions 9457 07:03:00,420 --> 07:03:02,458 that are the functions that are declared 9458 07:03:02,458 --> 07:03:05,700 inside the component object are also 9459 07:03:05,700 --> 07:03:06,780 called 9460 07:03:06,780 --> 07:03:09,900 class variables or 9461 07:03:09,900 --> 07:03:11,878 class functions because they don't 9462 07:03:11,878 --> 07:03:14,040 belong to they don't belong to a 9463 07:03:14,040 --> 07:03:15,840 specific object that you create with 9464 07:03:15,840 --> 07:03:19,378 that with that class instead they belong 9465 07:03:19,378 --> 07:03:21,718 to the class itself so you don't need an 9466 07:03:21,718 --> 07:03:24,600 instance to call that function and uh 9467 07:03:24,600 --> 07:03:26,820 that uh 9468 07:03:26,820 --> 07:03:30,298 implies that we can't use it with an 9469 07:03:30,298 --> 07:03:32,160 object because it belongs only to the 9470 07:03:32,160 --> 07:03:34,080 class and let's see how you can use it 9471 07:03:34,080 --> 07:03:36,360 using the class without creating an 9472 07:03:36,360 --> 07:03:40,400 object we just type calculator 9473 07:03:40,400 --> 07:03:44,218 dot sum and here you pass again 9474 07:03:44,218 --> 07:03:47,280 5 and 10. so 9475 07:03:47,280 --> 07:03:50,760 we just use our name or the name of our 9476 07:03:50,760 --> 07:03:54,600 class we put dot sum and now we can use 9477 07:03:54,600 --> 07:03:57,478 the function without uh then without us 9478 07:03:57,478 --> 07:03:59,280 needing to create an object and then 9479 07:03:59,280 --> 07:04:01,740 calling the function on that object so 9480 07:04:01,740 --> 07:04:05,160 this is a very powerful because uh 9481 07:04:05,160 --> 07:04:06,600 you will need this in your code 9482 07:04:06,600 --> 07:04:09,600 sometimes when you don't uh it it 9483 07:04:09,600 --> 07:04:12,120 actually doesn't make sense to create an 9484 07:04:12,120 --> 07:04:14,760 object every time you you call let's say 9485 07:04:14,760 --> 07:04:17,520 a sum function for a calculator 9486 07:04:17,520 --> 07:04:20,280 now we can also retain that value that 9487 07:04:20,280 --> 07:04:23,958 is written in a variable also result 9488 07:04:25,500 --> 07:04:28,020 put equals 9489 07:04:28,020 --> 07:04:31,940 now we can print this in the console 9490 07:04:41,340 --> 07:04:43,860 so we get also 15 but this time we 9491 07:04:43,860 --> 07:04:46,200 didn't create an object we didn't create 9492 07:04:46,200 --> 07:04:48,660 a new instance and we call that we call 9493 07:04:48,660 --> 07:04:50,820 the sum function on that object we just 9494 07:04:50,820 --> 07:04:53,280 type the calculator class and call that 9495 07:04:53,280 --> 07:04:56,160 function on directly on the class 9496 07:04:56,160 --> 07:04:57,840 all right so this is how you can use the 9497 07:04:57,840 --> 07:05:00,298 companion object 9498 07:05:00,298 --> 07:05:02,280 and this is also true for a variable so 9499 07:05:02,280 --> 07:05:05,040 if you put a variable here let's say Max 9500 07:05:05,040 --> 07:05:06,718 and you put an arbitrary number here 9501 07:05:06,718 --> 07:05:08,580 let's say 100 9502 07:05:08,580 --> 07:05:10,860 we can call this variable 9503 07:05:10,860 --> 07:05:14,160 also by typing calculator the name of 9504 07:05:14,160 --> 07:05:17,540 the class Dot and have our Max variable 9505 07:05:17,540 --> 07:05:21,798 uh like in contrast with how we did the 9506 07:05:21,798 --> 07:05:25,378 how you call the the properties in the 9507 07:05:25,378 --> 07:05:28,320 past by creating an object and using 9508 07:05:28,320 --> 07:05:30,540 that object to curl that property on 9509 07:05:30,540 --> 07:05:33,058 that object in this case you just use 9510 07:05:33,058 --> 07:05:35,340 the class and call the the variable and 9511 07:05:35,340 --> 07:05:37,680 this also can be can be useful if you 9512 07:05:37,680 --> 07:05:38,958 have some 9513 07:05:38,958 --> 07:05:42,240 constants to which you want to use in 9514 07:05:42,240 --> 07:05:45,180 our throughout your quad so let's delete 9515 07:05:45,180 --> 07:05:48,000 this so you can use also with variables 9516 07:05:48,000 --> 07:05:50,160 and if you think about uh 9517 07:05:50,160 --> 07:05:52,920 uh at our 9518 07:05:52,920 --> 07:05:56,218 one of our first lessons about the data 9519 07:05:56,218 --> 07:05:58,680 ties when you look at the minimum and 9520 07:05:58,680 --> 07:06:01,080 maximum value I I type something like 9521 07:06:01,080 --> 07:06:03,478 this Val Max 9522 07:06:03,478 --> 07:06:05,100 and I 9523 07:06:05,100 --> 07:06:07,500 to get the maximum volume I type int 9524 07:06:07,500 --> 07:06:09,780 that 9525 07:06:09,780 --> 07:06:12,360 max volume and this is using also a 9526 07:06:12,360 --> 07:06:13,978 companion object because as you can see 9527 07:06:13,978 --> 07:06:16,798 the integer class because the Primitive 9528 07:06:16,798 --> 07:06:18,718 types are also classes at least on the 9529 07:06:18,718 --> 07:06:19,680 surface 9530 07:06:19,680 --> 07:06:21,978 not when they are compiled on the jvm 9531 07:06:21,978 --> 07:06:25,558 they also use companion objects like we 9532 07:06:25,558 --> 07:06:29,638 have here to to get to to get the 9533 07:06:29,638 --> 07:06:32,218 properties and to get the functions 9534 07:06:32,218 --> 07:06:34,260 because you don't we didn't create an 9535 07:06:34,260 --> 07:06:37,080 instance of our integer of with our 9536 07:06:37,080 --> 07:06:40,020 integer class and then call that max 9537 07:06:40,020 --> 07:06:42,478 value on the object in fact if you hold 9538 07:06:42,478 --> 07:06:45,600 control on this integer class and if you 9539 07:06:45,600 --> 07:06:48,478 actually hover over it and after you 9540 07:06:48,478 --> 07:06:50,520 hold Ctrl it says 9541 07:06:50,520 --> 07:06:53,700 public companion object of int so if you 9542 07:06:53,700 --> 07:06:55,978 hold Ctrl and if you press and if you 9543 07:06:55,978 --> 07:06:57,478 press click now 9544 07:06:57,478 --> 07:07:02,360 it will open this primitives.kt file 9545 07:07:03,420 --> 07:07:05,100 and inside 9546 07:07:05,100 --> 07:07:08,940 primitive.gt if I have our end class and 9547 07:07:08,940 --> 07:07:10,440 we have a companion object inside the 9548 07:07:10,440 --> 07:07:13,138 companion you have this public constant 9549 07:07:13,138 --> 07:07:14,280 value we're going to look at the 9550 07:07:14,280 --> 07:07:16,320 constants in the negative but the point 9551 07:07:16,320 --> 07:07:19,138 here is that it's using the companion 9552 07:07:19,138 --> 07:07:22,080 object to to declare is declaring the 9553 07:07:22,080 --> 07:07:24,180 the this minimum value the maximum value 9554 07:07:24,180 --> 07:07:26,040 inside The Company audio so that you 9555 07:07:26,040 --> 07:07:27,478 don't need to create an instance an 9556 07:07:27,478 --> 07:07:30,840 object every time you need to use them 9557 07:07:30,840 --> 07:07:32,400 so 9558 07:07:32,400 --> 07:07:34,200 this is our discussion about compounding 9559 07:07:34,200 --> 07:07:36,420 object and see you on the next video I'm 9560 07:07:36,420 --> 07:07:37,978 going to close this file now 9561 07:07:37,978 --> 07:07:39,900 and if you if you want you can look you 9562 07:07:39,900 --> 07:07:41,638 can look in that Primitives file if you 9563 07:07:41,638 --> 07:07:45,540 want just take a look look what is there 9564 07:07:45,540 --> 07:07:47,700 so see in the next video 9565 07:07:47,700 --> 07:07:49,320 so now it's time to start a discussion 9566 07:07:49,320 --> 07:07:51,360 about Singleton but first i'm going to 9567 07:07:51,360 --> 07:07:52,798 copy this code I'm going to copy this 9568 07:07:52,798 --> 07:07:54,900 class with the companion object and I'm 9569 07:07:54,900 --> 07:07:56,218 going to paste it inside the classes 9570 07:07:56,218 --> 07:07:57,298 file 9571 07:07:57,298 --> 07:07:59,878 and if you don't have this code Discord 9572 07:07:59,878 --> 07:08:01,320 because you create a new project just 9573 07:08:01,320 --> 07:08:02,878 type the code that I'm going to type in 9574 07:08:02,878 --> 07:08:04,558 a second so let's delete the code inside 9575 07:08:04,558 --> 07:08:06,478 the main function 9576 07:08:06,478 --> 07:08:09,660 now what is a single then 9577 07:08:09,660 --> 07:08:11,580 a Singleton is a design pattern in 9578 07:08:11,580 --> 07:08:14,520 kotlin with which you want to have 9579 07:08:14,520 --> 07:08:17,280 only one instance of a particular web 9580 07:08:17,280 --> 07:08:19,798 object so you want to have only one 9581 07:08:19,798 --> 07:08:21,780 instance of a particular object and you 9582 07:08:21,780 --> 07:08:23,718 want to have that instance available 9583 07:08:23,718 --> 07:08:26,458 globally tutorial program so you don't 9584 07:08:26,458 --> 07:08:28,740 need to create an instance every time 9585 07:08:28,740 --> 07:08:30,540 you need to use that 9586 07:08:30,540 --> 07:08:33,600 that object in a different part of your 9587 07:08:33,600 --> 07:08:34,860 code 9588 07:08:34,860 --> 07:08:36,900 and there are 9589 07:08:36,900 --> 07:08:39,180 project practical reason for having only 9590 07:08:39,180 --> 07:08:40,920 one instance of a class let's say that 9591 07:08:40,920 --> 07:08:42,360 you have a database 9592 07:08:42,360 --> 07:08:44,218 you want to you want to have only one 9593 07:08:44,218 --> 07:08:46,680 instance of that database which creates 9594 07:08:46,680 --> 07:08:49,558 the database which gives the data and so 9595 07:08:49,558 --> 07:08:51,240 on because if you have multiple 9596 07:08:51,240 --> 07:08:53,638 instances if you able you are able to 9597 07:08:53,638 --> 07:08:56,218 create multiple objects with that class 9598 07:08:56,218 --> 07:08:59,280 then you'll have multiple databases in 9599 07:08:59,280 --> 07:09:00,240 your 9600 07:09:00,240 --> 07:09:03,360 phone or on our PC and that is not good 9601 07:09:03,360 --> 07:09:07,920 because you're using more memory than 9602 07:09:07,920 --> 07:09:10,798 you should and you have a duplicate 9603 07:09:10,798 --> 07:09:12,718 duplicated 9604 07:09:12,718 --> 07:09:16,320 duplicated and unnecessary code 9605 07:09:16,320 --> 07:09:19,020 so to solve that what we need to do is 9606 07:09:19,020 --> 07:09:21,718 to create only one instance of a 9607 07:09:21,718 --> 07:09:24,120 particular class and uh 9608 07:09:24,120 --> 07:09:27,240 having this example in mind this is what 9609 07:09:27,240 --> 07:09:30,180 we're gonna do next and first I'm going 9610 07:09:30,180 --> 07:09:33,360 to show you how we we did the 9611 07:09:33,360 --> 07:09:36,718 in the past how we created a Singleton 9612 07:09:36,718 --> 07:09:40,378 without having the object keyword so you 9613 07:09:40,378 --> 07:09:41,638 go down here 9614 07:09:41,638 --> 07:09:43,558 and first we're going to create a 9615 07:09:43,558 --> 07:09:45,120 Singleton in the world wave so we type 9616 07:09:45,120 --> 07:09:46,260 here class you're going to use our 9617 07:09:46,260 --> 07:09:48,240 example database not going to create a 9618 07:09:48,240 --> 07:09:50,520 real database we're gonna just use this 9619 07:09:50,520 --> 07:09:52,080 as an example 9620 07:09:52,080 --> 07:09:55,440 and to make to restrict the creation of 9621 07:09:55,440 --> 07:09:57,718 instances with this database class we 9622 07:09:57,718 --> 07:09:59,400 need to make the Constructor private so 9623 07:09:59,400 --> 07:10:03,000 we type private and uh when you use an 9624 07:10:03,000 --> 07:10:04,680 access modifier with the primary 9625 07:10:04,680 --> 07:10:06,840 Constructor you need to use the 9626 07:10:06,840 --> 07:10:09,780 Constructor keyword other otherwise will 9627 07:10:09,780 --> 07:10:11,820 have an error so if I delete the 9628 07:10:11,820 --> 07:10:14,878 Constructor keyword here 9629 07:10:14,878 --> 07:10:18,680 I have an error which says 9630 07:10:19,700 --> 07:10:23,458 after modifiers or Pro of primary 9631 07:10:23,458 --> 07:10:27,859 Constructors so we put Constructor 9632 07:10:28,280 --> 07:10:31,920 and inside the 9633 07:10:31,920 --> 07:10:34,260 inside the glass 9634 07:10:34,260 --> 07:10:37,320 now because we cannot create instances 9635 07:10:37,320 --> 07:10:39,360 of this class that means that we cannot 9636 07:10:39,360 --> 07:10:43,440 uh access any of the properties or the 9637 07:10:43,440 --> 07:10:45,718 functions of this class and to to to 9638 07:10:45,718 --> 07:10:47,878 have access to a function which is going 9639 07:10:47,878 --> 07:10:51,180 to return an instance of this class we 9640 07:10:51,180 --> 07:10:53,218 need to use a companion object because 9641 07:10:53,218 --> 07:10:56,218 with companion like this you can have 9642 07:10:56,218 --> 07:11:00,120 properties and functions being allowed 9643 07:11:00,120 --> 07:11:01,920 to being to be accessed outside of the 9644 07:11:01,920 --> 07:11:04,798 class without creating an instance 9645 07:11:04,798 --> 07:11:07,500 and here we type again 9646 07:11:07,500 --> 07:11:10,500 the private access modifier we call this 9647 07:11:10,500 --> 07:11:13,260 instance and it's going to be a database 9648 07:11:13,260 --> 07:11:15,540 a nullable database 9649 07:11:15,540 --> 07:11:18,978 should be valid there 9650 07:11:19,260 --> 07:11:22,080 instance 9651 07:11:22,080 --> 07:11:26,100 database and as I said nullable so we 9652 07:11:26,100 --> 07:11:29,940 can assign null to it 9653 07:11:29,940 --> 07:11:32,458 then we Define a function called get 9654 07:11:32,458 --> 07:11:34,200 instance 9655 07:11:34,200 --> 07:11:35,878 and this function is going to return an 9656 07:11:35,878 --> 07:11:37,920 instance so for instance of our database 9657 07:11:37,920 --> 07:11:39,420 class 9658 07:11:39,420 --> 07:11:42,058 only one instance one one single 9659 07:11:42,058 --> 07:11:43,978 instance not multiple instances 9660 07:11:43,978 --> 07:11:45,900 and to do that first to check to see if 9661 07:11:45,900 --> 07:11:47,940 the instance is null so if this is the 9662 07:11:47,940 --> 07:11:51,120 first time that we want to access 9663 07:11:51,120 --> 07:11:52,860 this uh 9664 07:11:52,860 --> 07:11:55,680 instance then it's gonna return true if 9665 07:11:55,680 --> 07:11:57,180 not it's going to return false so it 9666 07:11:57,180 --> 07:11:59,218 checks if it's equal to null if it's 9667 07:11:59,218 --> 07:12:01,500 equal to null so our instance hasn't 9668 07:12:01,500 --> 07:12:02,878 been initialized 9669 07:12:02,878 --> 07:12:05,400 you know type instance equals to 9670 07:12:05,400 --> 07:12:07,878 database 9671 07:12:10,440 --> 07:12:12,840 right so if the instance is equal to 9672 07:12:12,840 --> 07:12:14,700 nulled that means that the instance has 9673 07:12:14,700 --> 07:12:16,320 been initialized we initialize the 9674 07:12:16,320 --> 07:12:19,080 database so we can initialize the class 9675 07:12:19,080 --> 07:12:21,000 inside the class we cannot initialize it 9676 07:12:21,000 --> 07:12:22,260 outside 9677 07:12:22,260 --> 07:12:23,878 else 9678 07:12:23,878 --> 07:12:27,780 so if this condition fails the the code 9679 07:12:27,780 --> 07:12:30,180 below is going to be red else you're 9680 07:12:30,180 --> 07:12:32,878 gonna return the instance so if the 9681 07:12:32,878 --> 07:12:36,180 instance is not null and this uh this 9682 07:12:36,180 --> 07:12:39,000 code is not going to be a 9683 07:12:39,000 --> 07:12:41,160 this condition is going to be false and 9684 07:12:41,160 --> 07:12:42,180 the code will not be as a good inside 9685 07:12:42,180 --> 07:12:43,920 the if it's gonna we're gonna return the 9686 07:12:43,920 --> 07:12:45,680 instance but here you go you should 9687 07:12:45,680 --> 07:12:48,600 return the database 9688 07:12:48,600 --> 07:12:50,458 and it should be a knowledgeable 9689 07:12:50,458 --> 07:12:51,540 database 9690 07:12:51,540 --> 07:12:53,400 what is our Arrow disappeared let's 9691 07:12:53,400 --> 07:12:55,080 delete this 9692 07:12:55,080 --> 07:12:59,120 let's press Ctrl alt there 9693 07:12:59,760 --> 07:13:02,160 all right now how we're gonna use this 9694 07:13:02,160 --> 07:13:04,260 first 9695 07:13:04,260 --> 07:13:05,520 we type here 9696 07:13:05,520 --> 07:13:09,058 Val instance 9697 07:13:09,660 --> 07:13:13,440 input equals and type database Dot and 9698 07:13:13,440 --> 07:13:14,940 I'm going to use our function get 9699 07:13:14,940 --> 07:13:17,040 instance 9700 07:13:17,040 --> 07:13:22,020 and uh if we we print this instance in 9701 07:13:22,020 --> 07:13:25,760 the console so we type here instance 9702 07:13:26,400 --> 07:13:29,600 if you run the squad 9703 07:13:30,180 --> 07:13:32,700 so now we're calling the 9704 07:13:32,700 --> 07:13:34,920 fan gets instance inside the company of 9705 07:13:34,920 --> 07:13:37,217 object 9706 07:13:37,500 --> 07:13:39,420 we get this code that this code 9707 07:13:39,420 --> 07:13:42,120 represents the 9708 07:13:42,120 --> 07:13:44,700 location memory of the instance so we 9709 07:13:44,700 --> 07:13:48,080 have this code here and if we try to 9710 07:13:48,080 --> 07:13:50,940 instantiate this class so we put here 9711 07:13:50,940 --> 07:13:53,520 the Constructor if we do that then we 9712 07:13:53,520 --> 07:13:55,138 get an error because the Constructor is 9713 07:13:55,138 --> 07:13:56,760 private so we cannot create an instance 9714 07:13:56,760 --> 07:13:58,500 and 9715 07:13:58,500 --> 07:14:01,200 when you call the get instance function 9716 07:14:01,200 --> 07:14:04,638 so when we did this 9717 07:14:08,820 --> 07:14:11,280 first check to see if the instance was 9718 07:14:11,280 --> 07:14:13,440 equal to null and it was equal so now 9719 07:14:13,440 --> 07:14:15,478 then created the instance and then it 9720 07:14:15,478 --> 07:14:16,740 returned the instance 9721 07:14:16,740 --> 07:14:19,440 but if you call this 9722 07:14:19,440 --> 07:14:21,360 function again so let's call it 9723 07:14:21,360 --> 07:14:24,000 instance2 9724 07:14:24,000 --> 07:14:26,340 because database that 9725 07:14:26,340 --> 07:14:29,218 get instance now this will evaluate to 9726 07:14:29,218 --> 07:14:32,160 false because the instance is not equal 9727 07:14:32,160 --> 07:14:34,680 to null we have an instance and it's 9728 07:14:34,680 --> 07:14:36,478 going to come down here and it's going 9729 07:14:36,478 --> 07:14:37,798 to return the instance they're already 9730 07:14:37,798 --> 07:14:40,740 creating instance so if I 9731 07:14:40,740 --> 07:14:44,180 put a printerland here 9732 07:14:44,400 --> 07:14:47,820 and I'm gonna output the instance tool 9733 07:14:47,820 --> 07:14:49,740 you're gonna see that there is no 9734 07:14:49,740 --> 07:14:52,740 instance to you have the same instance 9735 07:14:52,740 --> 07:14:55,378 because it's you it's returning already 9736 07:14:55,378 --> 07:14:57,900 creating created the instance so we have 9737 07:14:57,900 --> 07:15:00,780 the same code here meaning 9738 07:15:00,780 --> 07:15:04,080 this translates to that you have the 9739 07:15:04,080 --> 07:15:06,120 same instance so no matter how many 9740 07:15:06,120 --> 07:15:08,520 times you use database I get instance if 9741 07:15:08,520 --> 07:15:10,020 it's the first time then it's going to 9742 07:15:10,020 --> 07:15:11,580 create the instance and it's going to 9743 07:15:11,580 --> 07:15:14,280 return in essence if you call that again 9744 07:15:14,280 --> 07:15:16,558 like we do for instance 2 then it's 9745 07:15:16,558 --> 07:15:19,160 gonna return the already created users 9746 07:15:19,160 --> 07:15:21,718 so in this way you have the same 9747 07:15:21,718 --> 07:15:23,878 instance no matter how many times you 9748 07:15:23,878 --> 07:15:26,160 you call the get instance function 9749 07:15:26,160 --> 07:15:28,500 because it will always return the same 9750 07:15:28,500 --> 07:15:30,478 instance because we 9751 07:15:30,478 --> 07:15:33,000 because of our logic that we created 9752 07:15:33,000 --> 07:15:35,458 here but there is a shorter way to 9753 07:15:35,458 --> 07:15:37,500 achieve the same thing by using the 9754 07:15:37,500 --> 07:15:40,740 object keyword and this is what we're 9755 07:15:40,740 --> 07:15:43,340 gonna do next 9756 07:15:43,920 --> 07:15:45,840 so let's close this 9757 07:15:45,840 --> 07:15:47,760 now let's see how we can create a 9758 07:15:47,760 --> 07:15:50,040 Singleton and which is going to have the 9759 07:15:50,040 --> 07:15:53,160 same behavior that we created here so 9760 07:15:53,160 --> 07:15:55,320 it's gonna be only one instance using 9761 07:15:55,320 --> 07:15:57,478 the object keyword so I'm going to type 9762 07:15:57,478 --> 07:16:01,020 down here object 9763 07:16:03,180 --> 07:16:06,558 I'm going to call it database 9764 07:16:06,900 --> 07:16:08,700 now let's delete this code because we 9765 07:16:08,700 --> 07:16:10,680 cannot have a 9766 07:16:10,680 --> 07:16:14,900 two objects which have the same name 9767 07:16:16,080 --> 07:16:18,180 and we cannot have a Constructor here 9768 07:16:18,180 --> 07:16:21,660 because it's a Singleton so you put only 9769 07:16:21,660 --> 07:16:23,280 curly braces 9770 07:16:23,280 --> 07:16:25,260 you can have properties and functions 9771 07:16:25,260 --> 07:16:28,138 inside the Singleton created with you 9772 07:16:28,138 --> 07:16:29,580 with the 9773 07:16:29,580 --> 07:16:32,040 object kios leslie.com inside Dimension 9774 07:16:32,040 --> 07:16:33,958 frame function too 9775 07:16:33,958 --> 07:16:35,820 so uh 9776 07:16:35,820 --> 07:16:39,360 Now by just typing object database now 9777 07:16:39,360 --> 07:16:41,040 we have the same thing that we had 9778 07:16:41,040 --> 07:16:43,740 previously just by typing object and 9779 07:16:43,740 --> 07:16:46,200 database so this will always return the 9780 07:16:46,200 --> 07:16:46,920 same 9781 07:16:46,920 --> 07:16:50,160 instance and we have only this code 9782 07:16:50,160 --> 07:16:53,340 object and the name of the object so the 9783 07:16:53,340 --> 07:16:56,700 in the instance of the object is going 9784 07:16:56,700 --> 07:16:58,680 to be created the first time you call it 9785 07:16:58,680 --> 07:17:01,020 and we can put an initializer block to 9786 07:17:01,020 --> 07:17:02,580 have some code to be executed here so 9787 07:17:02,580 --> 07:17:04,620 let's put a printerland 9788 07:17:04,620 --> 07:17:07,638 let's say yo 9789 07:17:10,440 --> 07:17:14,700 this is a database created 9790 07:17:14,700 --> 07:17:17,520 and to you to use this just type let's 9791 07:17:17,520 --> 07:17:19,978 put a printerland 9792 07:17:19,978 --> 07:17:23,120 and type database 9793 07:17:23,458 --> 07:17:26,717 and now if you run this 9794 07:17:28,740 --> 07:17:30,440 and the first time you call the 9795 07:17:30,440 --> 07:17:33,478 Singleton that time the Singleton is 9796 07:17:33,478 --> 07:17:34,740 going to be 9797 07:17:34,740 --> 07:17:37,558 it's going to be created so I have 9798 07:17:37,558 --> 07:17:39,120 database created then we have this code 9799 07:17:39,120 --> 07:17:40,978 which represents the 9800 07:17:40,978 --> 07:17:43,740 or Stringer is the string representation 9801 07:17:43,740 --> 07:17:46,920 of the object and if you press Ctrl D 9802 07:17:46,920 --> 07:17:49,798 here look what happens 9803 07:17:49,798 --> 07:17:51,958 we have 9804 07:17:51,958 --> 07:17:54,840 database created so because this is only 9805 07:17:54,840 --> 07:17:57,718 one instance this object is a single 9806 07:17:57,718 --> 07:18:01,080 turn it calls the initializer block only 9807 07:18:01,080 --> 07:18:03,840 once when it initializes itself so when 9808 07:18:03,840 --> 07:18:05,458 we call the database for the first time 9809 07:18:05,458 --> 07:18:08,638 it is initialized and it calls the code 9810 07:18:08,638 --> 07:18:10,680 inside the init block and after that it 9811 07:18:10,680 --> 07:18:11,760 only 9812 07:18:11,760 --> 07:18:14,340 it output this thing representation the 9813 07:18:14,340 --> 07:18:16,378 second time we call the database we 9814 07:18:16,378 --> 07:18:18,298 don't have database created because the 9815 07:18:18,298 --> 07:18:20,100 instance was already created and the 9816 07:18:20,100 --> 07:18:22,378 initializer block is not called again 9817 07:18:22,378 --> 07:18:24,360 because we have the same instance so if 9818 07:18:24,360 --> 07:18:26,420 you can press Ctrl D here multiple times 9819 07:18:26,420 --> 07:18:28,740 and we're gonna see that we have the 9820 07:18:28,740 --> 07:18:31,440 database created with only one 9821 07:18:31,440 --> 07:18:34,740 one uh once because 9822 07:18:34,740 --> 07:18:39,000 that is called only when the 9823 07:18:39,000 --> 07:18:41,100 only when the object is created for the 9824 07:18:41,100 --> 07:18:43,138 first time so on this line in the next 9825 07:18:43,138 --> 07:18:46,200 slide we have the same uh the same 9826 07:18:46,200 --> 07:18:48,420 objects we have here this 9827 07:18:48,420 --> 07:18:50,040 this is why I have the same code because 9828 07:18:50,040 --> 07:18:52,500 it's basically the same instance so as 9829 07:18:52,500 --> 07:18:56,520 you can see this is a lot simpler and um 9830 07:18:56,520 --> 07:18:58,440 more concise than the code that had 9831 07:18:58,440 --> 07:18:59,878 previously so this is how you can create 9832 07:18:59,878 --> 07:19:03,660 a Singleton in kotlin this is why this 9833 07:19:03,660 --> 07:19:05,280 is how we should do it 9834 07:19:05,280 --> 07:19:08,600 so see in the next video 9835 07:19:09,240 --> 07:19:10,798 so now it's time to start a discussion 9836 07:19:10,798 --> 07:19:13,620 about lazy initialization but first i'm 9837 07:19:13,620 --> 07:19:15,360 going to copy this Singleton and solid 9838 07:19:15,360 --> 07:19:18,120 hour classes file 9839 07:19:18,120 --> 07:19:20,000 and if you don't have this file because 9840 07:19:20,000 --> 07:19:22,920 you created a new project again just 9841 07:19:22,920 --> 07:19:24,120 type the code that I'm going to type in 9842 07:19:24,120 --> 07:19:26,760 a second so I'm going to delete this 9843 07:19:26,760 --> 07:19:29,520 now what is lazy initialization 9844 07:19:29,520 --> 07:19:33,298 lazy initialization is used when 9845 07:19:33,298 --> 07:19:35,878 creating an instance when creating an 9846 07:19:35,878 --> 07:19:38,700 object is expensive and the expensive in 9847 07:19:38,700 --> 07:19:40,260 the programming context means that it 9848 07:19:40,260 --> 07:19:41,700 will take some time 9849 07:19:41,700 --> 07:19:44,520 it will use some memory and in that case 9850 07:19:44,520 --> 07:19:46,440 it's recommended to use lazy 9851 07:19:46,440 --> 07:19:49,260 initialization and to illustrate the 9852 07:19:49,260 --> 07:19:51,660 lazy initialization I'm going to use our 9853 07:19:51,660 --> 07:19:53,520 user class so I'm going to compare user 9854 07:19:53,520 --> 07:19:55,558 class inside order 9855 07:19:55,558 --> 07:19:57,900 main file not because I can create here 9856 07:19:57,900 --> 07:20:01,200 an object by having the file here but 9857 07:20:01,200 --> 07:20:02,660 because 9858 07:20:02,660 --> 07:20:04,680 I want to 9859 07:20:04,680 --> 07:20:07,020 put the code here to make clear what I'm 9860 07:20:07,020 --> 07:20:09,000 doing so I'm gonna delete the code from 9861 07:20:09,000 --> 07:20:11,040 here 9862 07:20:11,040 --> 07:20:12,540 and if you don't have the file just 9863 07:20:12,540 --> 07:20:15,600 create a new class called user Define 9864 07:20:15,600 --> 07:20:17,940 the properties for its name last name 9865 07:20:17,940 --> 07:20:19,978 and age I'm gonna delete this because 9866 07:20:19,978 --> 07:20:21,660 you don't need this 9867 07:20:21,660 --> 07:20:24,180 and inside our user class I'm going to 9868 07:20:24,180 --> 07:20:25,978 put initialize an initializer block 9869 07:20:25,978 --> 07:20:27,840 which is going to be called every time 9870 07:20:27,840 --> 07:20:29,700 you create a new object and here we're 9871 07:20:29,700 --> 07:20:32,340 gonna put a printer printer print line 9872 07:20:32,340 --> 07:20:34,080 statement 9873 07:20:34,080 --> 07:20:37,280 which is gonna say 9874 07:20:37,500 --> 07:20:39,360 user 9875 07:20:39,360 --> 07:20:42,180 when I call the first name property so 9876 07:20:42,180 --> 07:20:43,740 the value that Sparks the first name 9877 07:20:43,740 --> 07:20:46,160 property 9878 07:20:50,820 --> 07:20:53,820 was created 9879 07:20:53,820 --> 07:20:57,200 so let's create a new object here 9880 07:20:57,360 --> 07:21:00,120 let's call it user1 9881 07:21:00,120 --> 07:21:01,978 equals user 9882 07:21:01,978 --> 07:21:04,200 now let's put some values to the 9883 07:21:04,200 --> 07:21:05,940 properties of the Constructor let's say 9884 07:21:05,940 --> 07:21:08,239 Alex 9885 07:21:14,218 --> 07:21:17,780 now if you run the squad 9886 07:21:25,260 --> 07:21:28,138 we get in the output user and the value 9887 07:21:28,138 --> 07:21:30,660 for the first name property Alex was 9888 07:21:30,660 --> 07:21:31,500 created 9889 07:21:31,500 --> 07:21:33,478 if I press Ctrl d 9890 07:21:33,478 --> 07:21:37,218 and I change this to user 2. 9891 07:21:37,978 --> 07:21:39,600 and let's change this to a generic name 9892 07:21:39,600 --> 07:21:41,638 user 9893 07:21:41,638 --> 07:21:44,700 for the last nameless 9894 07:21:44,700 --> 07:21:48,840 pass just the last name text 9895 07:21:48,840 --> 07:21:51,298 and let's skip that age so if I run this 9896 07:21:51,298 --> 07:21:52,860 code now 9897 07:21:52,860 --> 07:21:55,860 because now on the line 3 you create a 9898 07:21:55,860 --> 07:21:57,900 new object the initializer block is 9899 07:21:57,900 --> 07:21:59,160 going to be called again because we 9900 07:21:59,160 --> 07:22:02,458 create a new object which is user 9901 07:22:02,458 --> 07:22:04,978 user was created 9902 07:22:04,978 --> 07:22:07,680 now to initialize our user 2 with the 9903 07:22:07,680 --> 07:22:11,400 lazy initializer we just delete this 9904 07:22:11,400 --> 07:22:13,558 and to illustrate how the initializer 9905 07:22:13,558 --> 07:22:16,320 block works we delete this and we put 9906 07:22:16,320 --> 07:22:19,500 after user to buy so we put the keyword 9907 07:22:19,500 --> 07:22:21,000 by 9908 07:22:21,000 --> 07:22:24,240 and we type lazy and as I'm typing you 9909 07:22:24,240 --> 07:22:26,400 see that I have that black lace press 9910 07:22:26,400 --> 07:22:30,240 enter and put the curly braces and 9911 07:22:30,240 --> 07:22:31,920 inside the calibration we create our 9912 07:22:31,920 --> 07:22:35,218 object so user 9913 07:22:35,218 --> 07:22:37,200 and here to pass the values to the 9914 07:22:37,200 --> 07:22:40,020 properties so let's put 9915 07:22:40,020 --> 07:22:42,920 user one 9916 07:22:46,138 --> 07:22:50,120 let's pass last name 9917 07:22:51,360 --> 07:22:53,700 and for the age let's put 9918 07:22:53,700 --> 07:22:55,740 zero 9919 07:22:55,740 --> 07:22:57,478 now 9920 07:22:57,478 --> 07:22:59,700 previously when the 9921 07:22:59,700 --> 07:23:02,820 user was initialized 9922 07:23:02,820 --> 07:23:04,798 in the world way 9923 07:23:04,798 --> 07:23:07,138 I found out the user user was created so 9924 07:23:07,138 --> 07:23:09,120 the initializer block was called when he 9925 07:23:09,120 --> 07:23:11,100 created the second level but now 9926 07:23:11,100 --> 07:23:13,740 if I run this code 9927 07:23:13,740 --> 07:23:16,580 look what happens 9928 07:23:18,420 --> 07:23:21,478 we get only our first object we get the 9929 07:23:21,478 --> 07:23:23,160 initializer black only called for our 9930 07:23:23,160 --> 07:23:25,760 first object and for our second object 9931 07:23:25,760 --> 07:23:28,700 initialize using the lazy initialization 9932 07:23:28,700 --> 07:23:31,378 we don't have the initialize of law 9933 07:23:31,378 --> 07:23:34,620 because the object is not initialized is 9934 07:23:34,620 --> 07:23:36,420 the object is only going to be 9935 07:23:36,420 --> 07:23:37,798 initialized 9936 07:23:37,798 --> 07:23:41,100 when you use that um 9937 07:23:41,100 --> 07:23:43,860 object in your code so if you use that 9938 07:23:43,860 --> 07:23:46,440 object somewhere in a code the object 9939 07:23:46,440 --> 07:23:48,718 then is going to be initialized and it's 9940 07:23:48,718 --> 07:23:50,520 going to be used so if I type here a 9941 07:23:50,520 --> 07:23:51,958 println 9942 07:23:51,958 --> 07:23:53,700 which says 9943 07:23:53,700 --> 07:23:59,058 user DOT first name user tool 9944 07:24:03,478 --> 07:24:05,700 now if I run this 9945 07:24:05,700 --> 07:24:08,340 now because I know I'm I am using the 9946 07:24:08,340 --> 07:24:11,160 user 2 object now the user to object 9947 07:24:11,160 --> 07:24:13,200 instance is going to be created and then 9948 07:24:13,200 --> 07:24:15,958 I'm gonna see user 2. first name so now 9949 07:24:15,958 --> 07:24:18,660 we see user let's see user 1 was created 9950 07:24:18,660 --> 07:24:21,540 so the initializer block was called 9951 07:24:21,540 --> 07:24:24,600 because when we called the 9952 07:24:24,600 --> 07:24:26,840 the print line user to that first name 9953 07:24:26,840 --> 07:24:30,840 then in that at that moment it created 9954 07:24:30,840 --> 07:24:33,180 the instance and then it outputted the 9955 07:24:33,180 --> 07:24:36,000 user one so this is why now we see here 9956 07:24:36,000 --> 07:24:38,700 user one one was created and previously 9957 07:24:38,700 --> 07:24:40,378 we didn't because 9958 07:24:40,378 --> 07:24:44,280 now we're using the user to object in 9959 07:24:44,280 --> 07:24:48,478 our code so unless you are not using the 9960 07:24:48,478 --> 07:24:51,120 the instance the object that have 9961 07:24:51,120 --> 07:24:54,000 created using the lazy initializer it is 9962 07:24:54,000 --> 07:24:56,760 not going to be initialized so this is 9963 07:24:56,760 --> 07:24:59,580 initializer this is initialization by 9964 07:24:59,580 --> 07:25:00,718 lazy 9965 07:25:00,718 --> 07:25:02,820 so see in the next video 9966 07:25:02,820 --> 07:25:04,500 so now it's time to start a discussion 9967 07:25:04,500 --> 07:25:06,660 about enum classes but first i'm going 9968 07:25:06,660 --> 07:25:08,340 to copy our user class inside our 9969 07:25:08,340 --> 07:25:11,180 classes file 9970 07:25:12,780 --> 07:25:15,240 now I'm going to delete this class and 9971 07:25:15,240 --> 07:25:16,740 also I'm going to delete the code inside 9972 07:25:16,740 --> 07:25:18,540 the main function 9973 07:25:18,540 --> 07:25:21,120 now what are the num classes 9974 07:25:21,120 --> 07:25:24,240 in on classes are used when we want to 9975 07:25:24,240 --> 07:25:27,540 represent a fixed set of values so when 9976 07:25:27,540 --> 07:25:29,760 you want to represent we want to create 9977 07:25:29,760 --> 07:25:31,860 some constants you should use genomes 9978 07:25:31,860 --> 07:25:32,820 because 9979 07:25:32,820 --> 07:25:35,340 enums are like constants but they are 9980 07:25:35,340 --> 07:25:37,440 more powerful because you can they can 9981 07:25:37,440 --> 07:25:40,020 have properties and they can also have 9982 07:25:40,020 --> 07:25:41,638 functions 9983 07:25:41,638 --> 07:25:43,500 and you know I'm sorry 9984 07:25:43,500 --> 07:25:48,000 usually used in um if conditioners or if 9985 07:25:48,000 --> 07:25:50,700 statements or in in the when statement 9986 07:25:50,700 --> 07:25:54,058 or if I want to call it the if statement 9987 07:25:54,058 --> 07:25:56,280 expression or when statement expression 9988 07:25:56,280 --> 07:25:57,840 now let's see how I can create any 9989 07:25:57,840 --> 07:26:00,240 number class 9990 07:26:00,240 --> 07:26:02,340 and to create any num class just type 9991 07:26:02,340 --> 07:26:05,160 here enum the keyword enum 9992 07:26:05,160 --> 07:26:07,020 and as I'm typing I have some 9993 07:26:07,020 --> 07:26:08,940 suggestions 9994 07:26:08,940 --> 07:26:12,540 then we put class and let's call this uh 9995 07:26:12,540 --> 07:26:14,940 you know class directions 9996 07:26:14,940 --> 07:26:17,280 Direction actually 9997 07:26:17,280 --> 07:26:19,378 then you put curly braces 9998 07:26:19,378 --> 07:26:22,620 and inside the enumclass direction we 9999 07:26:22,620 --> 07:26:25,620 Define our enum values so here we Define 10000 07:26:25,620 --> 07:26:27,420 North 10001 07:26:27,420 --> 07:26:30,058 put comma enter 10002 07:26:30,058 --> 07:26:31,260 South 10003 07:26:31,260 --> 07:26:33,420 you can put them in one line just to 10004 07:26:33,420 --> 07:26:36,120 have comma between them 10005 07:26:36,120 --> 07:26:38,780 East 10006 07:26:39,840 --> 07:26:42,240 and west 10007 07:26:42,240 --> 07:26:44,638 right so we have our 10008 07:26:44,638 --> 07:26:47,458 enums values defined inside our 10009 07:26:47,458 --> 07:26:51,058 Direction enum class and those enums 10010 07:26:51,058 --> 07:26:53,160 values that we have here here they are 10011 07:26:53,160 --> 07:26:55,740 instances of our enum 10012 07:26:55,740 --> 07:26:58,138 class Direction so they are an instance 10013 07:26:58,138 --> 07:27:00,478 there an object created inside the 10014 07:27:00,478 --> 07:27:03,360 enumclass direction just that they have 10015 07:27:03,360 --> 07:27:05,580 this different name and as you can see 10016 07:27:05,580 --> 07:27:07,440 the name of the you know values should 10017 07:27:07,440 --> 07:27:09,600 be all the names should be in uppercase 10018 07:27:09,600 --> 07:27:11,580 letters now let's see how we can access 10019 07:27:11,580 --> 07:27:13,558 those you know values 10020 07:27:13,558 --> 07:27:16,138 to access the you know values we can 10021 07:27:16,138 --> 07:27:19,458 let's put the printer line here 10022 07:27:23,100 --> 07:27:25,500 we type Direction 10023 07:27:25,500 --> 07:27:27,420 then we press Dot 10024 07:27:27,420 --> 07:27:29,700 and we have some some suggestions here 10025 07:27:29,700 --> 07:27:32,100 we have west east north and south so we 10026 07:27:32,100 --> 07:27:35,160 have our enum values suggested so let's 10027 07:27:35,160 --> 07:27:37,978 choose West let's output this in the 10028 07:27:37,978 --> 07:27:40,280 console 10029 07:27:42,660 --> 07:27:45,298 so we get West so this is the 10030 07:27:45,298 --> 07:27:46,620 enum value 10031 07:27:46,620 --> 07:27:49,138 West and um 10032 07:27:49,138 --> 07:27:51,900 let's press Ctrl d 10033 07:27:51,900 --> 07:27:55,160 let's change this to East 10034 07:27:56,878 --> 07:28:01,280 so let's change this to East 10035 07:28:03,180 --> 07:28:06,500 let's change this to North 10036 07:28:08,100 --> 07:28:10,760 and South 10037 07:28:11,218 --> 07:28:14,458 now if you run this code 10038 07:28:14,458 --> 07:28:17,400 we get our enum values West is north and 10039 07:28:17,400 --> 07:28:18,360 south 10040 07:28:18,360 --> 07:28:20,900 so this is how you can get the you know 10041 07:28:20,900 --> 07:28:23,878 the how you can get the genome values 10042 07:28:23,878 --> 07:28:25,440 that you define inside your genome class 10043 07:28:25,440 --> 07:28:27,780 now we can also 10044 07:28:27,780 --> 07:28:29,820 you can also pass 10045 07:28:29,820 --> 07:28:31,920 you can define a Constructor here for 10046 07:28:31,920 --> 07:28:34,320 our enumclass and Define some properties 10047 07:28:34,320 --> 07:28:39,420 like VAR let's call it Direction 10048 07:28:41,820 --> 07:28:45,079 of type string 10049 07:28:46,320 --> 07:28:49,138 and VAR 10050 07:28:49,138 --> 07:28:51,298 distance 10051 07:28:51,298 --> 07:28:54,739 and this is going to be an integer 10052 07:28:55,860 --> 07:28:57,780 now if you hover over here you have some 10053 07:28:57,780 --> 07:28:59,760 underlines and the underlines if you 10054 07:28:59,760 --> 07:29:01,558 have always says that you Nam has not 10055 07:29:01,558 --> 07:29:04,378 the full Constructor so now because as I 10056 07:29:04,378 --> 07:29:06,958 said those elements values are instances 10057 07:29:06,958 --> 07:29:09,058 they are objects created inside the 10058 07:29:09,058 --> 07:29:11,458 original class now we need to pass some 10059 07:29:11,458 --> 07:29:13,020 values to 10060 07:29:13,020 --> 07:29:15,298 the properties that we Define here so 10061 07:29:15,298 --> 07:29:16,798 let's pass some values here so let's 10062 07:29:16,798 --> 07:29:19,558 press let's type here first 10063 07:29:19,558 --> 07:29:22,320 North again I'm gonna speed up this now 10064 07:29:22,320 --> 07:29:24,000 a little bit 10065 07:29:24,000 --> 07:29:26,100 all right so now what we did here we 10066 07:29:26,100 --> 07:29:28,200 passed some values to the 10067 07:29:28,200 --> 07:29:30,780 you know values Constructors because in 10068 07:29:30,780 --> 07:29:34,740 in up here in our enumclass direction we 10069 07:29:34,740 --> 07:29:36,958 Define some properties and those 10070 07:29:36,958 --> 07:29:40,260 properties because the enum values are 10071 07:29:40,260 --> 07:29:42,478 objects they are instances for a number 10072 07:29:42,478 --> 07:29:44,580 directions even though they have those 10073 07:29:44,580 --> 07:29:47,280 different names you need to pass to the 10074 07:29:47,280 --> 07:29:49,440 constructors then some value so we pass 10075 07:29:49,440 --> 07:29:51,540 here north south east west and password 10076 07:29:51,540 --> 07:29:54,718 for for the densest distance 10 20 50 10077 07:29:54,718 --> 07:29:56,340 and 40. 10078 07:29:56,340 --> 07:29:57,780 now 10079 07:29:57,780 --> 07:29:59,760 let's see how we can Loop through our 10080 07:29:59,760 --> 07:30:05,280 enum's values and to do that we just 10081 07:30:05,280 --> 07:30:07,138 delete this code 10082 07:30:07,138 --> 07:30:09,840 and what if and here we add the four 10083 07:30:09,840 --> 07:30:12,600 groups so we type four 10084 07:30:12,600 --> 07:30:15,478 let's call it the 10085 07:30:15,478 --> 07:30:17,458 Direction 10086 07:30:17,458 --> 07:30:20,520 in and here we type 10087 07:30:20,520 --> 07:30:23,580 Direction our enorm class 10088 07:30:23,580 --> 07:30:26,700 dot values and this is going to return 10089 07:30:26,700 --> 07:30:29,340 an array with all the enum values that 10090 07:30:29,340 --> 07:30:30,900 are defined in our class 10091 07:30:30,900 --> 07:30:33,600 now let's put calibrases 10092 07:30:33,600 --> 07:30:36,920 and let's draw the printerland 10093 07:30:37,320 --> 07:30:39,780 and let's put in the direction 10094 07:30:39,780 --> 07:30:41,700 so now if you run this 10095 07:30:41,700 --> 07:30:44,958 you'll see the same output 10096 07:30:47,280 --> 07:30:49,680 but in the order that they are defined 10097 07:30:49,680 --> 07:30:52,940 inside the class 10098 07:30:54,180 --> 07:30:57,718 so we see the same not the same output 10099 07:30:57,718 --> 07:30:59,520 because they are outputted in the order 10100 07:30:59,520 --> 07:31:00,840 in which they are defined in the class 10101 07:31:00,840 --> 07:31:03,660 so we have north south east and west and 10102 07:31:03,660 --> 07:31:06,298 this is how we can Loop through an enum 10103 07:31:06,298 --> 07:31:09,058 to through the enum values of a num 10104 07:31:09,058 --> 07:31:12,478 class so this is how you do it so now 10105 07:31:12,478 --> 07:31:15,378 let's delete this code 10106 07:31:15,900 --> 07:31:18,120 now if you want to access the arguments 10107 07:31:18,120 --> 07:31:20,700 that are passed to our enum values 10108 07:31:20,700 --> 07:31:22,020 Constructors 10109 07:31:22,020 --> 07:31:25,320 we just first put a printerland to 10110 07:31:25,320 --> 07:31:28,440 Output the values on the console 10111 07:31:28,440 --> 07:31:31,558 and type Direction so our rename class 10112 07:31:31,558 --> 07:31:33,120 Dot 10113 07:31:33,120 --> 07:31:35,700 and we choose our in our value let's 10114 07:31:35,700 --> 07:31:38,820 choose North and from here and here now 10115 07:31:38,820 --> 07:31:41,580 we type also that and now we have some 10116 07:31:41,580 --> 07:31:44,218 suggestions we have Direction distance 10117 07:31:44,218 --> 07:31:47,638 and we also have a name 10118 07:31:47,638 --> 07:31:49,500 and name is uh 10119 07:31:49,500 --> 07:31:52,920 is a variable which is not defined by us 10120 07:31:52,920 --> 07:31:55,620 so we've only defined those properties 10121 07:31:55,620 --> 07:31:58,458 direction and distance but name is 10122 07:31:58,458 --> 07:32:00,718 variable which is built in in the 10123 07:32:00,718 --> 07:32:05,820 enumclass similar to the the the 10124 07:32:05,820 --> 07:32:08,040 direction that values which return the 10125 07:32:08,040 --> 07:32:11,820 array is the the direction that values 10126 07:32:11,820 --> 07:32:15,840 values was also uh property which 10127 07:32:15,840 --> 07:32:18,420 actually it was an array property which 10128 07:32:18,420 --> 07:32:20,940 was built into the enumclass so we 10129 07:32:20,940 --> 07:32:22,680 didn't create that it's provided by the 10130 07:32:22,680 --> 07:32:25,080 enumclass so this is why we have that 10131 07:32:25,080 --> 07:32:27,958 name there so let's put Direction 10132 07:32:27,958 --> 07:32:30,620 let's press Ctrl d 10133 07:32:30,620 --> 07:32:34,080 uplicate this code and let's change this 10134 07:32:34,080 --> 07:32:36,240 to the instance 10135 07:32:36,240 --> 07:32:38,400 let's press Ctrl D again and let's also 10136 07:32:38,400 --> 07:32:40,860 print that name 10137 07:32:40,860 --> 07:32:44,280 which is the built-in variable which is 10138 07:32:44,280 --> 07:32:46,500 the built-in variable provided by the NM 10139 07:32:46,500 --> 07:32:49,440 class so let's put here name 10140 07:32:49,440 --> 07:32:52,699 now if we run this code 10141 07:32:54,478 --> 07:32:57,000 get North so this is the value that you 10142 07:32:57,000 --> 07:32:58,920 pass here we get 10 and then we get 10143 07:32:58,920 --> 07:33:02,600 North with capital letter so 10144 07:33:02,820 --> 07:33:04,940 so we get the value that we passed here 10145 07:33:04,940 --> 07:33:07,860 for direction and distance which are 10146 07:33:07,860 --> 07:33:10,080 defined in Direction class so we get the 10147 07:33:10,080 --> 07:33:12,478 value spots for this specific 10148 07:33:12,478 --> 07:33:15,718 you know more volume and then we get the 10149 07:33:15,718 --> 07:33:18,540 North and this is the built-in um 10150 07:33:18,540 --> 07:33:21,718 variable which is uh provided by you by 10151 07:33:21,718 --> 07:33:22,920 Dynam class 10152 07:33:22,920 --> 07:33:24,360 but as I said you can also have 10153 07:33:24,360 --> 07:33:26,400 functions so let's have a function which 10154 07:33:26,400 --> 07:33:29,340 Returns the direction and distance so 10155 07:33:29,340 --> 07:33:31,620 let's type here fun 10156 07:33:31,620 --> 07:33:33,660 we call it print data because it's gonna 10157 07:33:33,660 --> 07:33:35,878 print the data of a specific in our 10158 07:33:35,878 --> 07:33:37,680 value 10159 07:33:37,680 --> 07:33:40,020 it's not going to take any input put 10160 07:33:40,020 --> 07:33:41,638 curly braces 10161 07:33:41,638 --> 07:33:44,100 but now we have an error here that is 10162 07:33:44,100 --> 07:33:46,620 because uh when you declare a function 10163 07:33:46,620 --> 07:33:48,600 inside the nenum class you need to 10164 07:33:48,600 --> 07:33:52,138 provide a semicolon for the last in um 10165 07:33:52,138 --> 07:33:53,520 defined in our class so I need to put 10166 07:33:53,520 --> 07:33:55,860 here a semicolon and there will 10167 07:33:55,860 --> 07:33:58,020 disappear and let's print the data here 10168 07:33:58,020 --> 07:34:01,100 let's add the println 10169 07:34:03,000 --> 07:34:06,478 let's put quotation marks 10170 07:34:06,478 --> 07:34:09,440 as for Direction 10171 07:34:11,580 --> 07:34:13,440 equals 10172 07:34:13,440 --> 07:34:16,138 dollar sign 10173 07:34:16,138 --> 07:34:18,620 Direction 10174 07:34:20,700 --> 07:34:23,298 and 10175 07:34:25,978 --> 07:34:27,540 distance 10176 07:34:27,540 --> 07:34:29,458 equals dollar sign 10177 07:34:29,458 --> 07:34:32,057 distance 10178 07:34:33,120 --> 07:34:35,520 now if you call this wanna 10179 07:34:35,520 --> 07:34:36,958 our 10180 07:34:36,958 --> 07:34:41,520 wanna enam valueless type direction that 10181 07:34:41,520 --> 07:34:43,680 let's say West 10182 07:34:43,680 --> 07:34:46,620 Dot and now we have our function print 10183 07:34:46,620 --> 07:34:48,298 data 10184 07:34:48,298 --> 07:34:51,240 so this print data function is going to 10185 07:34:51,240 --> 07:34:54,900 put in the data of the West enum that is 10186 07:34:54,900 --> 07:34:56,100 defined here so it's going to Output 10187 07:34:56,100 --> 07:34:58,440 West and 40 in the console so you know 10188 07:34:58,440 --> 07:35:01,217 if you run this code 10189 07:35:05,638 --> 07:35:09,900 we get direction West and distance 40. 10190 07:35:09,900 --> 07:35:11,760 so this is how you can use functions 10191 07:35:11,760 --> 07:35:14,958 inside the noun classes 10192 07:35:15,120 --> 07:35:18,058 next let's see how we can use our enum 10193 07:35:18,058 --> 07:35:19,680 Direction and generally how we can use 10194 07:35:19,680 --> 07:35:22,500 enums with the when statement expression 10195 07:35:22,500 --> 07:35:25,200 so I'm going to close the console 10196 07:35:25,200 --> 07:35:26,820 I'm going to delete this code and the 10197 07:35:26,820 --> 07:35:30,000 print data function works for all of our 10198 07:35:30,000 --> 07:35:31,860 Renown values 10199 07:35:31,860 --> 07:35:34,798 so let's delete this 10200 07:35:34,798 --> 07:35:37,080 and here I'm going to Define a variable 10201 07:35:37,080 --> 07:35:38,700 it's going to be a Val it's going to be 10202 07:35:38,700 --> 07:35:40,138 called Direction 10203 07:35:40,138 --> 07:35:42,000 we're going to put equals and hero type 10204 07:35:42,000 --> 07:35:46,558 Direction our enumclass let's choose 10205 07:35:46,558 --> 07:35:48,298 East 10206 07:35:48,298 --> 07:35:51,660 so now the enum 10207 07:35:51,660 --> 07:35:54,900 actually is the west east so now our 10208 07:35:54,900 --> 07:35:57,840 Direction enum is is going to be stored 10209 07:35:57,840 --> 07:36:00,240 in our Direction variable let's use the 10210 07:36:00,240 --> 07:36:03,240 when statement expression 10211 07:36:03,240 --> 07:36:05,638 and we type here when 10212 07:36:05,638 --> 07:36:07,500 Direction 10213 07:36:07,500 --> 07:36:09,958 then you put curly braces and here we 10214 07:36:09,958 --> 07:36:11,760 type 10215 07:36:11,760 --> 07:36:13,680 Direction 10216 07:36:13,680 --> 07:36:16,520 Dot 10217 07:36:16,798 --> 07:36:21,020 East I'm gonna put 10218 07:36:21,900 --> 07:36:23,340 Auto 10219 07:36:23,340 --> 07:36:28,040 execute this course so print the Len 10220 07:36:31,440 --> 07:36:35,340 the direction is 10221 07:36:35,340 --> 07:36:37,440 yes and we'll do the same thing for the 10222 07:36:37,440 --> 07:36:40,558 next one so let's press Ctrl d 10223 07:36:40,558 --> 07:36:42,600 let's change this to 10224 07:36:42,600 --> 07:36:45,199 West 10225 07:36:51,900 --> 07:36:54,440 North 10226 07:36:56,878 --> 07:36:59,458 and South 10227 07:36:59,458 --> 07:37:02,218 so this is how you can use the enorms 10228 07:37:02,218 --> 07:37:06,958 with the one statement now if I run this 10229 07:37:06,958 --> 07:37:09,478 actually I should change the text there 10230 07:37:09,478 --> 07:37:11,520 but I'm going to change it 10231 07:37:11,520 --> 07:37:13,798 so let's close this to change the text 10232 07:37:13,798 --> 07:37:16,039 here 10233 07:37:16,740 --> 07:37:19,138 now if you run this you're gonna see 10234 07:37:19,138 --> 07:37:21,780 the direction it sees because this 10235 07:37:21,780 --> 07:37:24,500 condition is true 10236 07:37:24,840 --> 07:37:27,000 our Direction argument here is going to 10237 07:37:27,000 --> 07:37:30,120 match with this Branch so it's going to 10238 07:37:30,120 --> 07:37:31,620 evaluate true and then it's going to 10239 07:37:31,620 --> 07:37:32,820 execute this code you can pull 10240 07:37:32,820 --> 07:37:35,760 calibrations if you want but as I said 10241 07:37:35,760 --> 07:37:37,320 in our discussion about the when 10242 07:37:37,320 --> 07:37:39,000 statement expression 10243 07:37:39,000 --> 07:37:41,160 you should do scary braces if you have 10244 07:37:41,160 --> 07:37:43,440 multiple lines of code in our case we 10245 07:37:43,440 --> 07:37:45,240 have one single line of code but you can 10246 07:37:45,240 --> 07:37:47,400 put curly braces if you want and of 10247 07:37:47,400 --> 07:37:48,900 course that this Branch will always 10248 07:37:48,900 --> 07:37:51,058 evaluate to true because here we type 10249 07:37:51,058 --> 07:37:52,558 direction that is 10250 07:37:52,558 --> 07:37:55,138 into our Direction variable literally 10251 07:37:55,138 --> 07:37:57,478 directly and this value doesn't change 10252 07:37:57,478 --> 07:37:59,400 but in real up this value could come 10253 07:37:59,400 --> 07:38:01,320 from a database or from the user input 10254 07:38:01,320 --> 07:38:02,878 and in that case you need to use the 10255 07:38:02,878 --> 07:38:05,458 function which is also built into the 10256 07:38:05,458 --> 07:38:09,478 enorma class and that function 10257 07:38:09,478 --> 07:38:11,638 is called value of so we type Direction 10258 07:38:11,638 --> 07:38:15,840 dot value of and this value function 10259 07:38:15,840 --> 07:38:18,600 check his checks to see if our value 10260 07:38:18,600 --> 07:38:21,440 that we pass here exists inside our 10261 07:38:21,440 --> 07:38:24,000 enumclass so it checks to see if is 10262 07:38:24,000 --> 07:38:27,058 defined here and it all it also returns 10263 07:38:27,058 --> 07:38:30,900 that value so if I type here 10264 07:38:30,900 --> 07:38:32,340 yeast 10265 07:38:32,340 --> 07:38:35,540 now if I run this code 10266 07:38:37,798 --> 07:38:39,600 we get an error which has no num 10267 07:38:39,600 --> 07:38:42,120 constant direction that is because 10268 07:38:42,120 --> 07:38:45,180 all of our enum values have they are 10269 07:38:45,180 --> 07:38:47,280 defined in uppercase letters 10270 07:38:47,280 --> 07:38:49,680 but if I put here that 10271 07:38:49,680 --> 07:38:52,558 uppercase 10272 07:38:52,558 --> 07:38:56,000 and if I run this code 10273 07:38:58,798 --> 07:39:02,700 now we get the direction assist and uh 10274 07:39:02,700 --> 07:39:04,920 this value that we have here is this 10275 07:39:04,920 --> 07:39:06,840 value as I said could come from a 10276 07:39:06,840 --> 07:39:09,540 database or from the user input and the 10277 07:39:09,540 --> 07:39:11,878 direction that value of is going to 10278 07:39:11,878 --> 07:39:14,458 check to see if this 10279 07:39:14,458 --> 07:39:16,320 you know value exists in our rename 10280 07:39:16,320 --> 07:39:17,638 class and it's going to return that 10281 07:39:17,638 --> 07:39:18,780 value 10282 07:39:18,780 --> 07:39:21,780 in our variable so it's going to assign 10283 07:39:21,780 --> 07:39:23,280 that value to our variable 10284 07:39:23,280 --> 07:39:26,840 so see you in the next video 10285 07:39:27,420 --> 07:39:29,040 so now it's time to start a discussion 10286 07:39:29,040 --> 07:39:31,320 about inner classes so what are inner 10287 07:39:31,320 --> 07:39:33,660 classes in our classes are classes which 10288 07:39:33,660 --> 07:39:35,760 are declared inside another class and 10289 07:39:35,760 --> 07:39:37,320 they are generally used when you know 10290 07:39:37,320 --> 07:39:38,878 that there is a very close relationship 10291 07:39:38,878 --> 07:39:41,638 between two classes so when it doesn't 10292 07:39:41,638 --> 07:39:43,680 make sense to put two classes separated 10293 07:39:43,680 --> 07:39:46,378 then you should use inner classes and 10294 07:39:46,378 --> 07:39:48,420 let's look at an example and we're going 10295 07:39:48,420 --> 07:39:50,040 to imagine here that we have a list View 10296 07:39:50,040 --> 07:39:53,240 and this list video list view is gonna 10297 07:39:53,240 --> 07:39:57,900 display some items in a list in on the 10298 07:39:57,900 --> 07:40:00,900 screen and here we're going to use inner 10299 07:40:00,900 --> 07:40:03,900 classes because for displaying the each 10300 07:40:03,900 --> 07:40:06,298 individual item we're gonna create a 10301 07:40:06,298 --> 07:40:08,160 list view item so first you create a 10302 07:40:08,160 --> 07:40:11,298 class called list View 10303 07:40:20,400 --> 07:40:23,400 foreign 10304 07:40:26,580 --> 07:40:30,000 we Define a property called 10305 07:40:30,000 --> 07:40:32,520 items and it's going to be an array of 10306 07:40:32,520 --> 07:40:34,820 strings 10307 07:40:42,540 --> 07:40:45,958 now we put curly braces and inside the 10308 07:40:45,958 --> 07:40:48,660 class we type the keyword inner 10309 07:40:48,660 --> 07:40:51,600 then class and now we create a class 10310 07:40:51,600 --> 07:40:53,540 which is going to represent the 10311 07:40:53,540 --> 07:40:56,820 individual items in the list view so we 10312 07:40:56,820 --> 07:41:00,440 put type here list view item 10313 07:41:05,520 --> 07:41:07,080 put the 10314 07:41:07,080 --> 07:41:09,660 the primary Constructor but we put not 10315 07:41:09,660 --> 07:41:11,218 we don't Define any properties and then 10316 07:41:11,218 --> 07:41:13,020 we put curly braces 10317 07:41:13,020 --> 07:41:15,240 and this inner class let's use gonna 10318 07:41:15,240 --> 07:41:18,298 have a function called display 10319 07:41:18,298 --> 07:41:20,520 item and it's going to display the value 10320 07:41:20,520 --> 07:41:23,160 the values of an item of other specific 10321 07:41:23,160 --> 07:41:26,040 position so we put here position let's 10322 07:41:26,040 --> 07:41:27,840 say 10323 07:41:27,840 --> 07:41:30,298 Define a parameter to our function 10324 07:41:30,298 --> 07:41:33,260 it's going to be an end 10325 07:41:35,218 --> 07:41:37,260 and we're just gonna type here print 10326 07:41:37,260 --> 07:41:39,360 line 10327 07:41:39,360 --> 07:41:43,860 and the inner classes are have access to 10328 07:41:43,860 --> 07:41:45,780 the properties of the outer class so 10329 07:41:45,780 --> 07:41:48,298 inside our inner classes that we have 10330 07:41:48,298 --> 07:41:50,400 here we can have we can access the 10331 07:41:50,400 --> 07:41:52,080 properties that are defined inside the 10332 07:41:52,080 --> 07:41:54,240 outer class list view in this case we 10333 07:41:54,240 --> 07:41:55,920 have a single property called items and 10334 07:41:55,920 --> 07:41:57,500 here we can type 10335 07:41:57,500 --> 07:41:59,760 items and notice that I can use the 10336 07:41:59,760 --> 07:42:01,620 items property which is defined in our 10337 07:42:01,620 --> 07:42:04,020 outer class list view we type position 10338 07:42:04,020 --> 07:42:05,940 and this is going to return 10339 07:42:05,940 --> 07:42:08,520 an item at this position which is passed 10340 07:42:08,520 --> 07:42:10,920 as an argument here to to do our 10341 07:42:10,920 --> 07:42:12,478 parameter position 10342 07:42:12,478 --> 07:42:16,680 and we go up here and we type Val 10343 07:42:16,680 --> 07:42:18,718 list View 10344 07:42:18,718 --> 07:42:19,798 because we're going to create an 10345 07:42:19,798 --> 07:42:22,878 instance for this View 10346 07:42:23,400 --> 07:42:26,360 type list View 10347 07:42:30,840 --> 07:42:32,718 now we need to pass an array to our 10348 07:42:32,718 --> 07:42:35,580 primary Constructor to the property item 10349 07:42:35,580 --> 07:42:38,580 so we type here array off and we Define 10350 07:42:38,580 --> 07:42:40,920 some generic names here let's say uh 10351 07:42:40,920 --> 07:42:42,360 name 10352 07:42:42,360 --> 07:42:45,020 one 10353 07:42:47,458 --> 07:42:51,298 and name four now how can we access the 10354 07:42:51,298 --> 07:42:53,340 display item function which is inside 10355 07:42:53,340 --> 07:42:56,400 our inner class 10356 07:42:56,400 --> 07:42:59,878 list item list view items this this 10357 07:42:59,878 --> 07:43:02,700 should be list view item not items so 10358 07:43:02,700 --> 07:43:04,920 let's change this go right click on it 10359 07:43:04,920 --> 07:43:07,080 and go to refactor and rename 10360 07:43:07,080 --> 07:43:11,000 let's call it the items 10361 07:43:16,680 --> 07:43:19,280 press enter 10362 07:43:19,500 --> 07:43:21,958 now to access the display item function 10363 07:43:21,958 --> 07:43:23,580 which is inside the original class list 10364 07:43:23,580 --> 07:43:26,240 view item 10365 07:43:27,298 --> 07:43:29,940 we type list View 10366 07:43:29,940 --> 07:43:34,080 so our instance our object list view Dot 10367 07:43:34,080 --> 07:43:37,740 list view item and then again that and 10368 07:43:37,740 --> 07:43:39,360 now we have our function display item 10369 07:43:39,360 --> 07:43:44,540 and we can pass here let's say yo 10370 07:43:44,878 --> 07:43:48,000 let's say uh two 10371 07:43:48,000 --> 07:43:51,260 now if you run this code 10372 07:43:52,500 --> 07:43:55,500 so get in the output name tree and this 10373 07:43:55,500 --> 07:43:57,478 is correct because it entries the other 10374 07:43:57,478 --> 07:43:59,280 index too 10375 07:43:59,280 --> 07:44:01,260 and this is how you can access the 10376 07:44:01,260 --> 07:44:03,058 display how this how you can access 10377 07:44:03,058 --> 07:44:05,820 generally a function inside then uh in 10378 07:44:05,820 --> 07:44:08,700 inside an inner class you just create an 10379 07:44:08,700 --> 07:44:10,740 instance of the outer class then you 10380 07:44:10,740 --> 07:44:11,700 call a 10381 07:44:11,700 --> 07:44:13,920 you call the name of the class of the 10382 07:44:13,920 --> 07:44:15,420 name of the inner class and then you 10383 07:44:15,420 --> 07:44:19,138 call the function Auto so see you in the 10384 07:44:19,138 --> 07:44:20,218 next video 10385 07:44:20,218 --> 07:44:22,080 so now it's time to do a challenge using 10386 07:44:22,080 --> 07:44:23,580 the knowledge that we have about object 10387 07:44:23,580 --> 07:44:25,200 oriented programming 10388 07:44:25,200 --> 07:44:26,940 and the challenge is to create a class 10389 07:44:26,940 --> 07:44:28,378 which is going to represent a bank 10390 07:44:28,378 --> 07:44:32,760 account of a person and this class is 10391 07:44:32,760 --> 07:44:34,740 going to have three properties the first 10392 07:44:34,740 --> 07:44:36,420 one is going to be called the account 10393 07:44:36,420 --> 07:44:38,280 name the second one is going to be 10394 07:44:38,280 --> 07:44:41,520 called the balance and the third one is 10395 07:44:41,520 --> 07:44:43,798 going to be called transactions and it 10396 07:44:43,798 --> 07:44:46,080 should be a multiple list 10397 07:44:46,080 --> 07:44:49,400 also you should create functions for 10398 07:44:49,400 --> 07:44:52,978 deposit and withdraw and also function 10399 07:44:52,978 --> 07:44:55,500 for calculating the balance and you also 10400 07:44:55,500 --> 07:44:58,280 you also should add the 10401 07:44:58,280 --> 07:45:00,958 the checks for checking to see if the 10402 07:45:00,958 --> 07:45:04,620 amount is uh when you deposit the amount 10403 07:45:04,620 --> 07:45:06,600 is greater than zero and all of that 10404 07:45:06,600 --> 07:45:09,540 stuff so do this Challenge and then 10405 07:45:09,540 --> 07:45:12,240 watch my solution 10406 07:45:12,240 --> 07:45:13,978 so 10407 07:45:13,978 --> 07:45:16,440 my solution is this I'm going to create 10408 07:45:16,440 --> 07:45:18,120 the class 10409 07:45:18,120 --> 07:45:20,638 and for this challenge you should also 10410 07:45:20,638 --> 07:45:22,680 have in mind the discussion that you had 10411 07:45:22,680 --> 07:45:26,458 about access modifiers so I'm gonna call 10412 07:45:26,458 --> 07:45:28,940 it account 10413 07:45:29,878 --> 07:45:31,978 and I'm going to Define here a property 10414 07:45:31,978 --> 07:45:33,840 called the 10415 07:45:33,840 --> 07:45:36,058 we're gonna be called the Val is going 10416 07:45:36,058 --> 07:45:37,978 to be called the count uh 10417 07:45:37,978 --> 07:45:40,020 name 10418 07:45:40,020 --> 07:45:43,638 and it's going to be of type string 10419 07:45:47,520 --> 07:45:50,660 of type string 10420 07:45:50,878 --> 07:45:53,580 and we put curly braces and inside the 10421 07:45:53,580 --> 07:45:56,820 class I'm gonna declare the next two 10422 07:45:56,820 --> 07:45:59,760 properties and those one uh first I'm 10423 07:45:59,760 --> 07:46:00,958 going to declare it without the access 10424 07:46:00,958 --> 07:46:03,180 modifiers because IntelliJ probably is 10425 07:46:03,180 --> 07:46:05,878 gonna figure out that we need to put 10426 07:46:05,878 --> 07:46:07,440 access modifiers on them so I'm gonna 10427 07:46:07,440 --> 07:46:11,160 type VAR balance 10428 07:46:11,160 --> 07:46:14,760 and I'm gonna assign 0 so this is the 10429 07:46:14,760 --> 07:46:17,820 default value and 10430 07:46:17,820 --> 07:46:21,780 VAR transactions 10431 07:46:26,878 --> 07:46:29,160 and this is going to be equals to a 10432 07:46:29,160 --> 07:46:31,638 multiple 10433 07:46:33,780 --> 07:46:36,798 or Multiple List 10434 07:46:38,458 --> 07:46:41,340 of int because this this is going to 10435 07:46:41,340 --> 07:46:43,860 store the transactions 10436 07:46:43,860 --> 07:46:46,138 so these are the three properties of our 10437 07:46:46,138 --> 07:46:48,718 class now I'm gonna create the first 10438 07:46:48,718 --> 07:46:50,160 function which is going to be called 10439 07:46:50,160 --> 07:46:51,840 deposit and this function is going to be 10440 07:46:51,840 --> 07:46:54,120 used to deposit 10441 07:46:54,120 --> 07:46:56,638 money inside our bank account and it's 10442 07:46:56,638 --> 07:46:58,378 going to have a parameter called the 10443 07:46:58,378 --> 07:47:00,120 mount 10444 07:47:00,120 --> 07:47:02,878 it's going to be of type 10445 07:47:02,878 --> 07:47:05,420 end 10446 07:47:05,820 --> 07:47:07,558 and first we need to check if this 10447 07:47:07,558 --> 07:47:09,420 amount is greater than zero because you 10448 07:47:09,420 --> 07:47:12,240 don't want to deposit uh negative value 10449 07:47:12,240 --> 07:47:15,180 so if amount is greater than zero 10450 07:47:15,180 --> 07:47:17,218 and we choose greater than zero because 10451 07:47:17,218 --> 07:47:20,520 we also don't want to deposit zero so 10452 07:47:20,520 --> 07:47:22,680 this is why I put greater than zero then 10453 07:47:22,680 --> 07:47:24,360 if it is greater than zero then I'm 10454 07:47:24,360 --> 07:47:26,900 gonna add that to our transaction 10455 07:47:26,900 --> 07:47:29,760 list that add 10456 07:47:29,760 --> 07:47:31,558 amount 10457 07:47:31,558 --> 07:47:35,400 and then I'm gonna say balance 10458 07:47:35,400 --> 07:47:37,500 I'm gonna update our balance I'm gonna 10459 07:47:37,500 --> 07:47:39,360 put plus equals 10460 07:47:39,360 --> 07:47:41,878 amount then we're gonna add the print 10461 07:47:41,878 --> 07:47:44,700 line which is gonna say 10462 07:47:44,700 --> 07:47:46,798 I'm going to put the quotation marks 10463 07:47:46,798 --> 07:47:48,540 dollar sign 10464 07:47:48,540 --> 07:47:51,798 I'm gonna type amount 10465 07:47:52,740 --> 07:47:55,920 I'm gonna put dollar sign this dot 10466 07:47:55,920 --> 07:47:57,360 balance 10467 07:47:57,360 --> 07:47:59,100 so I'm gonna output in the console what 10468 07:47:59,100 --> 07:48:01,860 is the balance but if the amount that is 10469 07:48:01,860 --> 07:48:04,378 passed to this function is 10470 07:48:04,378 --> 07:48:07,138 a negative sum I'm gonna go in the else 10471 07:48:07,138 --> 07:48:08,940 part and we're gonna say 10472 07:48:08,940 --> 07:48:11,040 another print line here 10473 07:48:11,040 --> 07:48:12,900 which is going to say cannot deposit 10474 07:48:12,900 --> 07:48:15,540 negative sums so we're gonna put here 10475 07:48:15,540 --> 07:48:18,298 okay cannot 10476 07:48:18,298 --> 07:48:21,298 deposit 10477 07:48:21,620 --> 07:48:24,958 negative sums 10478 07:48:24,958 --> 07:48:27,900 so this is our deposit function next I'm 10479 07:48:27,900 --> 07:48:30,058 going to create another function and 10480 07:48:30,058 --> 07:48:32,458 this is going to be called withdraw and 10481 07:48:32,458 --> 07:48:34,260 this is going to be used for withdraw 10482 07:48:34,260 --> 07:48:36,120 withdrawing mine so we type fun with 10483 07:48:36,120 --> 07:48:38,539 draw 10484 07:48:38,760 --> 07:48:41,700 and we put parenthesis and this is gonna 10485 07:48:41,700 --> 07:48:45,320 take a parameter called with draw wool 10486 07:48:45,320 --> 07:48:48,620 so uh 10487 07:48:50,280 --> 07:48:52,740 it should be like this 10488 07:48:52,740 --> 07:48:55,080 and it's going to be an integer we put 10489 07:48:55,080 --> 07:48:56,760 curly braces 10490 07:48:56,760 --> 07:48:59,359 now 10491 07:48:59,458 --> 07:49:01,138 it should be with 10492 07:49:01,138 --> 07:49:04,558 with here with 10493 07:49:04,558 --> 07:49:06,240 with 10494 07:49:06,240 --> 07:49:08,940 draw 10495 07:49:08,940 --> 07:49:10,620 now we need to convert this into 10496 07:49:10,620 --> 07:49:12,600 negative sum because now we're uh 10497 07:49:12,600 --> 07:49:15,240 withdrawing money from autocon so first 10498 07:49:15,240 --> 07:49:18,000 we need to check if 10499 07:49:18,000 --> 07:49:20,760 and we're going to put minus because I'm 10500 07:49:20,760 --> 07:49:22,860 gonna convert this into a negative sum 10501 07:49:22,860 --> 07:49:25,620 withdrawal is less than zero 10502 07:49:25,620 --> 07:49:28,080 so if this is a negative 10503 07:49:28,080 --> 07:49:30,958 sum which is going to be withdrawal 10504 07:49:30,958 --> 07:49:33,500 you're gonna 10505 07:49:34,920 --> 07:49:37,558 I'm gonna add to our transactions is 10506 07:49:37,558 --> 07:49:39,660 transactions that add and you also put 10507 07:49:39,660 --> 07:49:41,940 the minus sign so you put minus with 10508 07:49:41,940 --> 07:49:44,280 drawal because now we're adding now 10509 07:49:44,280 --> 07:49:47,458 we're uh we're taking money off the of 10510 07:49:47,458 --> 07:49:49,620 our account then I'm gonna put this that 10511 07:49:49,620 --> 07:49:52,160 balance 10512 07:49:53,820 --> 07:49:57,440 my uh Plus 10513 07:49:57,440 --> 07:50:01,080 equals minus 10514 07:50:01,080 --> 07:50:03,298 withdrawal because now we're taking 10515 07:50:03,298 --> 07:50:04,320 money 10516 07:50:04,320 --> 07:50:06,120 and then we're going to add again a 10517 07:50:06,120 --> 07:50:08,520 print line it's going to be this one so 10518 07:50:08,520 --> 07:50:11,420 I'm going to copy this 10519 07:50:14,940 --> 07:50:16,860 but now this is going to be the 10520 07:50:16,860 --> 07:50:19,760 withdrawal 10521 07:50:22,740 --> 07:50:25,378 else if 10522 07:50:25,378 --> 07:50:27,360 this is not 10523 07:50:27,360 --> 07:50:30,120 a negative sum so 10524 07:50:30,120 --> 07:50:33,200 I'm gonna put else 10525 07:50:35,458 --> 07:50:39,440 I'm gonna type put in the line 10526 07:50:42,298 --> 07:50:45,000 cannot 10527 07:50:45,000 --> 07:50:47,780 wait 10528 07:50:47,820 --> 07:50:50,360 draw 10529 07:50:51,180 --> 07:50:55,740 negative sums so if he tries to withdraw 10530 07:50:55,740 --> 07:50:57,180 a negative sum then we're going to say 10531 07:50:57,180 --> 07:50:59,160 cannot withdraw a negative sum here we 10532 07:50:59,160 --> 07:51:01,860 put the minus because we we're taking 10533 07:51:01,860 --> 07:51:04,500 money out of our account so we need to 10534 07:51:04,500 --> 07:51:06,058 check for uh that's why we're checking 10535 07:51:06,058 --> 07:51:09,440 here if this is less than zero 10536 07:51:15,500 --> 07:51:18,360 shouldn't be negative here 10537 07:51:18,360 --> 07:51:19,680 and we're going to create another 10538 07:51:19,680 --> 07:51:21,478 function and this is going to be called 10539 07:51:21,478 --> 07:51:25,320 calculate balance so we type here fun 10540 07:51:25,320 --> 07:51:27,058 Let's uh 10541 07:51:27,058 --> 07:51:29,160 bring this 10542 07:51:29,160 --> 07:51:32,218 fun calculate 10543 07:51:32,218 --> 07:51:34,138 calculate 10544 07:51:34,138 --> 07:51:36,860 balance 10545 07:51:39,780 --> 07:51:41,638 this is not going to take any parameters 10546 07:51:41,638 --> 07:51:45,138 and it's going to return an INT 10547 07:51:46,860 --> 07:51:50,718 put curly braces 10548 07:51:52,860 --> 07:51:56,160 and I'm gonna put this that balance to 10549 07:51:56,160 --> 07:51:57,600 zero because 10550 07:51:57,600 --> 07:51:59,340 I want to calculate the balance I'm 10551 07:51:59,340 --> 07:52:01,760 going to add the transactions to this 10552 07:52:01,760 --> 07:52:04,440 property balance so here we're gonna say 10553 07:52:04,440 --> 07:52:06,478 and here we're gonna loop we're gonna 10554 07:52:06,478 --> 07:52:08,760 type four 10555 07:52:08,760 --> 07:52:12,120 turn suction 10556 07:52:12,120 --> 07:52:16,978 in our list of transactions 10557 07:52:16,978 --> 07:52:18,478 so 10558 07:52:18,478 --> 07:52:20,398 for transaction in our list of 10559 07:52:20,398 --> 07:52:23,820 transaction I'm gonna type this Dot 10560 07:52:23,820 --> 07:52:25,620 balance 10561 07:52:25,620 --> 07:52:29,398 plus and equals transaction 10562 07:52:29,398 --> 07:52:31,740 so our transaction and then we're going 10563 07:52:31,740 --> 07:52:34,320 to return the balance at the end of our 10564 07:52:34,320 --> 07:52:37,378 Loop when we end we ended looping so 10565 07:52:37,378 --> 07:52:40,558 return this Dot 10566 07:52:40,558 --> 07:52:43,920 balance so this is our solution to the 10567 07:52:43,920 --> 07:52:46,740 challenge and let's um let's create an 10568 07:52:46,740 --> 07:52:48,360 account and let's pass some values here 10569 07:52:48,360 --> 07:52:50,160 so I'm going to type here 10570 07:52:50,160 --> 07:52:51,600 Val 10571 07:52:51,600 --> 07:52:54,739 Alex account 10572 07:52:55,080 --> 07:52:57,298 I'm gonna put equals 10573 07:52:57,298 --> 07:52:59,958 account 10574 07:53:00,000 --> 07:53:01,978 I'm gonna put now I'm gonna call the 10575 07:53:01,978 --> 07:53:03,360 primary Constructor so I'm going to type 10576 07:53:03,360 --> 07:53:05,840 here Alex 10577 07:53:06,958 --> 07:53:10,138 and we still use I'm still using my name 10578 07:53:10,138 --> 07:53:11,638 here but 10579 07:53:11,638 --> 07:53:13,080 so I'm going to type here Alex account 10580 07:53:13,080 --> 07:53:15,660 Dot 10581 07:53:15,660 --> 07:53:17,638 deposit so I'm going to deposit let's 10582 07:53:17,638 --> 07:53:20,218 say one thousand 10583 07:53:20,218 --> 07:53:22,620 and uh 10584 07:53:22,620 --> 07:53:26,458 I'm gonna type OLX account that withdraw 10585 07:53:26,458 --> 07:53:29,040 I'm gonna withdraw 10586 07:53:29,040 --> 07:53:33,120 500 then I'm gonna put Alex account 10587 07:53:33,120 --> 07:53:35,458 and I'm gonna put uh 10588 07:53:35,458 --> 07:53:38,760 deposit the negative sign let's say 10589 07:53:38,760 --> 07:53:41,520 minus 20. 10590 07:53:41,520 --> 07:53:45,478 and Alex account Dot 10591 07:53:45,478 --> 07:53:48,180 that we draw also negative sign let's 10592 07:53:48,180 --> 07:53:50,478 say minus 10593 07:53:50,478 --> 07:53:52,378 100. 10594 07:53:52,378 --> 07:53:55,940 now if you're on this code 10595 07:53:56,218 --> 07:53:58,398 foreign 10596 07:54:06,320 --> 07:54:09,540 deposit balance is now 1000 so we have 10597 07:54:09,540 --> 07:54:12,420 our call card called from our deposit 10598 07:54:12,420 --> 07:54:13,440 function 10599 07:54:13,440 --> 07:54:16,280 then we have 10600 07:54:19,680 --> 07:54:22,500 so I should I didn't change the text 10601 07:54:22,500 --> 07:54:25,558 here so that's why it's saying uh 10602 07:54:25,558 --> 07:54:30,958 this should say with uh withdrawn so uh 10603 07:54:30,958 --> 07:54:33,240 sorry let's change this to 10604 07:54:33,240 --> 07:54:34,620 with 10605 07:54:34,620 --> 07:54:37,397 drone 10606 07:54:38,940 --> 07:54:42,680 so let's run our code again 10607 07:54:48,058 --> 07:54:50,700 so get 1000 deposit balance is now one 10608 07:54:50,700 --> 07:54:53,760 thousand now we get our uh correct 10609 07:54:53,760 --> 07:54:55,740 output to get the 10610 07:54:55,740 --> 07:54:59,580 then 500 withdrawn so get our call the 10611 07:54:59,580 --> 07:55:02,100 call from our withdrawn function we 10612 07:55:02,100 --> 07:55:05,340 withdrawn balance is now uh 500 then 10613 07:55:05,340 --> 07:55:07,138 then you get cannot deposit negative 10614 07:55:07,138 --> 07:55:09,420 sums cannot withdraw negative sounds 10615 07:55:09,420 --> 07:55:11,700 because we passed the negative numbers 10616 07:55:11,700 --> 07:55:13,558 to our deposits and withdrawal function 10617 07:55:13,558 --> 07:55:16,978 and here we're checking to see if those 10618 07:55:16,978 --> 07:55:20,638 are negative or positive so this is our 10619 07:55:20,638 --> 07:55:24,180 solution to our 10620 07:55:24,180 --> 07:55:26,280 to our Challenge and I hope that you 10621 07:55:26,280 --> 07:55:28,740 enjoyed this and that you solved this 10622 07:55:28,740 --> 07:55:32,820 and this is not again the best solution 10623 07:55:32,820 --> 07:55:34,260 this is just 10624 07:55:34,260 --> 07:55:36,478 my solution probably you'll find you 10625 07:55:36,478 --> 07:55:38,718 figure out a better solution 10626 07:55:38,718 --> 07:55:42,120 maybe your concern better and more 10627 07:55:42,120 --> 07:55:44,458 concise solution than this actually I 10628 07:55:44,458 --> 07:55:46,080 forgot before we end our video Let's 10629 07:55:46,080 --> 07:55:48,298 actually called also our calculate 10630 07:55:48,298 --> 07:55:51,600 balance function so I'm gonna type here 10631 07:55:51,600 --> 07:55:53,100 uh 10632 07:55:53,100 --> 07:55:56,000 Alex account 10633 07:55:56,160 --> 07:55:58,440 dot calculate balance and this is going 10634 07:55:58,440 --> 07:56:00,540 to return an integer so I'm gonna type 10635 07:56:00,540 --> 07:56:03,240 here Val because that function returns 10636 07:56:03,240 --> 07:56:04,798 something so I need to capture that in a 10637 07:56:04,798 --> 07:56:06,478 variable so I'm going to type here valve 10638 07:56:06,478 --> 07:56:08,040 balance 10639 07:56:08,040 --> 07:56:10,740 equals Alex account 10640 07:56:10,740 --> 07:56:14,600 so no other print line here 10641 07:56:15,180 --> 07:56:17,638 and there's gonna say 10642 07:56:17,638 --> 07:56:19,860 balance 10643 07:56:19,860 --> 07:56:22,740 is dollar sign 10644 07:56:22,740 --> 07:56:25,398 balance 10645 07:56:25,398 --> 07:56:27,420 format the code 10646 07:56:27,420 --> 07:56:30,978 now if we run this code 10647 07:56:33,120 --> 07:56:36,250 [Music] 10648 07:56:36,260 --> 07:56:40,080 now get our previous output to 1000 10649 07:56:40,080 --> 07:56:42,478 deposit balance now one thousand five 10650 07:56:42,478 --> 07:56:45,780 hundred withdrawn balance is now 500 10651 07:56:45,780 --> 07:56:47,760 then cannot deposit negative sums and 10652 07:56:47,760 --> 07:56:49,320 cannot withdraw negative sounds and then 10653 07:56:49,320 --> 07:56:52,798 get balances now 500 so our code works 10654 07:56:52,798 --> 07:56:53,940 well 10655 07:56:53,940 --> 07:56:57,058 so this is our code 10656 07:56:57,058 --> 07:56:59,458 one more thing that I forgot is that I 10657 07:56:59,458 --> 07:57:00,780 said at the beginning of the video that 10658 07:57:00,780 --> 07:57:02,700 you should have in mind the discussion 10659 07:57:02,700 --> 07:57:04,978 that we had about access modifiers when 10660 07:57:04,978 --> 07:57:06,420 doing this challenge 10661 07:57:06,420 --> 07:57:09,600 and I forgot to to add the access 10662 07:57:09,600 --> 07:57:11,700 modifiers for balance and transactions 10663 07:57:11,700 --> 07:57:14,520 because now what can I do for balance I 10664 07:57:14,520 --> 07:57:16,620 can 10665 07:57:16,620 --> 07:57:20,239 use our Alexa account 10666 07:57:20,760 --> 07:57:23,638 s and I can type here balance and now 10667 07:57:23,638 --> 07:57:26,280 because that is uh actually that is not 10668 07:57:26,280 --> 07:57:28,080 really probably because when I talked 10669 07:57:28,080 --> 07:57:31,138 about uh get us unsetters we said that 10670 07:57:31,138 --> 07:57:33,120 that when you access a property you 10671 07:57:33,120 --> 07:57:34,740 always access access that property 10672 07:57:34,740 --> 07:57:36,360 through get a run Center so we never 10673 07:57:36,360 --> 07:57:40,920 access directly the getter and the 10674 07:57:40,920 --> 07:57:42,780 property but uh 10675 07:57:42,780 --> 07:57:46,620 still you should not make that that 10676 07:57:46,620 --> 07:57:49,320 public because by default if you don't 10677 07:57:49,320 --> 07:57:51,718 provide an access modifier they are they 10678 07:57:51,718 --> 07:57:54,780 are by default public so if I type here 10679 07:57:54,780 --> 07:57:57,600 now account balance as you can see I can 10680 07:57:57,600 --> 07:58:00,958 call that and I can type here 1000 and I 10681 07:58:00,958 --> 07:58:03,478 can deposit now I can change the balance 10682 07:58:03,478 --> 07:58:06,240 of the account directly from here and if 10683 07:58:06,240 --> 07:58:07,860 you hover over here 10684 07:58:07,860 --> 07:58:11,100 uh IntelliJ figure out that you should 10685 07:58:11,100 --> 07:58:13,740 make this private so make this private 10686 07:58:13,740 --> 07:58:16,620 the balance and also the transactions 10687 07:58:16,620 --> 07:58:18,360 because 10688 07:58:18,360 --> 07:58:20,458 you don't use them inside the primary 10689 07:58:20,458 --> 07:58:22,200 Constructor and they are used only 10690 07:58:22,200 --> 07:58:24,360 through the to the functions that we 10691 07:58:24,360 --> 07:58:27,058 defined here the because of that you 10692 07:58:27,058 --> 07:58:29,218 should you should declare them as 10693 07:58:29,218 --> 07:58:31,500 private because you use them only 10694 07:58:31,500 --> 07:58:33,780 through the function that you declared 10695 07:58:33,780 --> 07:58:36,360 inside the class so declare them as 10696 07:58:36,360 --> 07:58:38,760 private and now you if you hover over 10697 07:58:38,760 --> 07:58:40,920 here it says you cannot access balance 10698 07:58:40,920 --> 07:58:42,958 because it's private in account and now 10699 07:58:42,958 --> 07:58:44,398 we cannot change the balance from 10700 07:58:44,398 --> 07:58:46,398 outside the class we can only 10701 07:58:46,398 --> 07:58:50,120 deposit or withdraw uh 10702 07:58:50,120 --> 07:58:53,760 sums of money from our bank account 10703 07:58:53,760 --> 07:58:56,280 by interacting only with the functions 10704 07:58:56,280 --> 07:58:59,100 and not by calling the property directly 10705 07:58:59,100 --> 07:59:02,520 and assigning a new balance to to the 10706 07:59:02,520 --> 07:59:05,520 property so make them private and this 10707 07:59:05,520 --> 07:59:09,958 way you cannot uh you don't uh you don't 10708 07:59:09,958 --> 07:59:13,260 access the property outside of the class 10709 07:59:13,260 --> 07:59:15,478 and you change data directly the the 10710 07:59:15,478 --> 07:59:17,218 value of the property again it's not 10711 07:59:17,218 --> 07:59:19,920 really direct because the code that you 10712 07:59:19,920 --> 07:59:22,138 had previously so this code actually is 10713 07:59:22,138 --> 07:59:24,420 calling the setter here but still you 10714 07:59:24,420 --> 07:59:26,700 can change the value of 10715 07:59:26,700 --> 07:59:28,860 the balance but now because we made that 10716 07:59:28,860 --> 07:59:33,058 private and the the compiler is smart 10717 07:59:33,058 --> 07:59:35,520 enough to figure out that because they 10718 07:59:35,520 --> 07:59:37,200 are not declared inside the primary 10719 07:59:37,200 --> 07:59:39,898 Concepts and they are only used you only 10720 07:59:39,898 --> 07:59:42,780 interact with them with the deposit and 10721 07:59:42,780 --> 07:59:43,920 withdraw 10722 07:59:43,920 --> 07:59:46,920 and uh calculate balance functions so we 10723 07:59:46,920 --> 07:59:48,840 don't use them outside you don't have 10724 07:59:48,840 --> 07:59:50,280 them also declaring the primary 10725 07:59:50,280 --> 07:59:52,200 Constructor it knows that you should 10726 07:59:52,200 --> 07:59:54,000 make them private because you interact 10727 07:59:54,000 --> 07:59:56,160 them only with the functions and now we 10728 07:59:56,160 --> 07:59:58,260 have this underline because it's private 10729 07:59:58,260 --> 07:59:59,940 so we cannot change this balance now 10730 07:59:59,940 --> 08:00:02,458 from outside the class we can only 10731 08:00:02,458 --> 08:00:05,218 interact them again using the functions 10732 08:00:05,218 --> 08:00:08,280 so I'm gonna delete this and 10733 08:00:08,280 --> 08:00:10,860 now see you in the next video 10734 08:00:10,860 --> 08:00:12,478 so let's delete this 10735 08:00:12,478 --> 08:00:13,978 so actually I'm gonna let it there 10736 08:00:13,978 --> 08:00:15,120 because 10737 08:00:15,120 --> 08:00:18,120 it's uh good to see so now it's time to 10738 08:00:18,120 --> 08:00:20,218 start a discussion about inheritance so 10739 08:00:20,218 --> 08:00:23,100 what is inheritance inheritance is the 10740 08:00:23,100 --> 08:00:24,898 concept in the quarterly programming 10741 08:00:24,898 --> 08:00:27,058 context with which you can create a 10742 08:00:27,058 --> 08:00:29,040 class using another class so in other 10743 08:00:29,040 --> 08:00:31,138 words you can get all the functions and 10744 08:00:31,138 --> 08:00:32,878 properties that have that the class 10745 08:00:32,878 --> 08:00:35,700 already has in your new class and in 10746 08:00:35,700 --> 08:00:37,440 addition to the getting all the 10747 08:00:37,440 --> 08:00:40,080 functions in properties in uh in your 10748 08:00:40,080 --> 08:00:42,958 new class you can add some new functions 10749 08:00:42,958 --> 08:00:44,760 some new properties which are specific 10750 08:00:44,760 --> 08:00:46,680 to this new class but they aren't for 10751 08:00:46,680 --> 08:00:49,138 the also this the class from which you 10752 08:00:49,138 --> 08:00:51,180 are inheriting is called The Base Class 10753 08:00:51,180 --> 08:00:53,700 the parent class or the super class 10754 08:00:53,700 --> 08:00:56,340 and you might be wondering why is this 10755 08:00:56,340 --> 08:00:58,978 useful why you want to do this 10756 08:00:58,978 --> 08:01:01,680 let's say that you have a very big class 10757 08:01:01,680 --> 08:01:05,580 which performs very hard task and has a 10758 08:01:05,580 --> 08:01:07,740 lot of functions in coding but you want 10759 08:01:07,740 --> 08:01:10,620 to create a new class which is slightly 10760 08:01:10,620 --> 08:01:13,080 different from this class what you can 10761 08:01:13,080 --> 08:01:15,420 do is you can get all that code copied 10762 08:01:15,420 --> 08:01:17,458 in the new class and then you can add 10763 08:01:17,458 --> 08:01:19,620 your new functions or new your new 10764 08:01:19,620 --> 08:01:23,718 Behavior to this class but dust is not 10765 08:01:23,718 --> 08:01:25,860 recommended because you have duplicate 10766 08:01:25,860 --> 08:01:28,500 code what you can do instead is you can 10767 08:01:28,500 --> 08:01:30,540 inherit all the functions and properties 10768 08:01:30,540 --> 08:01:33,660 which this big class already has and in 10769 08:01:33,660 --> 08:01:35,940 your new class you can add the new 10770 08:01:35,940 --> 08:01:38,280 functionality the new Behavior which is 10771 08:01:38,280 --> 08:01:40,138 specific to this new class but it's not 10772 08:01:40,138 --> 08:01:42,240 for the base class so in this way you 10773 08:01:42,240 --> 08:01:45,898 can uh you you avoid the duplicated code 10774 08:01:45,898 --> 08:01:47,940 and you increase the code reusability 10775 08:01:47,940 --> 08:01:50,340 and for that I'm going to show you in an 10776 08:01:50,340 --> 08:01:52,020 example and I'm going to create two 10777 08:01:52,020 --> 08:01:54,420 classes one is going to be called car 10778 08:01:54,420 --> 08:01:57,298 and one is going to be plain so we 10779 08:01:57,298 --> 08:02:00,420 create here a class we go down here 10780 08:02:00,420 --> 08:02:02,100 we type class 10781 08:02:02,100 --> 08:02:04,200 I'm going to call it car 10782 08:02:04,200 --> 08:02:06,120 is going to have some properties the 10783 08:02:06,120 --> 08:02:07,260 first one is going to be called name 10784 08:02:07,260 --> 08:02:08,760 this is going to be the brand name of 10785 08:02:08,760 --> 08:02:11,057 the car 10786 08:02:11,160 --> 08:02:13,378 the second one is going to be color also 10787 08:02:13,378 --> 08:02:15,860 string 10788 08:02:20,580 --> 08:02:23,820 engines the number of engines that this 10789 08:02:23,820 --> 08:02:25,138 car has 10790 08:02:25,138 --> 08:02:27,000 gonna be an integer 10791 08:02:27,000 --> 08:02:28,978 and Val 10792 08:02:28,978 --> 08:02:31,740 this is going to be 10793 08:02:31,740 --> 08:02:34,138 doors so we're gonna store the number of 10794 08:02:34,138 --> 08:02:35,340 doors 10795 08:02:35,340 --> 08:02:36,718 all right 10796 08:02:36,718 --> 08:02:38,398 we put curly braces because I'm gonna 10797 08:02:38,398 --> 08:02:41,218 add some functions to this class 10798 08:02:41,218 --> 08:02:44,280 to function actually fun 10799 08:02:44,280 --> 08:02:46,878 move 10800 08:02:48,840 --> 08:02:51,740 and uh 10801 08:02:52,860 --> 08:02:54,600 fun 10802 08:02:54,600 --> 08:02:56,897 stop 10803 08:02:59,100 --> 08:03:02,120 and here you're gonna say 10804 08:03:03,718 --> 08:03:06,620 print line 10805 08:03:08,280 --> 08:03:11,398 quotation marks dollar sign 10806 08:03:11,398 --> 08:03:13,500 the name of the car 10807 08:03:13,500 --> 08:03:16,500 is moving 10808 08:03:16,500 --> 08:03:18,360 similarly 10809 08:03:18,360 --> 08:03:20,218 down here in the stop we're going to say 10810 08:03:20,218 --> 08:03:23,240 the car has stopped 10811 08:03:23,340 --> 08:03:25,820 car 10812 08:03:25,920 --> 08:03:28,458 has 10813 08:03:31,020 --> 08:03:33,058 and we're gonna create a another class 10814 08:03:33,058 --> 08:03:36,020 called plane 10815 08:03:37,920 --> 08:03:40,320 which is going to have also Val 10816 08:03:40,320 --> 08:03:42,660 name 10817 08:03:42,660 --> 08:03:46,580 we're gonna be a string a vowel color 10818 08:03:46,580 --> 08:03:48,540 also string 10819 08:03:48,540 --> 08:03:52,978 Val engines an integer and the valve 10820 08:03:52,978 --> 08:03:55,080 doors 10821 08:03:55,080 --> 08:03:57,740 unint 10822 08:03:58,440 --> 08:04:00,600 put curly braces and we Define those two 10823 08:04:00,600 --> 08:04:02,340 function we're going to copy those 10824 08:04:02,340 --> 08:04:04,500 I'm gonna paste it here 10825 08:04:04,500 --> 08:04:06,958 so now what we did here is that we 10826 08:04:06,958 --> 08:04:09,240 created two classes 10827 08:04:09,240 --> 08:04:11,520 they both both have 10828 08:04:11,520 --> 08:04:13,620 uh properties for the primary 10829 08:04:13,620 --> 08:04:16,558 Constructors and but as you probably 10830 08:04:16,558 --> 08:04:18,898 already noticed we have the same 10831 08:04:18,898 --> 08:04:21,058 properties we have the name here we have 10832 08:04:21,058 --> 08:04:22,558 the name there we have the color there 10833 08:04:22,558 --> 08:04:24,000 we have the color here the color there 10834 08:04:24,000 --> 08:04:26,520 the engines also we have here and there 10835 08:04:26,520 --> 08:04:29,100 and we also have those functions which 10836 08:04:29,100 --> 08:04:32,700 are almost actually are the same so we 10837 08:04:32,700 --> 08:04:34,320 have what I said at the beginning 10838 08:04:34,320 --> 08:04:37,020 duplicated code so it doesn't make sense 10839 08:04:37,020 --> 08:04:40,260 to put this code here another 10840 08:04:40,260 --> 08:04:42,718 another way to do this is to create a 10841 08:04:42,718 --> 08:04:44,878 base class a generic class called 10842 08:04:44,878 --> 08:04:49,080 vehicle which is uh and usually 10843 08:04:49,080 --> 08:04:51,058 you create the generic class when you 10844 08:04:51,058 --> 08:04:52,200 know that 10845 08:04:52,200 --> 08:04:56,700 several classes we are gonna share those 10846 08:04:56,700 --> 08:04:59,580 properties and those behaviors so a car 10847 08:04:59,580 --> 08:05:02,218 in the plane both are Vehicles so we can 10848 08:05:02,218 --> 08:05:05,040 put here instead of declaring the the 10849 08:05:05,040 --> 08:05:08,280 name and the caller inside the our 10850 08:05:08,280 --> 08:05:10,978 inside our classes car and plane you can 10851 08:05:10,978 --> 08:05:14,700 put this in a vague vehicle uh 10852 08:05:14,700 --> 08:05:18,360 class in a base class and we can inherit 10853 08:05:18,360 --> 08:05:20,458 those properties in our classes instead 10854 08:05:20,458 --> 08:05:23,940 of declaring them your inside our car 10855 08:05:23,940 --> 08:05:26,340 and playing class so we can put here Val 10856 08:05:26,340 --> 08:05:29,340 name 10857 08:05:29,700 --> 08:05:31,740 it's going to be a string 10858 08:05:31,740 --> 08:05:35,040 and the Val 10859 08:05:35,040 --> 08:05:38,160 color also string 10860 08:05:38,160 --> 08:05:41,958 now I can copy those two 10861 08:05:42,298 --> 08:05:44,760 let's put curly braces here and I can 10862 08:05:44,760 --> 08:05:47,340 paste it paste it here so 10863 08:05:47,340 --> 08:05:51,058 now I can delete those those two 10864 08:05:51,058 --> 08:05:54,240 functions from our classes 10865 08:05:54,240 --> 08:05:57,718 and what we can do now is we can inherit 10866 08:05:57,718 --> 08:06:00,180 those properties so we can delete those 10867 08:06:00,180 --> 08:06:02,160 properties that we have here 10868 08:06:02,160 --> 08:06:04,440 we cannot we will as you'll see you'll 10869 08:06:04,440 --> 08:06:06,660 need to declare them as parameters but 10870 08:06:06,660 --> 08:06:07,978 uh 10871 08:06:07,978 --> 08:06:09,840 they are not they're not going to be as 10872 08:06:09,840 --> 08:06:12,420 properties in any they're not going to 10873 08:06:12,420 --> 08:06:13,740 be as properties here 10874 08:06:13,740 --> 08:06:15,058 now 10875 08:06:15,058 --> 08:06:17,280 what we can do is that we can inherit 10876 08:06:17,280 --> 08:06:20,638 this shared those share the functions 10877 08:06:20,638 --> 08:06:23,040 and those shared properties because both 10878 08:06:23,040 --> 08:06:25,378 the car and the plane 10879 08:06:25,378 --> 08:06:28,798 have a function to move have a function 10880 08:06:28,798 --> 08:06:31,500 to stop the baby they both share a name 10881 08:06:31,500 --> 08:06:34,020 they both share a color so they both 10882 08:06:34,020 --> 08:06:37,500 share those properties and uh functions 10883 08:06:37,500 --> 08:06:39,558 but uh 10884 08:06:39,558 --> 08:06:42,180 as we did previously is to put them 10885 08:06:42,180 --> 08:06:44,878 inside the the the classes would not 10886 08:06:44,878 --> 08:06:46,500 it's not good because we have a 10887 08:06:46,500 --> 08:06:49,260 duplicated code and in this way we put 10888 08:06:49,260 --> 08:06:50,458 this code here 10889 08:06:50,458 --> 08:06:52,740 and now we can inherit this code so we 10890 08:06:52,740 --> 08:06:56,218 can have this code directly you can you 10891 08:06:56,218 --> 08:06:59,840 can have this code the being uh 10892 08:06:59,840 --> 08:07:02,760 being accessed inside the car and plane 10893 08:07:02,760 --> 08:07:05,160 without declaring them in our classes so 10894 08:07:05,160 --> 08:07:07,558 to inherit from our class first you need 10895 08:07:07,558 --> 08:07:09,898 to Mark the class from which you want to 10896 08:07:09,898 --> 08:07:13,200 inherit as open because by default they 10897 08:07:13,200 --> 08:07:15,120 are they you cannot inherit from a class 10898 08:07:15,120 --> 08:07:19,320 so if I enter in head from a class we go 10899 08:07:19,320 --> 08:07:21,780 at the enclosing impurities of our 10900 08:07:21,780 --> 08:07:24,620 primary Constructor 10901 08:07:24,780 --> 08:07:27,420 we put colon and we type the name of the 10902 08:07:27,420 --> 08:07:29,218 class from which we want to inherit and 10903 08:07:29,218 --> 08:07:31,378 in this case it's going to be vehicle 10904 08:07:31,378 --> 08:07:32,520 now 10905 08:07:32,520 --> 08:07:34,740 if you hover over here 10906 08:07:34,740 --> 08:07:37,200 it says that this type has a Constructor 10907 08:07:37,200 --> 08:07:40,440 and thus mods B initialized here and and 10908 08:07:40,440 --> 08:07:42,478 next you say this type is final so it 10909 08:07:42,478 --> 08:07:45,120 cannot be inherited from and that means 10910 08:07:45,120 --> 08:07:46,740 that we cannot inherit from this class 10911 08:07:46,740 --> 08:07:49,860 unless we mark this class with the open 10912 08:07:49,860 --> 08:07:52,798 keyword in front of it so we need to put 10913 08:07:52,798 --> 08:07:55,320 here open 10914 08:07:55,320 --> 08:07:58,440 and now if we hover over here 10915 08:07:58,440 --> 08:08:00,718 now we don't have that error you only 10916 08:08:00,718 --> 08:08:02,398 have this here which is this type has a 10917 08:08:02,398 --> 08:08:05,100 Constructor and dust must be initialized 10918 08:08:05,100 --> 08:08:08,458 here and that basically means that it 10919 08:08:08,458 --> 08:08:11,760 says hey I'm trying to use this vehicle 10920 08:08:11,760 --> 08:08:14,820 class I'm trying to but in order to use 10921 08:08:14,820 --> 08:08:16,620 it I mean I need to initialize this 10922 08:08:16,620 --> 08:08:19,020 vehicle class so we need to pass some 10923 08:08:19,020 --> 08:08:21,298 values to the 10924 08:08:21,298 --> 08:08:23,160 construct of the primary Constructor 10925 08:08:23,160 --> 08:08:24,420 that is defined here you need to pass 10926 08:08:24,420 --> 08:08:26,638 some values to the name and to color 10927 08:08:26,638 --> 08:08:29,520 and to do that we Define here name we 10928 08:08:29,520 --> 08:08:31,620 type name 10929 08:08:31,620 --> 08:08:34,620 actually dot let's press Ctrl Z 10930 08:08:34,620 --> 08:08:38,280 let's put a space and we type name 10931 08:08:38,280 --> 08:08:40,200 we put 10932 08:08:40,200 --> 08:08:43,740 comma then we type 10933 08:08:43,740 --> 08:08:46,160 color 10934 08:08:47,100 --> 08:08:49,378 and notice that we don't have the VAR or 10935 08:08:49,378 --> 08:08:51,120 the Vault keyword in front of them that 10936 08:08:51,120 --> 08:08:54,739 is because the those 10937 08:08:54,920 --> 08:08:57,120 parameters that would have here because 10938 08:08:57,120 --> 08:08:58,440 they are parameters because they don't 10939 08:08:58,440 --> 08:09:01,200 have the value of our keyword they they 10940 08:09:01,200 --> 08:09:04,080 uh they they they have to be declared 10941 08:09:04,080 --> 08:09:05,760 only as parameters because the 10942 08:09:05,760 --> 08:09:08,218 properties are going to be declared and 10943 08:09:08,218 --> 08:09:09,780 they are declared by the primary 10944 08:09:09,780 --> 08:09:11,760 Constructor of the base class from which 10945 08:09:11,760 --> 08:09:14,660 we are inheriting from 10946 08:09:14,660 --> 08:09:18,478 and because of that we don't we we don't 10947 08:09:18,478 --> 08:09:20,100 we don't need to declare the property 10948 08:09:20,100 --> 08:09:22,200 again here as you can see if I put the 10949 08:09:22,200 --> 08:09:24,420 VAR keyword we have an error says name 10950 08:09:24,420 --> 08:09:27,600 hyzer anyway what that means that we 10951 08:09:27,600 --> 08:09:29,100 cannot have the property declared here 10952 08:09:29,100 --> 08:09:30,780 because the properties uh already 10953 08:09:30,780 --> 08:09:32,700 declared inside the Base Class inside 10954 08:09:32,700 --> 08:09:34,320 the vehicle class from which we are 10955 08:09:34,320 --> 08:09:36,958 inheriting from what 10956 08:09:36,958 --> 08:09:39,478 we can do here is Define the parameters 10957 08:09:39,478 --> 08:09:41,940 and the value that is passed to those 10958 08:09:41,940 --> 08:09:43,798 parameters so the primary Constructor of 10959 08:09:43,798 --> 08:09:46,320 the class cards are going to be passed 10960 08:09:46,320 --> 08:09:48,958 to the to the vehicle primary 10961 08:09:48,958 --> 08:09:51,000 Constructor so hero put the primary 10962 08:09:51,000 --> 08:09:52,680 Constructor so you put two parentheses 10963 08:09:52,680 --> 08:09:55,620 and we type name 10964 08:09:55,620 --> 08:09:58,920 and this name is this one from here 10965 08:09:58,920 --> 08:10:01,440 so we type name then we put comma 10966 08:10:01,440 --> 08:10:03,058 because we need to pass a value for the 10967 08:10:03,058 --> 08:10:05,040 second one for the color and the type 10968 08:10:05,040 --> 08:10:06,240 color 10969 08:10:06,240 --> 08:10:08,940 and this color is this color here 10970 08:10:08,940 --> 08:10:11,840 so we type color 10971 08:10:12,298 --> 08:10:13,740 now 10972 08:10:13,740 --> 08:10:17,458 if you go up here let's say for let's 10973 08:10:17,458 --> 08:10:20,040 look first as the actually let's also 10974 08:10:20,040 --> 08:10:24,780 inherit from inherit inherit also one in 10975 08:10:24,780 --> 08:10:27,180 on the plane class so we put colon also 10976 08:10:27,180 --> 08:10:29,340 here 10977 08:10:29,340 --> 08:10:31,620 we type vehicle 10978 08:10:31,620 --> 08:10:33,898 and now we need to also to Define here 10979 08:10:33,898 --> 08:10:37,080 the parameters the name and the color 10980 08:10:37,080 --> 08:10:40,160 so we type name 10981 08:10:40,200 --> 08:10:41,878 and at the time you can see that 10982 08:10:41,878 --> 08:10:43,680 IntelliJ has given us some hints so if I 10983 08:10:43,680 --> 08:10:45,360 type here color it says that color 10984 08:10:45,360 --> 08:10:48,180 string so it's very helpful 10985 08:10:48,180 --> 08:10:50,638 put colon we pass those values to 10986 08:10:50,638 --> 08:10:53,040 primary Constructor of the base class so 10987 08:10:53,040 --> 08:10:56,120 we type name 10988 08:10:56,878 --> 08:10:59,360 color 10989 08:11:02,398 --> 08:11:04,378 and now if I go up here on I create some 10990 08:11:04,378 --> 08:11:06,600 instances of our car and playing class 10991 08:11:06,600 --> 08:11:10,500 so we type here Val car let's put equals 10992 08:11:10,500 --> 08:11:13,520 this type card 10993 08:11:13,680 --> 08:11:18,058 let's choose for the name BMV 10994 08:11:18,298 --> 08:11:20,638 and the 10995 08:11:20,638 --> 08:11:25,280 for the color let's say red 10996 08:11:28,020 --> 08:11:31,200 for the engines one for the doors four 10997 08:11:31,200 --> 08:11:33,058 now I'm going to create also an object 10998 08:11:33,058 --> 08:11:35,580 with our plane so let's press Ctrl alt 10999 08:11:35,580 --> 08:11:37,320 form on the code 11000 08:11:37,320 --> 08:11:39,600 valve plane 11001 08:11:39,600 --> 08:11:43,320 equals plane so our class 11002 08:11:43,320 --> 08:11:45,958 we type here 11003 08:11:45,958 --> 08:11:48,798 for the name 11004 08:11:52,138 --> 08:11:55,320 for the color let's say 11005 08:11:55,320 --> 08:11:58,860 or white 11006 08:11:59,940 --> 08:12:02,040 and 11007 08:12:02,040 --> 08:12:04,580 blue 11008 08:12:05,340 --> 08:12:08,000 and 11009 08:12:08,398 --> 08:12:11,340 for the engines for 11010 08:12:11,340 --> 08:12:14,458 and for the doors let's see 11011 08:12:14,458 --> 08:12:17,218 also for 11012 08:12:17,218 --> 08:12:20,878 now if I go down here and I 11013 08:12:20,878 --> 08:12:24,420 type here car dot 11014 08:12:24,420 --> 08:12:26,280 if you look here you see that you see 11015 08:12:26,280 --> 08:12:28,978 that we have the function stop and the 11016 08:12:28,978 --> 08:12:31,500 function move but we didn't declare them 11017 08:12:31,500 --> 08:12:34,138 here you deleted them previously but 11018 08:12:34,138 --> 08:12:36,360 because we are inheriting from the 11019 08:12:36,360 --> 08:12:39,420 vehicle class because when you are in 11020 08:12:39,420 --> 08:12:43,200 editing the properties and the functions 11021 08:12:43,200 --> 08:12:44,940 from the class from which you are 11022 08:12:44,940 --> 08:12:48,298 inheriting from are part of the new 11023 08:12:48,298 --> 08:12:51,360 classes so the the the 11024 08:12:51,360 --> 08:12:53,520 move function in the stop function they 11025 08:12:53,520 --> 08:12:55,320 are now part of our color class 11026 08:12:55,320 --> 08:12:58,620 similarly it's the same in the same the 11027 08:12:58,620 --> 08:13:02,458 same here the the the the move and stop 11028 08:13:02,458 --> 08:13:05,878 are uh are part of the class plane so if 11029 08:13:05,878 --> 08:13:08,100 I type here car 11030 08:13:08,100 --> 08:13:11,057 dot move 11031 08:13:11,100 --> 08:13:16,920 and car dot stop and then plane dot move 11032 08:13:16,920 --> 08:13:19,200 and plane so look like then now I can 11033 08:13:19,200 --> 08:13:21,780 call them like they are declared inside 11034 08:13:21,780 --> 08:13:24,420 the our current plane but they are not 11035 08:13:24,420 --> 08:13:26,218 there but because we are inheriting they 11036 08:13:26,218 --> 08:13:27,958 are part of the class so you can think 11037 08:13:27,958 --> 08:13:29,280 of 11038 08:13:29,280 --> 08:13:31,320 you can think of this thing that we have 11039 08:13:31,320 --> 08:13:35,040 here as a single entity so it acts like 11040 08:13:35,040 --> 08:13:36,540 a single entity 11041 08:13:36,540 --> 08:13:39,978 so now if you run the squad 11042 08:13:43,138 --> 08:13:45,660 we get in the output band there is 11043 08:13:45,660 --> 08:13:47,398 moving then they have stopped bearing is 11044 08:13:47,398 --> 08:13:49,860 moving doing has stopped 11045 08:13:49,860 --> 08:13:51,360 and uh 11046 08:13:51,360 --> 08:13:54,000 this uh we get this output because as I 11047 08:13:54,000 --> 08:13:56,100 said those functions are part of our 11048 08:13:56,100 --> 08:13:59,580 classes and they are using the name 11049 08:13:59,580 --> 08:14:01,740 so in this case the name that is passed 11050 08:14:01,740 --> 08:14:03,600 to the car this name that we have here 11051 08:14:03,600 --> 08:14:04,860 been there 11052 08:14:04,860 --> 08:14:07,558 is passed to the to the primary 11053 08:14:07,558 --> 08:14:10,260 Constructor of the base class and then 11054 08:14:10,260 --> 08:14:12,240 it's used here so it's calling the 11055 08:14:12,240 --> 08:14:14,398 movement save and there is moving so in 11056 08:14:14,398 --> 08:14:16,920 the same way for the for the plane we 11057 08:14:16,920 --> 08:14:19,740 get the same output it gets the value 11058 08:14:19,740 --> 08:14:22,620 that is passed here viewing and then it 11059 08:14:22,620 --> 08:14:25,378 outputs the viewing is moving and then 11060 08:14:25,378 --> 08:14:26,580 it has stopped 11061 08:14:26,580 --> 08:14:29,600 but we can do even more we can 11062 08:14:29,600 --> 08:14:32,340 override those functions inside our 11063 08:14:32,340 --> 08:14:35,638 class so we can create a version which 11064 08:14:35,638 --> 08:14:37,920 is inside the base class from your 11065 08:14:37,920 --> 08:14:40,320 inheriting we can create a version of 11066 08:14:40,320 --> 08:14:42,898 this function which is specific to the 11067 08:14:42,898 --> 08:14:44,580 car or which is specific to the plane 11068 08:14:44,580 --> 08:14:46,558 and I'm gonna look at that 11069 08:14:46,558 --> 08:14:48,298 so now I'm going to override the move 11070 08:14:48,298 --> 08:14:50,218 function from our Base Class vehicle 11071 08:14:50,218 --> 08:14:52,740 inside our playing class because I want 11072 08:14:52,740 --> 08:14:54,478 to make the move function specific to 11073 08:14:54,478 --> 08:14:56,820 the playing class so I want to provide I 11074 08:14:56,820 --> 08:14:59,638 want to provide an implementation of the 11075 08:14:59,638 --> 08:15:01,558 move class which is specific to a plane 11076 08:15:01,558 --> 08:15:03,780 so how is our plane is moving a plane is 11077 08:15:03,780 --> 08:15:05,940 moving by flying so I'm not satisfied 11078 08:15:05,940 --> 08:15:07,798 with only this generic function which 11079 08:15:07,798 --> 08:15:11,458 says the name of the vehicle is moving 11080 08:15:11,458 --> 08:15:13,740 and to override the function you first 11081 08:15:13,740 --> 08:15:17,218 need to Mark Mark the function similarly 11082 08:15:17,218 --> 08:15:18,958 to 11083 08:15:18,958 --> 08:15:21,058 to the class we need to mark it with the 11084 08:15:21,058 --> 08:15:23,100 open keyword so we type open because 11085 08:15:23,100 --> 08:15:25,500 kotlin wants to make things explicit so 11086 08:15:25,500 --> 08:15:28,378 we put open and now we can 11087 08:15:28,378 --> 08:15:30,000 we can 11088 08:15:30,000 --> 08:15:31,798 we can override that function inside our 11089 08:15:31,798 --> 08:15:34,138 plane class 11090 08:15:34,138 --> 08:15:36,478 so we go to to override the function go 11091 08:15:36,478 --> 08:15:37,680 to code 11092 08:15:37,680 --> 08:15:40,200 generate and here we have vehicles in 11093 08:15:40,200 --> 08:15:42,360 harsh code tostring and go to override 11094 08:15:42,360 --> 08:15:45,240 methods and you you have those equals 11095 08:15:45,240 --> 08:15:46,798 hash code into string and those are here 11096 08:15:46,798 --> 08:15:48,298 because every class that you create in 11097 08:15:48,298 --> 08:15:51,540 Kathleen is implicitly inheriting from 11098 08:15:51,540 --> 08:15:53,340 the any class but we're gonna look at 11099 08:15:53,340 --> 08:15:56,520 that on but more more on that later 11100 08:15:56,520 --> 08:15:58,260 because you look at that in a different 11101 08:15:58,260 --> 08:16:01,378 video and we have our function move and 11102 08:16:01,378 --> 08:16:03,780 stop so 11103 08:16:03,780 --> 08:16:08,218 the the idea is telling us that those 11104 08:16:08,218 --> 08:16:09,780 are the functions which can be 11105 08:16:09,780 --> 08:16:11,878 overridden inside our plane class from 11106 08:16:11,878 --> 08:16:15,120 our vehicle Base Class so we choose move 11107 08:16:15,120 --> 08:16:17,638 we press ok 11108 08:16:17,638 --> 08:16:21,000 and you have this super dot move we're 11109 08:16:21,000 --> 08:16:23,340 gonna see middle what is this and you 11110 08:16:23,340 --> 08:16:24,898 can also override it 11111 08:16:24,898 --> 08:16:28,378 by just typing the keyword override and 11112 08:16:28,378 --> 08:16:30,420 you can override the function so you can 11113 08:16:30,420 --> 08:16:32,160 type here override 11114 08:16:32,160 --> 08:16:34,920 and while I type in you see that we have 11115 08:16:34,920 --> 08:16:37,138 this override fan move stop or and have 11116 08:16:37,138 --> 08:16:39,478 also those so we choose move 11117 08:16:39,478 --> 08:16:41,160 and I have the same thing 11118 08:16:41,160 --> 08:16:46,440 now super dot move means that call the 11119 08:16:46,440 --> 08:16:49,260 function so super means call something 11120 08:16:49,260 --> 08:16:52,200 from our base class in this case call 11121 08:16:52,200 --> 08:16:55,138 the move function from our Base Class so 11122 08:16:55,138 --> 08:16:57,360 we're gonna let that here but in 11123 08:16:57,360 --> 08:16:58,740 addition to that we're going to add a 11124 08:16:58,740 --> 08:17:00,420 new function so we're going to type here 11125 08:17:00,420 --> 08:17:02,280 fun 11126 08:17:02,280 --> 08:17:06,620 flying because the plane is flying 11127 08:17:07,020 --> 08:17:09,600 and we're just gonna have a simple print 11128 08:17:09,600 --> 08:17:11,958 line 11129 08:17:12,478 --> 08:17:14,100 which is going to say 11130 08:17:14,100 --> 08:17:17,700 put quotation marks let's bring this 11131 08:17:17,700 --> 08:17:20,420 down a little bit 11132 08:17:25,020 --> 08:17:27,798 a plane is 11133 08:17:28,920 --> 08:17:32,280 flying so now I'm gonna call this 11134 08:17:32,280 --> 08:17:35,160 function flying inside our move function 11135 08:17:35,160 --> 08:17:37,138 which was overridden 11136 08:17:37,138 --> 08:17:40,620 so we type flying 11137 08:17:40,620 --> 08:17:43,378 flying 11138 08:17:43,378 --> 08:17:46,680 now if we run this code look what 11139 08:17:46,680 --> 08:17:48,860 happens 11140 08:17:59,398 --> 08:18:01,138 if you look in the output to get when 11141 08:18:01,138 --> 08:18:02,820 they is moving when they have stopped 11142 08:18:02,820 --> 08:18:05,398 and then we get the apply the plane is 11143 08:18:05,398 --> 08:18:07,558 flying and then we get bearing is moving 11144 08:18:07,558 --> 08:18:11,100 and bearing has stopped so what what has 11145 08:18:11,100 --> 08:18:14,398 happened here now because we where when 11146 08:18:14,398 --> 08:18:16,620 this function move is called here on our 11147 08:18:16,620 --> 08:18:18,920 object plane created 11148 08:18:18,920 --> 08:18:22,320 now it's using our own implementation of 11149 08:18:22,320 --> 08:18:26,218 the move function and after that so it's 11150 08:18:26,218 --> 08:18:27,540 using our own implementation it's 11151 08:18:27,540 --> 08:18:29,040 calling the flying function that we 11152 08:18:29,040 --> 08:18:31,920 created here because a plane is moving 11153 08:18:31,920 --> 08:18:34,138 in a specific way is flying and then 11154 08:18:34,138 --> 08:18:36,058 it's calling super dot move and super 11155 08:18:36,058 --> 08:18:39,958 dot move is calling the function from 11156 08:18:39,958 --> 08:18:41,458 the base class so it's calling the 11157 08:18:41,458 --> 08:18:44,580 function from here so 11158 08:18:44,580 --> 08:18:46,860 by doing this we made our function 11159 08:18:46,860 --> 08:18:49,860 specific you make we make the we made 11160 08:18:49,860 --> 08:18:52,320 the function move specific make the 11161 08:18:52,320 --> 08:18:55,500 function move unique to the plane class 11162 08:18:55,500 --> 08:18:57,718 because a plane is moving in a specific 11163 08:18:57,718 --> 08:19:00,360 way and we can do the same thing for the 11164 08:19:00,360 --> 08:19:03,138 the car class so 11165 08:19:03,138 --> 08:19:07,020 uh now you may be wondering why why I 11166 08:19:07,020 --> 08:19:10,200 didn't declare the inside our Base Class 11167 08:19:10,200 --> 08:19:11,760 the engines because they are also 11168 08:19:11,760 --> 08:19:15,420 present in in both classes because when 11169 08:19:15,420 --> 08:19:18,080 you create a generic class you should 11170 08:19:18,080 --> 08:19:22,340 only include the commonly used functions 11171 08:19:22,340 --> 08:19:25,200 commonly used and shared functions and 11172 08:19:25,200 --> 08:19:28,620 the properties so uh 11173 08:19:28,620 --> 08:19:31,440 all all vehicles have a name and the 11174 08:19:31,440 --> 08:19:33,780 color but not all vehicles have engines 11175 08:19:33,780 --> 08:19:37,260 so this is why I put the engines here so 11176 08:19:37,260 --> 08:19:39,660 in addition to the name and the color 11177 08:19:39,660 --> 08:19:42,240 which all vehicles have we added some 11178 08:19:42,240 --> 08:19:44,340 new properties which are specific to a 11179 08:19:44,340 --> 08:19:45,058 car 11180 08:19:45,058 --> 08:19:48,360 similar here in addition to the name and 11181 08:19:48,360 --> 08:19:50,280 the color which all vehicles have and 11182 08:19:50,280 --> 08:19:52,378 we've inherited that from our class we 11183 08:19:52,378 --> 08:19:54,660 add that also engines and doors which 11184 08:19:54,660 --> 08:19:57,180 those things are in addition they are 11185 08:19:57,180 --> 08:20:00,718 specific to a plane and more we also 11186 08:20:00,718 --> 08:20:02,878 override the move function because we're 11187 08:20:02,878 --> 08:20:05,160 not satisfied with the with the simple 11188 08:20:05,160 --> 08:20:09,360 move function from our Base Class to 11189 08:20:09,360 --> 08:20:11,458 include to provide our own 11190 08:20:11,458 --> 08:20:13,740 implementation of how a plane is moving 11191 08:20:13,740 --> 08:20:15,780 so you can see that by using an 11192 08:20:15,780 --> 08:20:17,700 inheritance and 11193 08:20:17,700 --> 08:20:19,620 adding new properties are the new 11194 08:20:19,620 --> 08:20:22,440 function you can create more specific 11195 08:20:22,440 --> 08:20:25,978 classes you can inherit you can also 11196 08:20:25,978 --> 08:20:28,080 inherit from the car class to create a 11197 08:20:28,080 --> 08:20:29,878 more specific class you can create a 11198 08:20:29,878 --> 08:20:31,160 Tesla 11199 08:20:31,160 --> 08:20:34,080 class and so on so this is what 11200 08:20:34,080 --> 08:20:36,120 inheritance is and 11201 08:20:36,120 --> 08:20:37,740 see you in the next video 11202 08:20:37,740 --> 08:20:39,958 so I thought that's a good idea to show 11203 08:20:39,958 --> 08:20:41,878 you another example with inheritance and 11204 08:20:41,878 --> 08:20:44,820 I'm gonna go down here 11205 08:20:44,820 --> 08:20:46,680 and I'm going to define a class so I'm 11206 08:20:46,680 --> 08:20:48,540 going to type the keyword class and I'm 11207 08:20:48,540 --> 08:20:50,340 going to call it View 11208 08:20:50,340 --> 08:20:52,200 and this view is going to represent a 11209 08:20:52,200 --> 08:20:54,780 view in uh which is used in Android and 11210 08:20:54,780 --> 08:20:56,160 if you want Android is just a 11211 08:20:56,160 --> 08:20:58,138 rectangular area on the screen and it's 11212 08:20:58,138 --> 08:20:59,520 responsible for drawing and event 11213 08:20:59,520 --> 08:21:00,540 handling 11214 08:21:00,540 --> 08:21:03,478 but you're gonna mimic as I said what 11215 08:21:03,478 --> 08:21:05,580 the view is and uh you know Android or 11216 08:21:05,580 --> 08:21:08,280 not going to create a view like the one 11217 08:21:08,280 --> 08:21:10,620 that is in use like the one that's used 11218 08:21:10,620 --> 08:21:11,638 in Android 11219 08:21:11,638 --> 08:21:13,500 and we Define the primary Constructor 11220 08:21:13,500 --> 08:21:15,540 but we're gonna not Define we're not 11221 08:21:15,540 --> 08:21:16,860 going to Define any properties the 11222 08:21:16,860 --> 08:21:18,420 primary Constructor 11223 08:21:18,420 --> 08:21:20,340 and this view is going to have a simple 11224 08:21:20,340 --> 08:21:23,218 function called Draw and it's going to 11225 08:21:23,218 --> 08:21:25,020 be responsible for drawing this view on 11226 08:21:25,020 --> 08:21:29,100 the screen so we type draw 11227 08:21:29,100 --> 08:21:31,740 and create a function and we're gonna 11228 08:21:31,740 --> 08:21:33,600 add the simple a simple print line here 11229 08:21:33,600 --> 08:21:36,420 which is going to say drawing The View 11230 08:21:36,420 --> 08:21:39,120 so we put here 11231 08:21:39,120 --> 08:21:40,740 quotation marks 11232 08:21:40,740 --> 08:21:42,660 drawing 11233 08:21:42,660 --> 08:21:44,820 The View 11234 08:21:44,820 --> 08:21:47,218 and uh we're gonna 11235 08:21:47,218 --> 08:21:50,160 mark this class as open because I want 11236 08:21:50,160 --> 08:21:51,840 to inherit it from it 11237 08:21:51,840 --> 08:21:53,580 and we're also going to Mark the 11238 08:21:53,580 --> 08:21:54,958 function open because I'm going to 11239 08:21:54,958 --> 08:21:57,240 override that function in the 11240 08:21:57,240 --> 08:22:01,280 in the next class and because this is a 11241 08:22:01,280 --> 08:22:03,718 generic View and it's going to have this 11242 08:22:03,718 --> 08:22:06,600 draw function only this function what 11243 08:22:06,600 --> 08:22:08,398 you can do let's say that I want to I 11244 08:22:08,398 --> 08:22:11,458 don't want to create a simple uh a 11245 08:22:11,458 --> 08:22:13,080 simple view which is just a rectangular 11246 08:22:13,080 --> 08:22:14,940 area on the screen but I want to create 11247 08:22:14,940 --> 08:22:16,978 something more specific let's say that I 11248 08:22:16,978 --> 08:22:18,600 want to create a button 11249 08:22:18,600 --> 08:22:22,200 in that case instead of creating the 11250 08:22:22,200 --> 08:22:25,260 button the button from zero because this 11251 08:22:25,260 --> 08:22:26,340 View 11252 08:22:26,340 --> 08:22:28,378 in Android 11253 08:22:28,378 --> 08:22:31,440 has a lot of functions and properties 11254 08:22:31,440 --> 08:22:33,600 which are responsible for creating the 11255 08:22:33,600 --> 08:22:36,898 view for handling the events and so on 11256 08:22:36,898 --> 08:22:37,920 so 11257 08:22:37,920 --> 08:22:39,898 um because here we have on your simple 11258 08:22:39,898 --> 08:22:42,478 function but the real view has hundreds 11259 08:22:42,478 --> 08:22:45,718 of functions and instead of creating our 11260 08:22:45,718 --> 08:22:48,240 button from zero we can just inherit 11261 08:22:48,240 --> 08:22:51,000 from The View and we can inherit all the 11262 08:22:51,000 --> 08:22:52,440 functionality that the view already 11263 08:22:52,440 --> 08:22:55,798 provides to create a generic View and we 11264 08:22:55,798 --> 08:22:57,298 can add some 11265 08:22:57,298 --> 08:23:00,120 unique characteristics characteristics 11266 08:23:00,120 --> 08:23:02,940 which are specific to a button and in in 11267 08:23:02,940 --> 08:23:06,478 this case in this way we increase the 11268 08:23:06,478 --> 08:23:08,878 quad reusability and it's more easy for 11269 08:23:08,878 --> 08:23:11,700 us to to create the button than rather 11270 08:23:11,700 --> 08:23:13,260 than creating the button from zero 11271 08:23:13,260 --> 08:23:16,080 because the view already provides all 11272 08:23:16,080 --> 08:23:17,340 the tools that you need to create a 11273 08:23:17,340 --> 08:23:20,280 generic view only we you're gonna 11274 08:23:20,280 --> 08:23:23,520 inherit all that behavior all that 11275 08:23:23,520 --> 08:23:25,558 properties but I'm gonna but we're gonna 11276 08:23:25,558 --> 08:23:27,478 change it a little bit so this is what 11277 08:23:27,478 --> 08:23:29,458 we're gonna do with the inheritance so 11278 08:23:29,458 --> 08:23:33,540 we create a new class called button 11279 08:23:33,540 --> 08:23:36,420 so we Define the primary Constructor 11280 08:23:36,420 --> 08:23:38,398 and here you can Define some properties 11281 08:23:38,398 --> 08:23:40,740 which are specific to a button not not 11282 08:23:40,740 --> 08:23:43,080 just to a generic view which is a 11283 08:23:43,080 --> 08:23:45,898 rectangular area on the screen so we put 11284 08:23:45,898 --> 08:23:47,700 Val text and this is going to be a 11285 08:23:47,700 --> 08:23:50,398 string so our our button will have a 11286 08:23:50,398 --> 08:23:52,620 text which can be plugged in or signed 11287 08:23:52,620 --> 08:23:53,580 up 11288 08:23:53,580 --> 08:23:57,780 and also it's going to have orientation 11289 08:23:57,780 --> 08:24:00,478 because it's going to be one on a place 11290 08:24:00,478 --> 08:24:02,280 on the screen 11291 08:24:02,280 --> 08:24:04,740 on a specific place on the screen and 11292 08:24:04,740 --> 08:24:07,378 this is going to be a string 11293 08:24:07,378 --> 08:24:10,080 of course you just only Mimi only mimic 11294 08:24:10,080 --> 08:24:11,520 the 11295 08:24:11,520 --> 08:24:14,878 View and button which are are used in 11296 08:24:14,878 --> 08:24:17,458 Android this is why we have so many we 11297 08:24:17,458 --> 08:24:19,320 see we have so little properties and 11298 08:24:19,320 --> 08:24:22,138 only a simple function function in our 11299 08:24:22,138 --> 08:24:25,440 view draw and we put uh here colon to 11300 08:24:25,440 --> 08:24:27,478 inherit from our view because I want to 11301 08:24:27,478 --> 08:24:31,700 inherit all that functionality all that 11302 08:24:31,700 --> 08:24:34,020 properties that a view provides but I 11303 08:24:34,020 --> 08:24:35,700 wonder is just to change it a little bit 11304 08:24:35,700 --> 08:24:37,740 to make it more specific I want to make 11305 08:24:37,740 --> 08:24:40,920 a button and of course as I said this is 11306 08:24:40,920 --> 08:24:42,138 just uh 11307 08:24:42,138 --> 08:24:44,760 an example we don't have but in a real 11308 08:24:44,760 --> 08:24:47,760 in a real app this will have a hundreds 11309 08:24:47,760 --> 08:24:50,100 or thousands of line of code and we can 11310 08:24:50,100 --> 08:24:51,718 inherit all of that behavior or that 11311 08:24:51,718 --> 08:24:54,898 functionality in our new 11312 08:24:54,898 --> 08:24:58,020 New View which is a button but you can 11313 08:24:58,020 --> 08:24:59,878 override some functions and you can 11314 08:24:59,878 --> 08:25:02,218 provide some you can create a more 11315 08:25:02,218 --> 08:25:04,260 specific view than a general review so 11316 08:25:04,260 --> 08:25:07,138 you don't need to define the primary 11317 08:25:07,138 --> 08:25:08,520 Constructor 11318 08:25:08,520 --> 08:25:11,100 and you put the curly braces 11319 08:25:11,100 --> 08:25:13,378 now I want to override the draw 11320 08:25:13,378 --> 08:25:15,718 functions so that when the draw when the 11321 08:25:15,718 --> 08:25:18,478 draw function is called I want to not 11322 08:25:18,478 --> 08:25:21,180 drawing just a generic view but to draw 11323 08:25:21,180 --> 08:25:24,958 a button of course as I said this is 11324 08:25:24,958 --> 08:25:27,240 just an imaginary example so we type 11325 08:25:27,240 --> 08:25:29,520 overwrite fundra 11326 08:25:29,520 --> 08:25:33,860 and instead of calling the super 11327 08:25:33,860 --> 08:25:36,540 function which is inside our 11328 08:25:36,540 --> 08:25:39,120 our Base Class View or parent Class View 11329 08:25:39,120 --> 08:25:42,739 I'm gonna type here print line 11330 08:25:46,378 --> 08:25:50,100 and we're gonna put the text drawing 11331 08:25:50,100 --> 08:25:51,840 the 11332 08:25:51,840 --> 08:25:53,820 button 11333 08:25:53,820 --> 08:25:56,700 of course and now what we did here we 11334 08:25:56,700 --> 08:25:58,860 created the generic view as I said and 11335 08:25:58,860 --> 08:26:02,218 again this can have hundreds of maybe 11336 08:26:02,218 --> 08:26:04,200 thousands of line of code and functions 11337 08:26:04,200 --> 08:26:05,280 and different functions different 11338 08:26:05,280 --> 08:26:06,360 properties 11339 08:26:06,360 --> 08:26:09,540 but we we inherited from The View class 11340 08:26:09,540 --> 08:26:12,420 and we added some new functionality so 11341 08:26:12,420 --> 08:26:14,160 instead of just drawing a generic view 11342 08:26:14,160 --> 08:26:16,320 now of course we just add a simple print 11343 08:26:16,320 --> 08:26:18,478 line here but here you can have the code 11344 08:26:18,478 --> 08:26:20,160 which is going to be responsible for 11345 08:26:20,160 --> 08:26:22,798 drawing a speed a button so it's going 11346 08:26:22,798 --> 08:26:24,660 to be more specific than a simple 11347 08:26:24,660 --> 08:26:27,540 generic view so you can put here a here 11348 08:26:27,540 --> 08:26:29,700 is the code 11349 08:26:29,700 --> 08:26:30,958 for 11350 08:26:30,958 --> 08:26:32,580 creating 11351 08:26:32,580 --> 08:26:34,680 the button so I'm going to put the 11352 08:26:34,680 --> 08:26:37,920 Commander and let's say that 11353 08:26:37,920 --> 08:26:40,860 now I want to create a run around button 11354 08:26:40,860 --> 08:26:43,680 so instead of creating again all that 11355 08:26:43,680 --> 08:26:46,500 functionality which which is present in 11356 08:26:46,500 --> 08:26:49,260 the view and then create and then use 11357 08:26:49,260 --> 08:26:51,120 and then creating all the functionality 11358 08:26:51,120 --> 08:26:54,600 which is uh inside the button the button 11359 08:26:54,600 --> 08:26:57,958 class we can make this button uh open 11360 08:26:57,958 --> 08:27:00,420 and we can inherit all the functionality 11361 08:27:00,420 --> 08:27:03,540 that the button provides and we're gonna 11362 08:27:03,540 --> 08:27:06,058 add something new something unique to 11363 08:27:06,058 --> 08:27:07,200 this class that we're going to create 11364 08:27:07,200 --> 08:27:08,580 and this class is going to be a round 11365 08:27:08,580 --> 08:27:11,100 button so we type here round 11366 08:27:11,100 --> 08:27:13,020 button 11367 08:27:13,020 --> 08:27:16,500 and here you need to define the text 11368 08:27:16,500 --> 08:27:19,260 because we're going to inherit from the 11369 08:27:19,260 --> 08:27:21,420 for the from the class button 11370 08:27:21,420 --> 08:27:24,058 orientation and notice that I don't use 11371 08:27:24,058 --> 08:27:26,458 the VAR or the VAR keyword here because 11372 08:27:26,458 --> 08:27:28,680 they they're going to be only parameters 11373 08:27:28,680 --> 08:27:30,120 because the properties the actual 11374 08:27:30,120 --> 08:27:32,340 property is going to be are going to be 11375 08:27:32,340 --> 08:27:33,000 um 11376 08:27:33,000 --> 08:27:35,520 ultimately declared 11377 08:27:35,520 --> 08:27:37,798 in the button class 11378 08:27:37,798 --> 08:27:39,058 and 11379 08:27:39,058 --> 08:27:41,280 one thing which is going to be specific 11380 08:27:41,280 --> 08:27:42,840 to a round button that you're going to 11381 08:27:42,840 --> 08:27:45,180 create is going to be Corners so we type 11382 08:27:45,180 --> 08:27:48,540 Val corners 11383 08:27:49,558 --> 08:27:52,080 it was gonna be an integer because the 11384 08:27:52,080 --> 08:27:54,840 the corners basically here are going to 11385 08:27:54,840 --> 08:27:56,940 pass a value which is gonna in our 11386 08:27:56,940 --> 08:27:58,620 marginal example this is going to 11387 08:27:58,620 --> 08:28:01,320 receive a value which can be 11388 08:28:01,320 --> 08:28:03,360 um in degrees so how many degrees do you 11389 08:28:03,360 --> 08:28:04,920 want to how many degrees you want to 11390 08:28:04,920 --> 08:28:07,260 make the buttons by how many degrees you 11391 08:28:07,260 --> 08:28:09,600 want to make the button around so it can 11392 08:28:09,600 --> 08:28:12,298 pass here 20 or 30 anyway so we put 11393 08:28:12,298 --> 08:28:14,700 colon here and we inherit now from our 11394 08:28:14,700 --> 08:28:15,780 button 11395 08:28:15,780 --> 08:28:17,458 class 11396 08:28:17,458 --> 08:28:21,120 right and here you need to pass the 11397 08:28:21,120 --> 08:28:23,840 text 11398 08:28:24,058 --> 08:28:27,000 the orientation 11399 08:28:27,000 --> 08:28:28,920 and that's all 11400 08:28:28,920 --> 08:28:30,660 and you put calibrases now let's 11401 08:28:30,660 --> 08:28:33,298 override the draw function in our round 11402 08:28:33,298 --> 08:28:35,280 button because when that function is 11403 08:28:35,280 --> 08:28:37,620 called now I want to add some specific 11404 08:28:37,620 --> 08:28:40,260 code some new functionality which is 11405 08:28:40,260 --> 08:28:42,298 specific to a round button so it's not 11406 08:28:42,298 --> 08:28:46,320 gonna draw just a button but it's gonna 11407 08:28:46,320 --> 08:28:49,680 draw a round button so we of course 11408 08:28:49,680 --> 08:28:51,240 we're just going to add the simple print 11409 08:28:51,240 --> 08:28:53,218 line there but um 11410 08:28:53,218 --> 08:28:55,260 in a real app you can put here the 11411 08:28:55,260 --> 08:28:57,718 actual cottage which is gonna draw the 11412 08:28:57,718 --> 08:29:01,760 round butter so we put here print line 11413 08:29:03,240 --> 08:29:05,280 and we put 11414 08:29:05,280 --> 08:29:07,260 rowing 11415 08:29:07,260 --> 08:29:09,120 the round 11416 08:29:09,120 --> 08:29:11,580 button 11417 08:29:11,580 --> 08:29:13,920 now let's create some instances let's 11418 08:29:13,920 --> 08:29:15,540 create some objects with those classes 11419 08:29:15,540 --> 08:29:18,558 and we type here 11420 08:29:19,740 --> 08:29:22,378 Val view so we're going to create our 11421 08:29:22,378 --> 08:29:24,120 generic View 11422 08:29:24,120 --> 08:29:25,920 we Define the battery Constructor we 11423 08:29:25,920 --> 08:29:27,360 don't have to pass any values there 11424 08:29:27,360 --> 08:29:29,458 because we didn't Define any properties 11425 08:29:29,458 --> 08:29:31,500 inside our view 11426 08:29:31,500 --> 08:29:35,040 then we create a button so we put here 11427 08:29:35,040 --> 08:29:37,200 Val 11428 08:29:37,200 --> 08:29:39,718 button 11429 08:29:39,718 --> 08:29:43,500 equals button 11430 08:29:44,760 --> 08:29:46,320 and you don't need to pass some value so 11431 08:29:46,320 --> 08:29:47,820 I'm going to pass here the text let's 11432 08:29:47,820 --> 08:29:49,798 say login 11433 08:29:49,798 --> 08:29:52,798 the orientation let's say yo 11434 08:29:52,798 --> 08:29:54,420 it's going to be a text here so we put 11435 08:29:54,420 --> 08:29:57,440 here let's say Center 11436 08:29:58,978 --> 08:30:01,080 next we create a new 11437 08:30:01,080 --> 08:30:02,940 a new object but this is going to be now 11438 08:30:02,940 --> 08:30:06,120 a run around the button so we put around 11439 08:30:06,120 --> 08:30:07,798 button 11440 08:30:07,798 --> 08:30:11,218 equals round button 11441 08:30:11,218 --> 08:30:14,520 and you pass the text here round 11442 08:30:14,520 --> 08:30:17,240 button 11443 08:30:18,058 --> 08:30:22,878 actually let's pause it takes uh sign up 11444 08:30:25,200 --> 08:30:27,000 and uh 11445 08:30:27,000 --> 08:30:30,740 for the orientation let's put 11446 08:30:33,420 --> 08:30:36,540 Center also 11447 08:30:36,540 --> 08:30:38,760 lo you need to pass also value from for 11448 08:30:38,760 --> 08:30:40,798 the colors let's put let's say 30 11449 08:30:40,798 --> 08:30:42,958 degrees it will be 11450 08:30:42,958 --> 08:30:45,898 in our imaginary example now let's call 11451 08:30:45,898 --> 08:30:49,020 the function the function draw on all of 11452 08:30:49,020 --> 08:30:50,638 our objects that we create so we type 11453 08:30:50,638 --> 08:30:53,940 view dot draw 11454 08:30:53,940 --> 08:30:57,000 then we put button 11455 08:30:57,000 --> 08:30:58,798 dot draw 11456 08:30:58,798 --> 08:31:03,138 and round button 11457 08:31:05,100 --> 08:31:08,580 dot draw now if you 11458 08:31:08,580 --> 08:31:10,320 just press Ctrl Z 11459 08:31:10,320 --> 08:31:13,398 if you run this code 11460 08:31:36,478 --> 08:31:39,540 so we get in the output drawing The View 11461 08:31:39,540 --> 08:31:41,340 then we get drawn in the button drawing 11462 08:31:41,340 --> 08:31:43,740 The View again drawing the round button 11463 08:31:43,740 --> 08:31:46,138 and let's uh think about why you have 11464 08:31:46,138 --> 08:31:49,680 this output so first we see that we have 11465 08:31:49,680 --> 08:31:51,780 drawing The View which is the function 11466 08:31:51,780 --> 08:31:54,298 inside our view that you created here so 11467 08:31:54,298 --> 08:31:55,860 it's from the generic view so it's 11468 08:31:55,860 --> 08:31:58,620 drawing The View then we have drawing 11469 08:31:58,620 --> 08:32:01,020 the button so 11470 08:32:01,020 --> 08:32:01,978 here 11471 08:32:01,978 --> 08:32:04,860 when this line of code is uh red it says 11472 08:32:04,860 --> 08:32:07,320 drawing the button so you have 11473 08:32:07,320 --> 08:32:09,958 we have our we have our code which is 11474 08:32:09,958 --> 08:32:12,478 you know which is with just a print line 11475 08:32:12,478 --> 08:32:16,378 but uh so this is drawing the the button 11476 08:32:16,378 --> 08:32:18,718 then it's calling the super function so 11477 08:32:18,718 --> 08:32:19,978 it's calling the function inside the 11478 08:32:19,978 --> 08:32:22,200 view which it says drawing The View 11479 08:32:22,200 --> 08:32:23,820 next 11480 08:32:23,820 --> 08:32:27,240 when this line of code is red so round 11481 08:32:27,240 --> 08:32:29,638 that round button that draw now it's 11482 08:32:29,638 --> 08:32:31,798 calling the draw 11483 08:32:31,798 --> 08:32:33,478 function which is inside the round 11484 08:32:33,478 --> 08:32:36,240 button so instead of just now calling 11485 08:32:36,240 --> 08:32:39,120 the because now we are inheriting from 11486 08:32:39,120 --> 08:32:40,558 the button class 11487 08:32:40,558 --> 08:32:43,378 now uh we override this function here 11488 08:32:43,378 --> 08:32:45,898 and it's it's uh using our 11489 08:32:45,898 --> 08:32:47,940 implementation for creating a round 11490 08:32:47,940 --> 08:32:50,340 button it's uh executing this line of 11491 08:32:50,340 --> 08:32:52,080 code and then it's calling the super 11492 08:32:52,080 --> 08:32:54,180 implementation which is drawing the 11493 08:32:54,180 --> 08:32:56,100 button you can actually remove this if 11494 08:32:56,100 --> 08:32:58,280 you want because you let's say that 11495 08:32:58,280 --> 08:33:02,580 calling the code inside from the button 11496 08:33:02,580 --> 08:33:06,600 is not gonna affect our logic so if I 11497 08:33:06,600 --> 08:33:10,820 delete this now if you run this code 11498 08:33:17,780 --> 08:33:21,180 now we have drawing the view drawing the 11499 08:33:21,180 --> 08:33:23,100 button and we have drawing the view 11500 08:33:23,100 --> 08:33:25,378 drawing the wrong button so now it's not 11501 08:33:25,378 --> 08:33:27,298 calling here the super so it's not it's 11502 08:33:27,298 --> 08:33:30,420 not calling the code which is inside the 11503 08:33:30,420 --> 08:33:32,940 for the bottom because we are inheriting 11504 08:33:32,940 --> 08:33:35,218 first from the button but but the button 11505 08:33:35,218 --> 08:33:37,920 is also inheriting from The View so it's 11506 08:33:37,920 --> 08:33:39,840 calling first this then it's calling 11507 08:33:39,840 --> 08:33:42,360 draw which is inside here and we have 11508 08:33:42,360 --> 08:33:44,820 the round but now we only have drawing 11509 08:33:44,820 --> 08:33:47,398 the wrong button because here now it's 11510 08:33:47,398 --> 08:33:49,260 using our own implementation of the draw 11511 08:33:49,260 --> 08:33:52,138 so we have drawing the round button 11512 08:33:52,138 --> 08:33:55,020 so buy this just by 11513 08:33:55,020 --> 08:33:58,558 by making our view because as I said in 11514 08:33:58,558 --> 08:34:01,200 real app this view is can have hundreds 11515 08:34:01,200 --> 08:34:03,000 of maybe thousands of line of code of 11516 08:34:03,000 --> 08:34:05,398 different functions and properties so 11517 08:34:05,398 --> 08:34:07,260 you don't want to create all of that 11518 08:34:07,260 --> 08:34:10,440 from zero when I want only to let's say 11519 08:34:10,440 --> 08:34:12,138 I want to create a button I can't 11520 08:34:12,138 --> 08:34:14,580 inherit I can inherit from The View 11521 08:34:14,580 --> 08:34:17,100 which already provides all the functions 11522 08:34:17,100 --> 08:34:19,500 and properties and logic to create a 11523 08:34:19,500 --> 08:34:21,420 generic view but I can override some 11524 08:34:21,420 --> 08:34:22,860 functions which are responsible let's 11525 08:34:22,860 --> 08:34:27,180 say for like this draw and I can ex I 11526 08:34:27,180 --> 08:34:30,240 can Define some logic and when all of 11527 08:34:30,240 --> 08:34:31,798 that functionality is called It's Gonna 11528 08:34:31,798 --> 08:34:33,780 not draw only a generic view but it's 11529 08:34:33,780 --> 08:34:36,600 gonna draw a button so by inheriting 11530 08:34:36,600 --> 08:34:39,478 from The View we increase we increase 11531 08:34:39,478 --> 08:34:41,458 code the code reusability 11532 08:34:41,458 --> 08:34:43,740 and this is very very powerful because 11533 08:34:43,740 --> 08:34:47,040 let's say that uh like in our example 11534 08:34:47,040 --> 08:34:49,620 you want to you don't you're not 11535 08:34:49,620 --> 08:34:51,600 satisfied with the simple button which 11536 08:34:51,600 --> 08:34:53,580 is used in Android and you want to 11537 08:34:53,580 --> 08:34:55,798 create a draw a round button so instead 11538 08:34:55,798 --> 08:34:58,020 of creating all of that logic from zero 11539 08:34:58,020 --> 08:35:00,378 you can just inherit from a simple 11540 08:35:00,378 --> 08:35:03,058 button you can have override some 11541 08:35:03,058 --> 08:35:06,840 functions and when when you execute that 11542 08:35:06,840 --> 08:35:08,760 quad now it's gonna execute the code 11543 08:35:08,760 --> 08:35:10,740 with the implementation with the 11544 08:35:10,740 --> 08:35:13,020 additional implementation the with the 11545 08:35:13,020 --> 08:35:15,540 additional code which is which is gonna 11546 08:35:15,540 --> 08:35:17,878 generate a round button and it's just 11547 08:35:17,878 --> 08:35:20,878 not just a simple button so this is our 11548 08:35:20,878 --> 08:35:23,398 this is our discussion about inheritance 11549 08:35:23,398 --> 08:35:26,340 I hope that this clarifies some things 11550 08:35:26,340 --> 08:35:30,020 and see you in the next video 11551 08:35:31,798 --> 08:35:33,540 so now it's time to start a discussion 11552 08:35:33,540 --> 08:35:36,180 about seal classes so often you want to 11553 08:35:36,180 --> 08:35:37,798 represent only a fixed set of 11554 08:35:37,798 --> 08:35:40,020 possibilities so however case can either 11555 08:35:40,020 --> 08:35:43,260 success or it can fail a user can be a 11556 08:35:43,260 --> 08:35:45,718 standard user or a pro user and in those 11557 08:35:45,718 --> 08:35:47,700 cases you may be thinking now that you 11558 08:35:47,700 --> 08:35:49,860 can use enum classes but enumclasses 11559 08:35:49,860 --> 08:35:52,080 have some limitations and let's see why 11560 08:35:52,080 --> 08:35:54,240 you should use instead seal class so 11561 08:35:54,240 --> 08:35:55,500 let's type Main 11562 08:35:55,500 --> 08:35:57,600 I'm going to create a in on class here 11563 08:35:57,600 --> 08:36:01,217 call it the result 11564 08:36:01,680 --> 08:36:05,520 and let's put curly braces unless I hear 11565 08:36:05,520 --> 08:36:08,058 success 11566 08:36:10,620 --> 08:36:14,160 and the error 11567 08:36:14,160 --> 08:36:17,398 now the problem with this is that we 11568 08:36:17,398 --> 08:36:19,620 cannot encode more information here so 11569 08:36:19,620 --> 08:36:22,978 let's say that I want to to to Define 11570 08:36:22,978 --> 08:36:26,820 here a non-resolvable resolver error and 11571 08:36:26,820 --> 08:36:30,540 resolver error and I cannot do that here 11572 08:36:30,540 --> 08:36:33,718 so if I type here x dot exception I can 11573 08:36:33,718 --> 08:36:36,780 I cannot do that because 11574 08:36:36,780 --> 08:36:39,840 um the the the the types that are 11575 08:36:39,840 --> 08:36:41,940 defined here cannot have properties so 11576 08:36:41,940 --> 08:36:45,360 you cannot Define this here and for that 11577 08:36:45,360 --> 08:36:47,580 we need to use seal classes and to use 11578 08:36:47,580 --> 08:36:51,120 the seal class you just type seal class 11579 08:36:51,120 --> 08:36:53,878 I'm going to call it result 11580 08:36:53,878 --> 08:36:55,740 it's going to have a parameter called 11581 08:36:55,740 --> 08:36:58,260 Val message it's going to be of type 11582 08:36:58,260 --> 08:37:00,420 string 11583 08:37:00,420 --> 08:37:03,840 and you can Define your uh 11584 08:37:03,840 --> 08:37:05,760 classes which are gone inherit from the 11585 08:37:05,760 --> 08:37:08,458 seal class either outside the seal class 11586 08:37:08,458 --> 08:37:10,320 so you can Target class let's say 11587 08:37:10,320 --> 08:37:13,100 success 11588 08:37:13,320 --> 08:37:16,978 and let's type here 11589 08:37:16,978 --> 08:37:18,360 meh 11590 08:37:18,360 --> 08:37:20,520 message of type string and we can 11591 08:37:20,520 --> 08:37:23,160 inherit from the seal class so you can 11592 08:37:23,160 --> 08:37:26,218 type here result 11593 08:37:26,218 --> 08:37:28,940 message 11594 08:37:30,000 --> 08:37:33,840 or you can copy this and you can Define 11595 08:37:33,840 --> 08:37:36,718 it inside the enumclass so you can have 11596 08:37:36,718 --> 08:37:38,760 it as an SSC class so I'm going to type 11597 08:37:38,760 --> 08:37:41,580 another class here called 11598 08:37:41,580 --> 08:37:44,360 error 11599 08:37:45,898 --> 08:37:48,440 foreign 11600 08:37:51,320 --> 08:37:54,058 the parameter here 11601 08:37:54,058 --> 08:37:55,558 actually not 11602 08:37:55,558 --> 08:37:59,218 that is the parameter 11603 08:37:59,218 --> 08:38:02,058 message 11604 08:38:18,600 --> 08:38:20,780 foreign 11605 08:38:25,520 --> 08:38:28,740 the parameter here 11606 08:38:28,740 --> 08:38:31,200 message I'm gonna hit it from the result 11607 08:38:31,200 --> 08:38:34,978 you know from the results class 11608 08:38:34,978 --> 08:38:37,978 I'm gonna pass that parameter here 11609 08:38:37,978 --> 08:38:41,120 to the parent class 11610 08:38:41,700 --> 08:38:43,020 now 11611 08:38:43,020 --> 08:38:45,540 I'm gonna Define a function down here 11612 08:38:45,540 --> 08:38:49,920 called get data fun get data and it's 11613 08:38:49,920 --> 08:38:51,958 gonna have 11614 08:38:51,958 --> 08:38:56,398 a parameter called the result 11615 08:38:56,398 --> 08:38:59,520 it's going to be of type result 11616 08:38:59,520 --> 08:39:01,080 and here you're going to define a when 11617 08:39:01,080 --> 08:39:04,080 statement so we type when 11618 08:39:04,080 --> 08:39:06,718 the type result and now is the 11619 08:39:06,718 --> 08:39:08,580 interesting part 11620 08:39:08,580 --> 08:39:11,100 now if you hover over this underline 11621 08:39:11,100 --> 08:39:14,458 over when it says when expression must 11622 08:39:14,458 --> 08:39:19,440 be exhaustive unnecessary is error 11623 08:39:19,440 --> 08:39:22,860 comma is Success branches or else Branch 11624 08:39:22,860 --> 08:39:25,558 instead so in other words what is saying 11625 08:39:25,558 --> 08:39:27,298 here by uh 11626 08:39:27,298 --> 08:39:30,240 to have the one statement exhaustive 11627 08:39:30,240 --> 08:39:32,280 it's it's what it's saying another word 11628 08:39:32,280 --> 08:39:35,218 is that it needs to cover all the 11629 08:39:35,218 --> 08:39:37,978 possibilities so both possibilities that 11630 08:39:37,978 --> 08:39:40,138 we have here and you can type yourself 11631 08:39:40,138 --> 08:39:43,260 this or you can click on here click on 11632 08:39:43,260 --> 08:39:45,420 this right bulb and you can click here 11633 08:39:45,420 --> 08:39:47,458 add the remaining branches and as you 11634 08:39:47,458 --> 08:39:49,620 can see it's added the remaining browser 11635 08:39:49,620 --> 08:39:53,040 you add is result. error and easy result 11636 08:39:53,040 --> 08:39:56,160 does success and those are exhausted 11637 08:39:56,160 --> 08:39:58,138 those are the only two possibilities 11638 08:39:58,138 --> 08:40:00,000 which are 11639 08:40:00,000 --> 08:40:02,218 which are possible so here I'm gonna 11640 08:40:02,218 --> 08:40:04,020 type result 11641 08:40:04,020 --> 08:40:06,660 let's define a function inside the CL 11642 08:40:06,660 --> 08:40:09,120 class so I'm going to type here fun 11643 08:40:09,120 --> 08:40:10,500 show 11644 08:40:10,500 --> 08:40:13,458 message 11645 08:40:15,420 --> 08:40:18,540 it's gonna have a print line which is 11646 08:40:18,540 --> 08:40:21,798 going to say result 11647 08:40:23,280 --> 08:40:26,180 dollar sign 11648 08:40:28,920 --> 08:40:31,500 message 11649 08:40:31,500 --> 08:40:34,680 so and now I'm going to put dot 11650 08:40:34,680 --> 08:40:38,580 show message and also here result 11651 08:40:38,580 --> 08:40:43,440 dot show message and now this is 11652 08:40:43,440 --> 08:40:47,820 uh this is uh exhausted because now we 11653 08:40:47,820 --> 08:40:51,478 call we covered all the possibilities 11654 08:40:51,478 --> 08:40:56,898 which which are uh inside the which are 11655 08:40:56,898 --> 08:41:00,600 as a consequence of the fact that we our 11656 08:41:00,600 --> 08:41:03,240 classes are uh inheriting from the seal 11657 08:41:03,240 --> 08:41:08,458 class so if I add here an uh else Branch 11658 08:41:08,458 --> 08:41:11,940 so if I put here else as you can as you 11659 08:41:11,940 --> 08:41:13,680 you'll see this is going to be redundant 11660 08:41:13,680 --> 08:41:15,718 because we covered all the possibilities 11661 08:41:15,718 --> 08:41:18,000 and the compilers know this and this can 11662 08:41:18,000 --> 08:41:20,878 be very useful in some scenarios 11663 08:41:20,878 --> 08:41:22,798 so if I put calibrations as you can see 11664 08:41:22,798 --> 08:41:25,320 this is gray out because it's redundant 11665 08:41:25,320 --> 08:41:28,440 because it knows that we covered all the 11666 08:41:28,440 --> 08:41:30,240 possibilities and the possibilities are 11667 08:41:30,240 --> 08:41:32,160 only those two result that error and 11668 08:41:32,160 --> 08:41:36,079 result are success so if I type here now 11669 08:41:36,500 --> 08:41:40,940 success let's type success 11670 08:41:41,280 --> 08:41:44,180 equals 11671 08:41:44,340 --> 08:41:46,340 result 11672 08:41:46,340 --> 08:41:49,320 bo2s there 11673 08:41:49,320 --> 08:41:53,520 equals result dot success 11674 08:41:53,520 --> 08:41:57,260 let's type success here 11675 08:42:00,180 --> 08:42:05,660 let's press Ctrl D let's call this error 11676 08:42:08,280 --> 08:42:11,218 result let's change this to error so 11677 08:42:11,218 --> 08:42:16,398 this is how you create instances of the 11678 08:42:17,160 --> 08:42:21,500 nested classes let's call this failed 11679 08:42:24,660 --> 08:42:28,500 now let's type here get data down here 11680 08:42:28,500 --> 08:42:31,680 get data and let's let's pass success 11681 08:42:31,680 --> 08:42:34,620 and let's run our code now as you'll see 11682 08:42:34,620 --> 08:42:35,700 the message 11683 08:42:35,700 --> 08:42:39,020 result success 11684 08:42:50,160 --> 08:42:52,978 so we get results success in the output 11685 08:42:52,978 --> 08:42:54,000 so 11686 08:42:54,000 --> 08:42:56,458 if I change now this to error you will 11687 08:42:56,458 --> 08:42:58,378 see that this is the result error 11688 08:42:58,378 --> 08:43:01,680 because it is executing this branch 11689 08:43:01,680 --> 08:43:04,878 so if I run this 11690 08:43:08,398 --> 08:43:10,920 to get result failed 11691 08:43:10,920 --> 08:43:13,558 now the interesting part here is if I 11692 08:43:13,558 --> 08:43:16,218 add here another 11693 08:43:16,820 --> 08:43:19,138 possibilities so 11694 08:43:19,138 --> 08:43:21,420 if I type here class 11695 08:43:21,420 --> 08:43:23,458 progress 11696 08:43:23,458 --> 08:43:26,218 and I type message 11697 08:43:26,218 --> 08:43:29,398 string inheriting from result 11698 08:43:29,398 --> 08:43:31,138 let's type let's pass the message 11699 08:43:31,138 --> 08:43:33,120 parameter to the super class to the 11700 08:43:33,120 --> 08:43:35,160 parent class 11701 08:43:35,160 --> 08:43:38,700 now what what we have because our class 11702 08:43:38,700 --> 08:43:41,100 is a sealed class what we have here is 11703 08:43:41,100 --> 08:43:43,798 that the when is underlined and is 11704 08:43:43,798 --> 08:43:45,780 trying to say something to us is saying 11705 08:43:45,780 --> 08:43:47,760 that when expression must be exhaustive 11706 08:43:47,760 --> 08:43:50,878 unnecessary is progress so it knows that 11707 08:43:50,878 --> 08:43:54,540 there is another possibility for uh for 11708 08:43:54,540 --> 08:43:59,458 our result uh we it can be therefore you 11709 08:43:59,458 --> 08:44:01,500 need to add that possibility otherwise 11710 08:44:01,500 --> 08:44:03,120 I'm going to show you this order and if 11711 08:44:03,120 --> 08:44:04,740 you try to compile this code I'm gonna 11712 08:44:04,740 --> 08:44:08,878 throw an error so what what you can do 11713 08:44:08,878 --> 08:44:11,458 here is can we can type here is a result 11714 08:44:11,458 --> 08:44:14,100 to make it exhaustive so to cover all 11715 08:44:14,100 --> 08:44:18,660 the possibility result of that progress 11716 08:44:19,920 --> 08:44:22,860 let's put Arrow 11717 08:44:22,860 --> 08:44:26,100 result dot show message 11718 08:44:26,100 --> 08:44:28,860 and now if I create here another Val 11719 08:44:28,860 --> 08:44:31,740 let's call this uh 11720 08:44:31,740 --> 08:44:33,840 progress 11721 08:44:33,840 --> 08:44:37,558 because the result dot progress and 11722 08:44:37,558 --> 08:44:41,958 let's define for the parameter 11723 08:44:44,580 --> 08:44:47,340 progress here 11724 08:44:47,340 --> 08:44:51,540 now if I change this to progress 11725 08:44:51,540 --> 08:44:53,040 now you'll see the output result 11726 08:44:53,040 --> 08:44:55,760 progress 11727 08:44:59,218 --> 08:45:02,878 so we get result progress because 11728 08:45:02,878 --> 08:45:07,500 now this branch is true but if I let's 11729 08:45:07,500 --> 08:45:09,958 if I delete all of those look what 11730 08:45:09,958 --> 08:45:13,320 happens if I click here or is where you 11731 08:45:13,320 --> 08:45:15,660 have those on this underline it says add 11732 08:45:15,660 --> 08:45:17,760 else Branch so when expression must be 11733 08:45:17,760 --> 08:45:19,978 exhausted so it means that it needs to 11734 08:45:19,978 --> 08:45:21,600 cover all the possibility 11735 08:45:21,600 --> 08:45:24,540 possibilities are necessary is error is 11736 08:45:24,540 --> 08:45:26,878 progress and is Success so if we click 11737 08:45:26,878 --> 08:45:28,740 here on more actions 11738 08:45:28,740 --> 08:45:30,780 and if you click here on ADD remaining 11739 08:45:30,780 --> 08:45:34,920 branches it it knows that those are all 11740 08:45:34,920 --> 08:45:37,260 the possibilities on which our result 11741 08:45:37,260 --> 08:45:40,200 can be and it automatically generated 11742 08:45:40,200 --> 08:45:41,638 the 11743 08:45:41,638 --> 08:45:45,600 the check for us and we we only just 11744 08:45:45,600 --> 08:45:47,458 need to type here result 11745 08:45:47,458 --> 08:45:50,820 not show message and that's all and with 11746 08:45:50,820 --> 08:45:53,120 tnumps you you don't have this 11747 08:45:53,120 --> 08:45:58,820 auto-generated code for you because uh 11748 08:45:58,940 --> 08:46:03,377 I'm gonna explain immediately 11749 08:46:03,780 --> 08:46:06,860 so with with an enum if you add another 11750 08:46:06,860 --> 08:46:11,040 so if you have a Nemo class I can delete 11751 08:46:11,040 --> 08:46:13,680 the code now here because 11752 08:46:13,680 --> 08:46:15,600 but I'm going to keep the code as it is 11753 08:46:15,600 --> 08:46:18,420 and because all the subclasses of excel 11754 08:46:18,420 --> 08:46:20,878 class are known by the compiler the 11755 08:46:20,878 --> 08:46:23,280 compiler can fill all the possible cases 11756 08:46:23,280 --> 08:46:25,558 automatically for us if I declare this 11757 08:46:25,558 --> 08:46:28,378 Arrow class as a seal class and I 11758 08:46:28,378 --> 08:46:30,958 declare two classes inside here called 11759 08:46:30,958 --> 08:46:33,420 let's say class 11760 08:46:33,420 --> 08:46:39,320 resolvable error so or recoverable error 11761 08:46:42,180 --> 08:46:43,978 and it's going to have a parameter 11762 08:46:43,978 --> 08:46:46,080 called exception it's going to be of 11763 08:46:46,080 --> 08:46:48,898 type exception 11764 08:46:48,898 --> 08:46:51,680 this one 11765 08:46:52,978 --> 08:46:55,138 and we're going to extend from our error 11766 08:46:55,138 --> 08:46:57,360 seal class 11767 08:46:57,360 --> 08:47:00,058 not this one 11768 08:47:00,058 --> 08:47:01,440 this one 11769 08:47:01,440 --> 08:47:02,580 and we're going to pass the message 11770 08:47:02,580 --> 08:47:05,420 parameter 11771 08:47:06,058 --> 08:47:09,058 and I'm going to declare another class 11772 08:47:09,058 --> 08:47:12,058 non-recoverable 11773 08:47:15,780 --> 08:47:19,138 and it's going to have an exception 11774 08:47:19,138 --> 08:47:21,780 so this is more powerful than 11775 08:47:21,780 --> 08:47:24,420 in arms because you can have all those 11776 08:47:24,420 --> 08:47:27,600 properties and you can you can have 11777 08:47:27,600 --> 08:47:30,478 subtypes and and all of this and within 11778 08:47:30,478 --> 08:47:32,218 arms we cannot do this so let's extend 11779 08:47:32,218 --> 08:47:34,940 from error 11780 08:47:35,218 --> 08:47:38,340 let's pass the message 11781 08:47:38,340 --> 08:47:40,440 and now I need to delete this because we 11782 08:47:40,440 --> 08:47:43,260 cannot instantiate The Shield 11783 08:47:43,260 --> 08:47:46,638 class so let's delete this 11784 08:47:47,580 --> 08:47:50,218 now if I delete this now from here look 11785 08:47:50,218 --> 08:47:52,740 what happens I have an underline and it 11786 08:47:52,740 --> 08:47:54,478 says 11787 08:47:54,478 --> 08:47:55,680 add 11788 08:47:55,680 --> 08:47:57,478 it says one expression must be 11789 08:47:57,478 --> 08:47:59,940 exhaustive so it must cover all possible 11790 08:47:59,940 --> 08:48:03,840 cases that that exists for this cell 11791 08:48:03,840 --> 08:48:07,080 class unnecessary is not recoverable or 11792 08:48:07,080 --> 08:48:10,620 is recovered or branches so if I click 11793 08:48:10,620 --> 08:48:13,680 on this and I click on this bulb I click 11794 08:48:13,680 --> 08:48:16,080 here add remaining branches as you can 11795 08:48:16,080 --> 08:48:18,540 see it adds all the possible cases so 11796 08:48:18,540 --> 08:48:20,340 all the possible cases that we Define 11797 08:48:20,340 --> 08:48:22,558 inside our seal class for us 11798 08:48:22,558 --> 08:48:23,820 automatically 11799 08:48:23,820 --> 08:48:27,320 and we just type your result 11800 08:48:27,320 --> 08:48:30,000 dot show message 11801 08:48:30,000 --> 08:48:32,360 again here result 11802 08:48:32,360 --> 08:48:35,638 message and you cannot have this 11803 08:48:35,638 --> 08:48:37,500 Behavior with the non-class so this is 11804 08:48:37,500 --> 08:48:39,360 why still classes are more powerful 11805 08:48:39,360 --> 08:48:42,058 because they can have subclasses you can 11806 08:48:42,058 --> 08:48:43,020 have 11807 08:48:43,020 --> 08:48:45,478 more information on the classes so you 11808 08:48:45,478 --> 08:48:47,100 can have exceptions passed to them and 11809 08:48:47,100 --> 08:48:49,260 so on so 11810 08:48:49,260 --> 08:48:51,058 this is our discussion about seal 11811 08:48:51,058 --> 08:48:55,820 classes and see you in the next video 11812 08:48:57,000 --> 08:48:58,620 so now it's time to start a discussion 11813 08:48:58,620 --> 08:49:00,660 about abstract classes and to show you 11814 08:49:00,660 --> 08:49:02,340 what abstract classes are I'm going to 11815 08:49:02,340 --> 08:49:04,440 use the code that you had in our 11816 08:49:04,440 --> 08:49:06,958 discussion about inheritance but I'm 11817 08:49:06,958 --> 08:49:08,280 going to change it a little bit so I'm 11818 08:49:08,280 --> 08:49:09,840 going to delete for this class plane 11819 08:49:09,840 --> 08:49:13,798 because you don't need this class and 11820 08:49:13,798 --> 08:49:16,200 I'm gonna 11821 08:49:16,200 --> 08:49:18,840 I'm gonna delete this instances that we 11822 08:49:18,840 --> 08:49:21,200 have here 11823 08:49:22,080 --> 08:49:25,138 and I'm going to declare this as a VAR 11824 08:49:25,138 --> 08:49:27,420 and this also has a VAR so they're gonna 11825 08:49:27,420 --> 08:49:29,398 be now properties 11826 08:49:29,398 --> 08:49:31,320 I'm gonna delete the properties that we 11827 08:49:31,320 --> 08:49:33,240 have here 11828 08:49:33,240 --> 08:49:35,160 and uh 11829 08:49:35,160 --> 08:49:37,798 now we have this here so I'm gonna 11830 08:49:37,798 --> 08:49:39,780 delete these two 11831 08:49:39,780 --> 08:49:41,940 let's delete this 11832 08:49:41,940 --> 08:49:43,740 let's delete this 11833 08:49:43,740 --> 08:49:45,180 now 11834 08:49:45,180 --> 08:49:47,280 what are abstract classes 11835 08:49:47,280 --> 08:49:50,058 um abstract classes are very similar to 11836 08:49:50,058 --> 08:49:52,860 interfaces the only difference is that 11837 08:49:52,860 --> 08:49:54,840 in abstract classes you can declare 11838 08:49:54,840 --> 08:49:57,958 properties which can have a value and 11839 08:49:57,958 --> 08:49:59,700 you know in interfaces you can't 11840 08:49:59,700 --> 08:50:02,458 actually you can but you you need to 11841 08:50:02,458 --> 08:50:04,680 provide an access modifier 11842 08:50:04,680 --> 08:50:07,740 and to declare an abstract class we go 11843 08:50:07,740 --> 08:50:10,260 in front of the class that you I want to 11844 08:50:10,260 --> 08:50:12,540 make it abstract and we type the 11845 08:50:12,540 --> 08:50:14,398 abstract keyword 11846 08:50:14,398 --> 08:50:17,780 let's put the class keyword 11847 08:50:18,058 --> 08:50:21,360 and now our class is abstract and with 11848 08:50:21,360 --> 08:50:22,860 an abstract class you cannot create 11849 08:50:22,860 --> 08:50:25,680 instances abstract classes are created 11850 08:50:25,680 --> 08:50:29,160 only to be inherited from uh by other 11851 08:50:29,160 --> 08:50:31,978 classes and now if I try to create an 11852 08:50:31,978 --> 08:50:33,958 instance with this abstract class and I 11853 08:50:33,958 --> 08:50:37,378 just type Val vehicle 11854 08:50:37,378 --> 08:50:40,200 and we type vehicle here and we put the 11855 08:50:40,200 --> 08:50:42,058 parenthesis to call the go to the 11856 08:50:42,058 --> 08:50:43,138 Constructor 11857 08:50:43,138 --> 08:50:45,360 you have an error here and says cannot 11858 08:50:45,360 --> 08:50:46,798 create an instance of an abstract class 11859 08:50:46,798 --> 08:50:48,420 so you cannot create an object with an 11860 08:50:48,420 --> 08:50:50,100 abstract class an abstract class can 11861 08:50:50,100 --> 08:50:54,898 only be inherited by another class and 11862 08:50:54,898 --> 08:50:58,200 abstract classes usually are used like 11863 08:50:58,200 --> 08:51:00,860 interfaces when you want to provide 11864 08:51:00,860 --> 08:51:03,898 what you need what needs to be done but 11865 08:51:03,898 --> 08:51:05,398 you don't provide how it's going to be 11866 08:51:05,398 --> 08:51:07,320 done so you can you need abstract 11867 08:51:07,320 --> 08:51:09,780 classes are used when you define 11868 08:51:09,780 --> 08:51:13,260 uh functions without the body and the 11869 08:51:13,260 --> 08:51:16,558 code the body and the code is uh it's uh 11870 08:51:16,558 --> 08:51:18,780 implemented it's provided by the class 11871 08:51:18,780 --> 08:51:21,360 by the class which inherits from the 11872 08:51:21,360 --> 08:51:24,000 abstract class so they are as I say 11873 08:51:24,000 --> 08:51:25,798 similar very similar to interfaces the 11874 08:51:25,798 --> 08:51:27,360 only difference that in abstract classes 11875 08:51:27,360 --> 08:51:29,398 you can have properties so you can have 11876 08:51:29,398 --> 08:51:31,440 here a Val 11877 08:51:31,440 --> 08:51:33,478 text 11878 08:51:33,478 --> 08:51:36,000 and you can assign here some text in uh 11879 08:51:36,000 --> 08:51:38,040 in an interface you cannot do this let's 11880 08:51:38,040 --> 08:51:39,840 type some text 11881 08:51:39,840 --> 08:51:42,898 so this is the difference and uh 11882 08:51:42,898 --> 08:51:44,818 also 11883 08:51:44,818 --> 08:51:47,458 um you I should say that um 11884 08:51:47,458 --> 08:51:50,520 you can Implement many as many 11885 08:51:50,520 --> 08:51:52,218 interfaces as you want 11886 08:51:52,218 --> 08:51:55,260 but in a class but you can only inherit 11887 08:51:55,260 --> 08:51:58,080 from a single class right this I forgot 11888 08:51:58,080 --> 08:51:59,458 to say when I talked about the 11889 08:51:59,458 --> 08:52:01,680 interfaces now 11890 08:52:01,680 --> 08:52:05,218 if I want to make uh if I want to if I 11891 08:52:05,218 --> 08:52:08,218 to make now our functions abstract you 11892 08:52:08,218 --> 08:52:10,020 just type the keyword abstract in front 11893 08:52:10,020 --> 08:52:13,740 of them and now they are abstract and 11894 08:52:13,740 --> 08:52:15,718 we're gonna do the same for our stop 11895 08:52:15,718 --> 08:52:18,180 function but now we have this underline 11896 08:52:18,180 --> 08:52:21,180 because as I said abstract functions 11897 08:52:21,180 --> 08:52:23,878 like interfaces or the functions which 11898 08:52:23,878 --> 08:52:25,500 are defined in the interfaces cannot 11899 08:52:25,500 --> 08:52:28,680 have a body so we need to delete this uh 11900 08:52:28,680 --> 08:52:30,420 you need to delete the body you need to 11901 08:52:30,420 --> 08:52:31,740 delete the curly braces Because the 11902 08:52:31,740 --> 08:52:34,200 actual body the actual logic and the 11903 08:52:34,200 --> 08:52:35,818 code is going to be provided by the 11904 08:52:35,818 --> 08:52:38,340 class which uh in head is from the 11905 08:52:38,340 --> 08:52:39,840 abstract class now 11906 08:52:39,840 --> 08:52:42,660 to inherit from an abstract class we go 11907 08:52:42,660 --> 08:52:43,798 here 11908 08:52:43,798 --> 08:52:46,318 we put colon and we type the name of the 11909 08:52:46,318 --> 08:52:47,638 abstract class from which you want to 11910 08:52:47,638 --> 08:52:49,318 inherit vehicle 11911 08:52:49,318 --> 08:52:51,180 now 11912 08:52:51,180 --> 08:52:54,240 if you go out here now we have this red 11913 08:52:54,240 --> 08:52:57,058 bar which says 11914 08:52:57,058 --> 08:53:01,160 so need to call the Constructor here 11915 08:53:04,138 --> 08:53:06,240 all right actually I think I can delete 11916 08:53:06,240 --> 08:53:08,398 this but you still you still need to 11917 08:53:08,398 --> 08:53:09,958 call this Constructor here even though 11918 08:53:09,958 --> 08:53:13,080 we don't have any any parameters any 11919 08:53:13,080 --> 08:53:15,420 even though we didn't even Define The 11920 08:53:15,420 --> 08:53:19,080 Constructor here now we need because 11921 08:53:19,080 --> 08:53:22,138 like the interface the abstract class if 11922 08:53:22,138 --> 08:53:24,000 you inherit from an abstract cluster and 11923 08:53:24,000 --> 08:53:26,040 if that abstract abstract class has some 11924 08:53:26,040 --> 08:53:28,200 abstract abstract functions defined in 11925 08:53:28,200 --> 08:53:31,558 it you need to implement those functions 11926 08:53:31,558 --> 08:53:33,600 inside your class and you go here when 11927 08:53:33,600 --> 08:53:35,940 you have this underline if you click on 11928 08:53:35,940 --> 08:53:37,740 this right red bulb and it says 11929 08:53:37,740 --> 08:53:40,500 Implement members order make car 11930 08:53:40,500 --> 08:53:43,020 abstract and we click on Implement 11931 08:53:43,020 --> 08:53:45,898 members we select our two functions we 11932 08:53:45,898 --> 08:53:47,218 click ok 11933 08:53:47,218 --> 08:53:49,620 and now we have our functions 11934 08:53:49,620 --> 08:53:51,898 implemented inside our class 11935 08:53:51,898 --> 08:53:54,478 so this is what 11936 08:53:54,478 --> 08:53:57,180 Astra classes are and they are as I said 11937 08:53:57,180 --> 08:53:59,940 very similar to interfaces but the only 11938 08:53:59,940 --> 08:54:01,500 difference is that you can Define in 11939 08:54:01,500 --> 08:54:03,840 properties inside the an abstract class 11940 08:54:03,840 --> 08:54:05,638 and you can provide the value to that 11941 08:54:05,638 --> 08:54:10,200 property and here because we we have 11942 08:54:10,200 --> 08:54:12,120 inherited from our abstract class here 11943 08:54:12,120 --> 08:54:13,080 you can provide the actual 11944 08:54:13,080 --> 08:54:15,898 implementation of the functions so as 11945 08:54:15,898 --> 08:54:17,160 you can see we have this to do which 11946 08:54:17,160 --> 08:54:20,100 says not yet implemented so they are 11947 08:54:20,100 --> 08:54:23,100 they are very similar to interfaces 11948 08:54:23,100 --> 08:54:25,260 so I'm gonna end this video now and see 11949 08:54:25,260 --> 08:54:27,420 you in the next video but before I end 11950 08:54:27,420 --> 08:54:30,260 the video I saw that it's a good idea to 11951 08:54:30,260 --> 08:54:33,540 to say to you where abstract classes and 11952 08:54:33,540 --> 08:54:36,780 interfaces are used uh interfaces that 11953 08:54:36,780 --> 08:54:39,958 are using many many places but um 11954 08:54:39,958 --> 08:54:42,200 abstract classes are particularly used 11955 08:54:42,200 --> 08:54:46,080 when uh and also interfaces are used 11956 08:54:46,080 --> 08:54:49,378 when you have some apis and those apis 11957 08:54:49,378 --> 08:54:52,318 want to just to define the functions and 11958 08:54:52,318 --> 08:54:54,478 the API is going to generate the actual 11959 08:54:54,478 --> 08:54:57,240 code for uh for those function so you 11960 08:54:57,240 --> 08:54:59,818 can have a let's say a database 11961 08:54:59,818 --> 08:55:02,040 and you can provide the the name of the 11962 08:55:02,040 --> 08:55:03,840 functions which are responsible for 11963 08:55:03,840 --> 08:55:06,318 creating the database for getting 11964 08:55:06,318 --> 08:55:09,478 getting data from the database but you 11965 08:55:09,478 --> 08:55:11,700 don't provide the actual code which is 11966 08:55:11,700 --> 08:55:13,798 gonna select that data from the database 11967 08:55:13,798 --> 08:55:16,200 and return the data you just you just 11968 08:55:16,200 --> 08:55:18,600 declare your functions as as the 11969 08:55:18,600 --> 08:55:19,740 abstract 11970 08:55:19,740 --> 08:55:22,440 and the API is going to generate all 11971 08:55:22,440 --> 08:55:25,378 that code which is responsible for uh 11972 08:55:25,378 --> 08:55:28,798 returning the data and from the in other 11973 08:55:28,798 --> 08:55:30,360 words the API is going to do all the 11974 08:55:30,360 --> 08:55:32,878 hard work you just you you just Define 11975 08:55:32,878 --> 08:55:36,540 the the functions if you have if you 11976 08:55:36,540 --> 08:55:37,920 let's say if you have a property Define 11977 08:55:37,920 --> 08:55:39,600 an abstract class in the abstract 11978 08:55:39,600 --> 08:55:42,540 functions or interfaces and the API is 11979 08:55:42,540 --> 08:55:45,420 going to do the hard work of uh based on 11980 08:55:45,420 --> 08:55:48,958 what your function is doing by 11981 08:55:48,958 --> 08:55:50,700 editing the name of the function is 11982 08:55:50,700 --> 08:55:52,740 going to generate all that code which 11983 08:55:52,740 --> 08:55:56,040 you you've you've had to generate if you 11984 08:55:56,040 --> 08:55:58,440 do if you don't have if you're not 11985 08:55:58,440 --> 08:56:00,240 before the API to generate that for you 11986 08:56:00,240 --> 08:56:04,200 so this is where uh interfaces and after 11987 08:56:04,200 --> 08:56:07,860 classes are sometimes used and the 11988 08:56:07,860 --> 08:56:10,440 interfaces also are used in event 11989 08:56:10,440 --> 08:56:14,420 handling and that is one in one um uh 11990 08:56:14,420 --> 08:56:17,940 the you need to write code to respond to 11991 08:56:17,940 --> 08:56:20,340 respond to respond to graphical using 11992 08:56:20,340 --> 08:56:25,260 using your interface uh movements 11993 08:56:25,500 --> 08:56:27,600 so I'm gonna end the video now see you 11994 08:56:27,600 --> 08:56:29,340 next 11995 08:56:29,340 --> 08:56:31,020 so now it's time to start a discussion 11996 08:56:31,020 --> 08:56:32,818 about data classes but in order to 11997 08:56:32,818 --> 08:56:34,680 understand what data classes are we 11998 08:56:34,680 --> 08:56:36,180 first need to understand what is the 11999 08:56:36,180 --> 08:56:37,680 difference between structure equality 12000 08:56:37,680 --> 08:56:39,898 versus referential equality 12001 08:56:39,898 --> 08:56:41,580 and to restart the difference I'm going 12002 08:56:41,580 --> 08:56:43,138 to create two variables the first one is 12003 08:56:43,138 --> 08:56:44,700 going to be a Val it's going to be 12004 08:56:44,700 --> 08:56:46,200 called name one 12005 08:56:46,200 --> 08:56:48,660 and we're gonna assign the 12006 08:56:48,660 --> 08:56:50,458 follow Alex three so it's going to be of 12007 08:56:50,458 --> 08:56:51,840 type string 12008 08:56:51,840 --> 08:56:53,458 now I'm going to press Ctrl D to 12009 08:56:53,458 --> 08:56:55,558 duplicate that line of code 12010 08:56:55,558 --> 08:56:57,958 and to declare the second variable which 12011 08:56:57,958 --> 08:56:59,340 is going to be called name two and it's 12012 08:56:59,340 --> 08:57:01,680 also going to have the value RX 12013 08:57:01,680 --> 08:57:03,478 now I'm going to add the printerland 12014 08:57:03,478 --> 08:57:05,580 here to output something to the console 12015 08:57:05,580 --> 08:57:07,878 and here we're gonna type name one 12016 08:57:07,878 --> 08:57:10,200 equals equals 12017 08:57:10,200 --> 08:57:11,458 name to 12018 08:57:11,458 --> 08:57:14,760 so the equals to operator is doing what 12019 08:57:14,760 --> 08:57:17,218 is called is checking for structural 12020 08:57:17,218 --> 08:57:20,040 equality because it's checking to see if 12021 08:57:20,040 --> 08:57:22,260 the content of the variable name 1 is 12022 08:57:22,260 --> 08:57:23,520 the same with the content of the 12023 08:57:23,520 --> 08:57:25,200 variable name too so another loss is 12024 08:57:25,200 --> 08:57:26,818 checking to see if they have the same 12025 08:57:26,818 --> 08:57:30,180 value and it's they have because both 12026 08:57:30,180 --> 08:57:32,398 have Alex assigned to them so now if I 12027 08:57:32,398 --> 08:57:33,840 run this code 12028 08:57:33,840 --> 08:57:35,940 because this is a Boolean expression 12029 08:57:35,940 --> 08:57:37,978 this is going to return true or false 12030 08:57:37,978 --> 08:57:39,120 and we're going to see in the console 12031 08:57:39,120 --> 08:57:40,200 too 12032 08:57:40,200 --> 08:57:43,558 but if I change this to Alexandro now 12033 08:57:43,558 --> 08:57:45,540 the variable name 2 has a different 12034 08:57:45,540 --> 08:57:47,818 value so this is going to return false 12035 08:57:47,818 --> 08:57:50,040 because they are not structurally equal 12036 08:57:50,040 --> 08:57:52,080 their content is different they have 12037 08:57:52,080 --> 08:57:54,798 different values 12038 08:57:55,080 --> 08:57:58,020 now get false and this is called the 12039 08:57:58,020 --> 08:57:59,760 structure equality 12040 08:57:59,760 --> 08:58:02,100 the next type of equality is called 12041 08:58:02,100 --> 08:58:04,500 referential equality and the reference 12042 08:58:04,500 --> 08:58:06,540 equality is used when you want to check 12043 08:58:06,540 --> 08:58:09,898 if two variables or two objects are the 12044 08:58:09,898 --> 08:58:13,378 same so what the referential equality 12045 08:58:13,378 --> 08:58:16,020 operator is doing is checking to see if 12046 08:58:16,020 --> 08:58:18,600 a tool as I said variables or 12047 08:58:18,600 --> 08:58:20,580 objects are the same so if I type here 12048 08:58:20,580 --> 08:58:22,860 name one 12049 08:58:22,860 --> 08:58:25,138 and to use the the reference we call it 12050 08:58:25,138 --> 08:58:27,240 operator we put three equal sign here 12051 08:58:27,240 --> 08:58:29,520 and if I type here name one equals name 12052 08:58:29,520 --> 08:58:32,760 one so is name one equals to itself that 12053 08:58:32,760 --> 08:58:34,740 is going to be true 12054 08:58:34,740 --> 08:58:36,000 so you're gonna see through on the 12055 08:58:36,000 --> 08:58:37,200 console 12056 08:58:37,200 --> 08:58:40,798 outputted so we have two but if I change 12057 08:58:40,798 --> 08:58:43,920 name one equals equals equals to name 12058 08:58:43,920 --> 08:58:46,138 two that is going to return false 12059 08:58:46,138 --> 08:58:48,298 because they are distinct objects in 12060 08:58:48,298 --> 08:58:51,660 memory they are different uh variables 12061 08:58:51,660 --> 08:58:55,200 they are not the same there is the name 12062 08:58:55,200 --> 08:58:56,100 one 12063 08:58:56,100 --> 08:58:57,840 name one variable is not the same 12064 08:58:57,840 --> 08:58:59,580 variable as name two so we're gonna have 12065 08:58:59,580 --> 08:59:00,958 false here 12066 08:59:00,958 --> 08:59:02,700 so we have false so this is the 12067 08:59:02,700 --> 08:59:04,500 difference between a structural versus 12068 08:59:04,500 --> 08:59:06,898 referential equality so now I'm going to 12069 08:59:06,898 --> 08:59:08,280 close this 12070 08:59:08,280 --> 08:59:10,440 I'm going to close the console 12071 08:59:10,440 --> 08:59:12,898 and having this in mind we're going to 12072 08:59:12,898 --> 08:59:15,958 start a discussion about data classes 12073 08:59:15,958 --> 08:59:17,878 so let's go down here and let's create 12074 08:59:17,878 --> 08:59:21,240 our user class because I want to compare 12075 08:59:21,240 --> 08:59:23,160 to user objects to see if they are 12076 08:59:23,160 --> 08:59:25,798 structure equals so you type user let's 12077 08:59:25,798 --> 08:59:27,958 define the primary Constructor and let's 12078 08:59:27,958 --> 08:59:30,000 define three properties for the primary 12079 08:59:30,000 --> 08:59:31,620 Constructor the 12080 08:59:31,620 --> 08:59:34,579 first name 12081 08:59:36,298 --> 08:59:39,318 the last name 12082 08:59:39,540 --> 08:59:42,378 and the age 12083 08:59:46,558 --> 08:59:48,540 which is going to be an integer 12084 08:59:48,540 --> 08:59:51,420 now if I want to check if to 12085 08:59:51,420 --> 08:59:53,520 user objects are a star vehicle let's 12086 08:59:53,520 --> 08:59:55,260 first create those objects so let's 12087 08:59:55,260 --> 08:59:58,100 delete this code 12088 08:59:59,760 --> 09:00:02,638 let's say Val user1 12089 09:00:02,638 --> 09:00:04,978 equals now let's create our object and 12090 09:00:04,978 --> 09:00:06,958 let's pass some values to the 12091 09:00:06,958 --> 09:00:08,818 properties of the Constructor let's pass 12092 09:00:08,818 --> 09:00:11,360 here Alex 12093 09:00:13,378 --> 09:00:16,740 for the last name let's put open 12094 09:00:16,740 --> 09:00:19,798 and for the age 23. 12095 09:00:19,798 --> 09:00:21,600 now let's press Ctrl D to duplicate that 12096 09:00:21,600 --> 09:00:23,160 line of code and to create the second 12097 09:00:23,160 --> 09:00:26,040 user object let's give a different name 12098 09:00:26,040 --> 09:00:28,878 to this user too 12099 09:00:29,940 --> 09:00:33,000 now go down here let's press Ctrl alt 12100 09:00:33,000 --> 09:00:35,160 alt for mother code 12101 09:00:35,160 --> 09:00:37,440 we typed our println because I want to 12102 09:00:37,440 --> 09:00:39,958 check to see if our users objects are 12103 09:00:39,958 --> 09:00:40,798 equal 12104 09:00:40,798 --> 09:00:42,360 if they are structurally equal so we 12105 09:00:42,360 --> 09:00:43,740 type here user1 12106 09:00:43,740 --> 09:00:46,040 equals equals so the equals to operator 12107 09:00:46,040 --> 09:00:48,600 user 2. 12108 09:00:48,600 --> 09:00:51,298 now if you run this code what do you 12109 09:00:51,298 --> 09:00:53,340 think you're going to see in the output 12110 09:00:53,340 --> 09:00:54,718 what you're seeing the output is 12111 09:00:54,718 --> 09:00:57,478 actually that you'll get 12112 09:00:57,478 --> 09:01:00,240 we get false and you may be wondering 12113 09:01:00,240 --> 09:01:01,978 why because if you think about the air 12114 09:01:01,978 --> 09:01:03,058 structurally because they have the same 12115 09:01:03,058 --> 09:01:05,160 values for the properties but they are a 12116 09:01:05,160 --> 09:01:09,240 distinct objects but we said previously 12117 09:01:09,240 --> 09:01:11,398 that the equals 12118 09:01:11,398 --> 09:01:13,620 equals uh two equals operator is 12119 09:01:13,620 --> 09:01:15,898 checking for uh structure equality so 12120 09:01:15,898 --> 09:01:17,818 why get false here 12121 09:01:17,818 --> 09:01:20,040 this is because previously the equals to 12122 09:01:20,040 --> 09:01:22,700 operator is 12123 09:01:22,700 --> 09:01:25,318 it was checking to see if the two 12124 09:01:25,318 --> 09:01:27,840 strings are structurally equal and the 12125 09:01:27,840 --> 09:01:30,420 equals to operator is actually 12126 09:01:30,420 --> 09:01:33,000 a function which is defined inside the 12127 09:01:33,000 --> 09:01:35,398 any class and the string class what 12128 09:01:35,398 --> 09:01:37,080 actually was doing when we called the 12129 09:01:37,080 --> 09:01:38,700 equals to operator 12130 09:01:38,700 --> 09:01:42,298 it was using its own implementation of 12131 09:01:42,298 --> 09:01:45,058 the equals method so 12132 09:01:45,058 --> 09:01:49,620 uh every class that we create in kotlin 12133 09:01:49,620 --> 09:01:53,040 it's using its implicitly inheriting 12134 09:01:53,040 --> 09:01:54,898 from the any class and because every 12135 09:01:54,898 --> 09:01:56,458 class that we created it's implicitly 12136 09:01:56,458 --> 09:01:58,378 inheriting from the any class 12137 09:01:58,378 --> 09:02:01,440 every class is also going to use the 12138 09:02:01,440 --> 09:02:03,840 functions which are inside the any class 12139 09:02:03,840 --> 09:02:06,600 so if you don't override those functions 12140 09:02:06,600 --> 09:02:08,398 inside your class to provide your own 12141 09:02:08,398 --> 09:02:10,318 implementation like the string class did 12142 09:02:10,318 --> 09:02:12,958 for our strengths then it's going to use 12143 09:02:12,958 --> 09:02:16,200 the implicit uh the font is going to use 12144 09:02:16,200 --> 09:02:17,580 the function which inside which are 12145 09:02:17,580 --> 09:02:20,340 inside the any class and 12146 09:02:20,340 --> 09:02:24,780 if you type here user1 that and we have 12147 09:02:24,780 --> 09:02:26,818 equals to string and hashcode those 12148 09:02:26,818 --> 09:02:28,920 three but those three functions we are 12149 09:02:28,920 --> 09:02:30,360 particularly interested and the 12150 09:02:30,360 --> 09:02:32,100 particular one is interested inside the 12151 09:02:32,100 --> 09:02:34,798 equals they perform when you don't 12152 09:02:34,798 --> 09:02:37,020 Implement inside your own class those 12153 09:02:37,020 --> 09:02:38,700 functions 12154 09:02:38,700 --> 09:02:40,500 again particularly on Bay course class 12155 09:02:40,500 --> 09:02:43,200 the equals class spin forums only a 12156 09:02:43,200 --> 09:02:45,120 referential equality so it only checks 12157 09:02:45,120 --> 09:02:46,920 if those two users 12158 09:02:46,920 --> 09:02:49,620 are the same users and if they're not 12159 09:02:49,620 --> 09:02:52,020 that's gonna it's gonna return false so 12160 09:02:52,020 --> 09:02:53,580 this is why you get false here because 12161 09:02:53,580 --> 09:02:56,280 it's calling the function the function 12162 09:02:56,280 --> 09:02:58,318 which is inside any class so what you 12163 09:02:58,318 --> 09:03:01,020 have implicitly is something like this 12164 09:03:01,020 --> 09:03:03,958 so for any class that you you use or for 12165 09:03:03,958 --> 09:03:05,700 any class that you declare 12166 09:03:05,700 --> 09:03:07,920 you have implicity inherited from the 12167 09:03:07,920 --> 09:03:10,020 any class and because of that you have 12168 09:03:10,020 --> 09:03:11,398 those 12169 09:03:11,398 --> 09:03:13,138 three functions which 12170 09:03:13,138 --> 09:03:15,898 are defined and if you don't override 12171 09:03:15,898 --> 09:03:17,638 them to provide your own implementation 12172 09:03:17,638 --> 09:03:19,378 then it's going to use the code that's 12173 09:03:19,378 --> 09:03:21,660 inside there so actually if you press 12174 09:03:21,660 --> 09:03:23,160 Ctrl here 12175 09:03:23,160 --> 09:03:24,780 and if you 12176 09:03:24,780 --> 09:03:27,180 click it takes us inside the any class 12177 09:03:27,180 --> 09:03:30,718 so it's clear that this function 12178 09:03:30,718 --> 09:03:33,260 the equals function is inside any class 12179 09:03:33,260 --> 09:03:36,058 so because we don't have any functions 12180 09:03:36,058 --> 09:03:37,080 here so we need to override that 12181 09:03:37,080 --> 09:03:39,120 function inside our class and we need to 12182 09:03:39,120 --> 09:03:40,978 provide our own implementation to Define 12183 09:03:40,978 --> 09:03:44,520 our own code to determine if two user 12184 09:03:44,520 --> 09:03:46,978 are one of when two users are slash 12185 09:03:46,978 --> 09:03:48,420 legal this is what you're going to 12186 09:03:48,420 --> 09:03:51,780 Define inside our class so let's delete 12187 09:03:51,780 --> 09:03:53,580 this 12188 09:03:53,580 --> 09:03:55,378 let's pull the curly braces and actually 12189 09:03:55,378 --> 09:03:57,540 I'm gonna bring an image to show you how 12190 09:03:57,540 --> 09:04:00,420 the quarter and hierarchy actually looks 12191 09:04:00,420 --> 09:04:02,940 so this is the image and at the top as 12192 09:04:02,940 --> 09:04:04,558 you can see we have the any class and 12193 09:04:04,558 --> 09:04:06,718 then we have our Boolean class our 12194 09:04:06,718 --> 09:04:08,638 string class and then we have our user 12195 09:04:08,638 --> 09:04:10,378 class and all of those classes I have 12196 09:04:10,378 --> 09:04:12,718 here the Boolean the string the number 12197 09:04:12,718 --> 09:04:14,700 they are inheriting from the any class 12198 09:04:14,700 --> 09:04:16,138 they are 12199 09:04:16,138 --> 09:04:18,600 using the the functions which are 12200 09:04:18,600 --> 09:04:21,058 defined inside the any class and they 12201 09:04:21,058 --> 09:04:23,218 override the they are overriding those 12202 09:04:23,218 --> 09:04:25,260 methods inside them and they are 12203 09:04:25,260 --> 09:04:27,420 providing their own implementation for 12204 09:04:27,420 --> 09:04:29,760 those functions and this is what we need 12205 09:04:29,760 --> 09:04:31,378 to do also here as you can see they will 12206 09:04:31,378 --> 09:04:34,620 inherit from the any class implicitly so 12207 09:04:34,620 --> 09:04:36,478 we need to override them to provide our 12208 09:04:36,478 --> 09:04:39,240 own implementation for the equals and 12209 09:04:39,240 --> 09:04:40,500 for the next two because we're going to 12210 09:04:40,500 --> 09:04:42,000 look at also at the 12211 09:04:42,000 --> 09:04:44,340 as you saw there you are going to look 12212 09:04:44,340 --> 09:04:46,860 also at hash code because uh equals and 12213 09:04:46,860 --> 09:04:49,378 hash code are very linked together and 12214 09:04:49,378 --> 09:04:51,360 also the two string function 12215 09:04:51,360 --> 09:04:53,878 so we need to override those uh inside 12216 09:04:53,878 --> 09:04:54,958 our class 12217 09:04:54,958 --> 09:04:57,420 so this is what we're gonna do 12218 09:04:57,420 --> 09:04:59,520 so we go inside our user class and here 12219 09:04:59,520 --> 09:05:01,200 we need to override the equals function 12220 09:05:01,200 --> 09:05:03,420 when we type overwrite and while I'm 12221 09:05:03,420 --> 09:05:05,218 dumping you can see that we have three 12222 09:05:05,218 --> 09:05:07,200 suggestions here and we select the user 12223 09:05:07,200 --> 09:05:09,180 function because this is the one that we 12224 09:05:09,180 --> 09:05:11,218 are interested in and as you can see 12225 09:05:11,218 --> 09:05:12,120 here 12226 09:05:12,120 --> 09:05:15,120 this function is part of the any class 12227 09:05:15,120 --> 09:05:18,058 so we press enter we delete the super 12228 09:05:18,058 --> 09:05:19,260 return 12229 09:05:19,260 --> 09:05:20,700 and the first thing that we need to 12230 09:05:20,700 --> 09:05:23,340 check is to see if the current instance 12231 09:05:23,340 --> 09:05:25,620 the current object that on which this 12232 09:05:25,620 --> 09:05:28,700 equals function is called it's comparing 12233 09:05:28,700 --> 09:05:33,718 with its cell so we need to check if the 12234 09:05:33,718 --> 09:05:36,840 the if the object is the same as the 12235 09:05:36,840 --> 09:05:38,760 object that we are comparing with and to 12236 09:05:38,760 --> 09:05:41,540 do that we type here if this 12237 09:05:41,540 --> 09:05:45,120 so if this current object is current 12238 09:05:45,120 --> 09:05:48,360 instance equals equals so three equals 12239 09:05:48,360 --> 09:05:50,398 we check for referential equality to see 12240 09:05:50,398 --> 09:05:53,818 if they are the same equals 12241 09:05:53,818 --> 09:05:56,818 other so 12242 09:05:56,818 --> 09:05:59,818 we type other and if that is the case if 12243 09:05:59,818 --> 09:06:02,218 we call we are comparing the same object 12244 09:06:02,218 --> 09:06:05,940 so you have user one equals user one 12245 09:06:05,940 --> 09:06:08,339 then gonna return true because that 12246 09:06:08,339 --> 09:06:11,758 means that uh we are comparing uh with 12247 09:06:11,758 --> 09:06:15,480 the same object if that is not true then 12248 09:06:15,480 --> 09:06:17,820 the next thing that we need to check we 12249 09:06:17,820 --> 09:06:21,000 we're gonna go down here so if not we're 12250 09:06:21,000 --> 09:06:23,218 not comparing with uh it's not comparing 12251 09:06:23,218 --> 09:06:27,000 with itself then we need to check if the 12252 09:06:27,000 --> 09:06:29,640 other so the object which is passed as 12253 09:06:29,640 --> 09:06:31,800 an argument to this function which is 12254 09:06:31,800 --> 09:06:34,557 called on another object 12255 09:06:34,557 --> 09:06:37,980 and to check if other so the argument 12256 09:06:37,980 --> 09:06:42,417 is user because we we can uh we need to 12257 09:06:42,417 --> 09:06:44,519 check the expressly to see if this 12258 09:06:44,519 --> 09:06:46,377 object which is passed as an argument to 12259 09:06:46,377 --> 09:06:49,199 our equals function is an user object 12260 09:06:49,199 --> 09:06:51,718 because we don't want to check for a 12261 09:06:51,718 --> 09:06:53,820 different object we need we need to to 12262 09:06:53,820 --> 09:06:56,282 see if this is a new zero object so only 12263 09:06:56,282 --> 09:06:58,680 if the object which is passed as an 12264 09:06:58,680 --> 09:07:01,917 argument to the equals function is an 12265 09:07:01,917 --> 09:07:04,377 user object then we're gonna 12266 09:07:04,377 --> 09:07:07,438 return true so here you need to type now 12267 09:07:07,438 --> 09:07:09,300 we need to determine we need to Define 12268 09:07:09,300 --> 09:07:11,519 our Logic for what it means for two 12269 09:07:11,519 --> 09:07:15,417 users to be equal and it's up to you how 12270 09:07:15,417 --> 09:07:16,980 we Define this implementation I'm going 12271 09:07:16,980 --> 09:07:20,758 to type here only return so I'm gonna my 12272 09:07:20,758 --> 09:07:23,339 logic is if they have the same first 12273 09:07:23,339 --> 09:07:25,320 name the same last name and the same age 12274 09:07:25,320 --> 09:07:28,258 we're gonna say that they are equal we 12275 09:07:28,258 --> 09:07:30,300 have the same user of course that in a 12276 09:07:30,300 --> 09:07:32,398 real app you can have a more complex 12277 09:07:32,398 --> 09:07:34,078 implementation so we type here return 12278 09:07:34,078 --> 09:07:37,377 this so This current instance that first 12279 09:07:37,377 --> 09:07:39,237 name so 12280 09:07:39,237 --> 09:07:43,500 and then put two equals other so 12281 09:07:43,500 --> 09:07:46,438 if so the other is because we check here 12282 09:07:46,438 --> 09:07:48,960 explicit to see if this is another also 12283 09:07:48,960 --> 09:07:51,237 because this condition is going to only 12284 09:07:51,237 --> 09:07:54,300 pass if the other 12285 09:07:54,300 --> 09:07:57,718 argument is unusual so we type other 12286 09:07:57,718 --> 09:08:00,782 because we have access to the first name 12287 09:08:00,782 --> 09:08:02,699 and the last name and the age if other 12288 09:08:02,699 --> 09:08:06,122 DOT first name so this is our first 12289 09:08:06,122 --> 09:08:09,140 condition and 12290 09:08:10,199 --> 09:08:13,519 this Dot 12291 09:08:14,938 --> 09:08:18,480 this dot last name 12292 09:08:18,480 --> 09:08:21,718 so I'm going to check for the all uh for 12293 09:08:21,718 --> 09:08:23,098 our three properties 12294 09:08:23,098 --> 09:08:25,980 equals equals other so other is another 12295 09:08:25,980 --> 09:08:29,898 object because we determine that here 12296 09:08:31,140 --> 09:08:33,057 that last name 12297 09:08:33,057 --> 09:08:35,659 and 12298 09:08:37,377 --> 09:08:42,718 this dot age equals equals H so this 12299 09:08:42,718 --> 09:08:44,877 entire expression that is that we have 12300 09:08:44,877 --> 09:08:47,339 here is gonna return through only or if 12301 09:08:47,339 --> 09:08:49,377 all the our three conditionals so if 12302 09:08:49,377 --> 09:08:51,057 this DOT first name is equal to other 12303 09:08:51,057 --> 09:08:53,218 that first name so with the object that 12304 09:08:53,218 --> 09:08:55,800 is passed as an argument or equals and 12305 09:08:55,800 --> 09:08:58,258 if this data last name is equal to other 12306 09:08:58,258 --> 09:09:01,377 that last name and if this dot h is 12307 09:09:01,377 --> 09:09:02,699 equal to 12308 09:09:02,699 --> 09:09:07,098 the other that age let's put here 12309 09:09:07,980 --> 09:09:10,557 then and only then we're gonna have a 12310 09:09:10,557 --> 09:09:11,699 true here so it's going to return 12311 09:09:11,699 --> 09:09:14,699 through otherwise if one of those 12312 09:09:14,699 --> 09:09:17,160 condition fails it's gonna return false 12313 09:09:17,160 --> 09:09:20,282 and down here you need to if other let's 12314 09:09:20,282 --> 09:09:23,339 say doesn't pass so if other reason is 12315 09:09:23,339 --> 09:09:27,360 not an user we're gonna return false so 12316 09:09:27,360 --> 09:09:30,237 in that case going to turn false now if 12317 09:09:30,237 --> 09:09:34,098 I run this code look what happens 12318 09:09:34,438 --> 09:09:37,078 so previously we had the false and now 12319 09:09:37,078 --> 09:09:40,622 we have true here and this is because 12320 09:09:40,622 --> 09:09:43,500 now it's using the equals function which 12321 09:09:43,500 --> 09:09:46,078 is inside our user class so it's using 12322 09:09:46,078 --> 09:09:49,140 our own implementation so it's checked 12323 09:09:49,140 --> 09:09:53,122 here to see if uh our 12324 09:09:53,122 --> 09:09:55,622 first name is equal to other DOT first 12325 09:09:55,622 --> 09:09:58,199 name so it's checking uh so it's using 12326 09:09:58,199 --> 09:10:02,282 our logic inside the the equals function 12327 09:10:02,282 --> 09:10:05,578 so this is why you get true and you can 12328 09:10:05,578 --> 09:10:08,040 also you can now if you press if you 12329 09:10:08,040 --> 09:10:11,160 hold Ctrl here as you can see now it's 12330 09:10:11,160 --> 09:10:13,199 taking us inside the equals method that 12331 09:10:13,199 --> 09:10:15,000 we defined here it's not taking it 12332 09:10:15,000 --> 09:10:17,578 inside the ionic class this is because 12333 09:10:17,578 --> 09:10:19,680 now it's using our own implementation so 12334 09:10:19,680 --> 09:10:21,598 first to check to see if we are dealing 12335 09:10:21,598 --> 09:10:24,237 with the same object then we're going to 12336 09:10:24,237 --> 09:10:27,057 return true you're not going to go any 12337 09:10:27,057 --> 09:10:30,078 any we're not going to go down and if 12338 09:10:30,078 --> 09:10:33,122 this is false you're gonna come up here 12339 09:10:33,122 --> 09:10:34,800 you're gonna check to see if other the 12340 09:10:34,800 --> 09:10:36,300 object which is passed or equals 12341 09:10:36,300 --> 09:10:39,122 function is an user instance it's an 12342 09:10:39,122 --> 09:10:41,578 user object then I'm gonna check to see 12343 09:10:41,578 --> 09:10:44,519 if our current object that we are 12344 09:10:44,519 --> 09:10:46,800 calling on the sqls 12345 09:10:46,800 --> 09:10:49,140 function is equal which exclusive for 12346 09:10:49,140 --> 09:10:51,598 the first name there is the same we 12347 09:10:51,598 --> 09:10:53,040 check to see if the last name is the 12348 09:10:53,040 --> 09:10:54,898 same and we check to see if the age is 12349 09:10:54,898 --> 09:10:56,098 also the same then in that case it's 12350 09:10:56,098 --> 09:10:58,680 going to return true and we're gonna say 12351 09:10:58,680 --> 09:11:00,057 that they are structurally equal so 12352 09:11:00,057 --> 09:11:01,680 we're gonna see through in the output 12353 09:11:01,680 --> 09:11:04,500 else if other is not an user so if you 12354 09:11:04,500 --> 09:11:06,122 pass 12355 09:11:06,122 --> 09:11:08,160 to the function argument the different 12356 09:11:08,160 --> 09:11:10,737 parameter a different argument here 12357 09:11:10,737 --> 09:11:12,660 let's say you pass an integer then it's 12358 09:11:12,660 --> 09:11:14,519 going to return false here we're going 12359 09:11:14,519 --> 09:11:17,398 to come down here so now if you look 12360 09:11:17,398 --> 09:11:18,960 here you may be wondering how this 12361 09:11:18,960 --> 09:11:20,758 syntax works so we have those signs but 12362 09:11:20,758 --> 09:11:23,339 it's calling the equal smart function so 12363 09:11:23,339 --> 09:11:26,699 you can also put that equals if it makes 12364 09:11:26,699 --> 09:11:29,040 more sense and if we put that equals 12365 09:11:29,040 --> 09:11:31,377 user so 12366 09:11:31,377 --> 09:11:33,480 so uh 12367 09:11:33,480 --> 09:11:36,718 now you can see that but if you hover 12368 09:11:36,718 --> 09:11:39,000 over he says replace with the two equals 12369 09:11:39,000 --> 09:11:40,737 so it's the same thing as having that 12370 09:11:40,737 --> 09:11:42,180 equals and 12371 09:11:42,180 --> 09:11:45,122 passing that argument here which is user 12372 09:11:45,122 --> 09:11:48,360 two so again we are invoking uh the 12373 09:11:48,360 --> 09:11:51,839 equals method on the user one object 12374 09:11:51,839 --> 09:11:53,699 let's press Ctrl J 12375 09:11:53,699 --> 09:11:56,282 and we are passing our user to objects 12376 09:11:56,282 --> 09:11:58,860 and we are using our logic that we 12377 09:11:58,860 --> 09:12:00,718 Define here to determine if they are 12378 09:12:00,718 --> 09:12:02,898 equal 12379 09:12:04,377 --> 09:12:06,480 and you can replace this back to to 12380 09:12:06,480 --> 09:12:10,040 equals or if it makes more 12381 09:12:10,040 --> 09:12:12,782 visual sense to put the to use the 12382 09:12:12,782 --> 09:12:14,699 equals you can use equals it's the same 12383 09:12:14,699 --> 09:12:16,320 thing 12384 09:12:16,320 --> 09:12:19,699 we put user to here 12385 09:12:20,578 --> 09:12:23,098 so as you can see we are calling the 12386 09:12:23,098 --> 09:12:26,519 function so this means this object so 12387 09:12:26,519 --> 09:12:30,180 this object so defined here this if uh 12388 09:12:30,180 --> 09:12:32,578 when we're saying this that first name 12389 09:12:32,578 --> 09:12:36,237 it we we mean this object so if this is 12390 09:12:36,237 --> 09:12:39,237 that first name it's equal with the 12391 09:12:39,237 --> 09:12:42,898 other other that first name it's then we 12392 09:12:42,898 --> 09:12:44,578 are calling this object that is passed 12393 09:12:44,578 --> 09:12:47,820 here as an argument so 12394 09:12:47,820 --> 09:12:49,680 um next we will look we're going to look 12395 09:12:49,680 --> 09:12:52,078 at the next two functions because as I 12396 09:12:52,078 --> 09:12:54,417 said the equals and hash codes are very 12397 09:12:54,417 --> 09:12:56,339 linked together and we're going to also 12398 09:12:56,339 --> 09:13:00,480 look at the the two string function and 12399 09:13:00,480 --> 09:13:04,622 now if I change the first name for the 12400 09:13:04,622 --> 09:13:07,377 user 2 to join and if I run this code 12401 09:13:07,377 --> 09:13:09,480 you will get false in the output because 12402 09:13:09,480 --> 09:13:11,758 when it will come inside this if it will 12403 09:13:11,758 --> 09:13:13,019 check to see if this does first name 12404 09:13:13,019 --> 09:13:15,417 equals to other at first name and 12405 09:13:15,417 --> 09:13:17,460 because we're using the end operator and 12406 09:13:17,460 --> 09:13:19,019 all the conditions have to be true this 12407 09:13:19,019 --> 09:13:20,877 is going to return false so I get false 12408 09:13:20,877 --> 09:13:22,140 in the output 12409 09:13:22,140 --> 09:13:24,660 and um 12410 09:13:24,660 --> 09:13:28,878 but if I change it back to Alex 12411 09:13:28,917 --> 09:13:30,718 we'll have 12412 09:13:30,718 --> 09:13:33,300 true so the objects are structurally 12413 09:13:33,300 --> 09:13:35,820 equal based on the logic that we defined 12414 09:13:35,820 --> 09:13:37,199 here 12415 09:13:37,199 --> 09:13:40,199 inside our equals function 12416 09:13:40,199 --> 09:13:43,377 and as I said you also 12417 09:13:43,377 --> 09:13:45,360 you can also look at equals and hashcode 12418 09:13:45,360 --> 09:13:47,398 because they are very linked together 12419 09:13:47,398 --> 09:13:48,960 and uh 12420 09:13:48,960 --> 09:13:52,078 the rule is whenever you override equals 12421 09:13:52,078 --> 09:13:54,300 you also need to override the the 12422 09:13:54,300 --> 09:13:56,877 hashcode and if two objects are 12423 09:13:56,877 --> 09:13:58,980 comparing equal so if they are equal 12424 09:13:58,980 --> 09:14:00,782 based on the equals method that you 12425 09:14:00,782 --> 09:14:03,237 implemented they also have to have the 12426 09:14:03,237 --> 09:14:04,680 same hash code 12427 09:14:04,680 --> 09:14:08,098 and I'm not going to go into that but 12428 09:14:08,098 --> 09:14:11,160 this is because they are 12429 09:14:11,160 --> 09:14:13,680 the hash code is used for performers 12430 09:14:13,680 --> 09:14:16,438 reasons in collections but we're not to 12431 09:14:16,438 --> 09:14:19,980 go now into that we can just type here 12432 09:14:19,980 --> 09:14:22,820 override 12433 09:14:23,218 --> 09:14:24,480 and uh 12434 09:14:24,480 --> 09:14:27,300 here we just type return zero and this 12435 09:14:27,300 --> 09:14:29,339 uh this is not gonna affect our equality 12436 09:14:29,339 --> 09:14:32,218 at all as I said this hash code it will 12437 09:14:32,218 --> 09:14:34,860 make it makes more sense to implement 12438 09:14:34,860 --> 09:14:36,480 the hash code in a different way in 12439 09:14:36,480 --> 09:14:39,057 collections because they are used um for 12440 09:14:39,057 --> 09:14:41,160 performance reasons particularly with 12441 09:14:41,160 --> 09:14:43,860 hash set and hashmobs 12442 09:14:43,860 --> 09:14:47,218 but uh for now for our simple class 12443 09:14:47,218 --> 09:14:49,258 because we don't use this class inside 12444 09:14:49,258 --> 09:14:52,800 any collections we don't need because uh 12445 09:14:52,800 --> 09:14:55,377 we don't uh don't have to think about 12446 09:14:55,377 --> 09:14:58,199 that per the performance reasons to of 12447 09:14:58,199 --> 09:15:01,377 the hashcode and uh just if you want to 12448 09:15:01,377 --> 09:15:03,540 just uh just return zero here like I 12449 09:15:03,540 --> 09:15:05,160 return and yeah because it would work 12450 09:15:05,160 --> 09:15:06,660 fine it's not it's not going to affect 12451 09:15:06,660 --> 09:15:09,718 at all our equality but remember that 12452 09:15:09,718 --> 09:15:13,140 rulebook that if you override the equals 12453 09:15:13,140 --> 09:15:15,540 you also have to write a hash code and 12454 09:15:15,540 --> 09:15:17,460 uh if two objects are comparing equal 12455 09:15:17,460 --> 09:15:19,680 the more must have the same hashcode 12456 09:15:19,680 --> 09:15:22,019 this is true as I said particularly in 12457 09:15:22,019 --> 09:15:24,839 collections not now because we're not 12458 09:15:24,839 --> 09:15:27,360 using this code inside collection 12459 09:15:27,360 --> 09:15:29,460 and we can also override the tostring 12460 09:15:29,460 --> 09:15:31,019 method and this two string method is 12461 09:15:31,019 --> 09:15:33,122 used to return the string representation 12462 09:15:33,122 --> 09:15:36,122 of the class so we also type here 12463 09:15:36,122 --> 09:15:38,519 override and we have our two string 12464 09:15:38,519 --> 09:15:42,237 function I I said method a few times but 12465 09:15:42,237 --> 09:15:44,460 there are functions not methods methods 12466 09:15:44,460 --> 09:15:46,258 are different thing in Java 12467 09:15:46,258 --> 09:15:48,300 and uh 12468 09:15:48,300 --> 09:15:50,460 let's delete this function let's type it 12469 09:15:50,460 --> 09:15:54,078 again so if you can see 12470 09:15:54,122 --> 09:15:55,800 so this is going to return the string 12471 09:15:55,800 --> 09:15:57,898 representation of the class so I need to 12472 09:15:57,898 --> 09:16:00,898 to return here the values that that are 12473 09:16:00,898 --> 09:16:02,938 passed to our to our first name and last 12474 09:16:02,938 --> 09:16:06,360 name and age and if we don't do that if 12475 09:16:06,360 --> 09:16:09,300 you add the print line here 12476 09:16:09,300 --> 09:16:12,237 and you type user1 12477 09:16:12,237 --> 09:16:15,540 let's press Ctrl D and user 2. you're 12478 09:16:15,540 --> 09:16:18,860 gonna see just some 12479 09:16:19,140 --> 09:16:20,938 you're gonna see the user and you're 12480 09:16:20,938 --> 09:16:23,098 gonna see some numbers but we're not 12481 09:16:23,098 --> 09:16:24,898 gonna see the string representation of 12482 09:16:24,898 --> 09:16:27,000 the those two users you're not going to 12483 09:16:27,000 --> 09:16:29,578 see Alex so the name and the age so you 12484 09:16:29,578 --> 09:16:31,019 see this 12485 09:16:31,019 --> 09:16:33,898 dosto but uh 12486 09:16:33,898 --> 09:16:36,360 to avoid that because this also use 12487 09:16:36,360 --> 09:16:38,578 useful because previously when we want 12488 09:16:38,578 --> 09:16:40,320 to 12489 09:16:40,320 --> 09:16:41,938 um output in the console the values of 12490 09:16:41,938 --> 09:16:43,199 the properties you have to call those 12491 09:16:43,199 --> 09:16:45,839 properties and to add multiple Sprint 12492 09:16:45,839 --> 09:16:48,417 lines but with the two string 12493 09:16:48,417 --> 09:16:52,737 function we can just type here return 12494 09:16:52,737 --> 09:16:54,839 and type here 12495 09:16:54,839 --> 09:16:57,621 user 12496 09:16:58,320 --> 09:17:03,078 we put parenthesis and here we type 12497 09:17:03,237 --> 09:17:06,140 first name 12498 09:17:12,660 --> 09:17:15,480 we put equals we put dollar sign 12499 09:17:15,480 --> 09:17:18,237 let's put a single quotation mark dollar 12500 09:17:18,237 --> 09:17:19,737 sign 12501 09:17:19,737 --> 09:17:22,577 first name 12502 09:17:23,098 --> 09:17:24,839 let's put the quotation marks here a 12503 09:17:24,839 --> 09:17:26,699 single quotation marks 12504 09:17:26,699 --> 09:17:29,460 last name 12505 09:17:29,460 --> 09:17:33,540 equals dollar sign last name let's put 12506 09:17:33,540 --> 09:17:37,938 inside the single quotation marks this 12507 09:17:40,320 --> 09:17:44,758 comma and age we put also equals dollar 12508 09:17:44,758 --> 09:17:46,320 sign h 12509 09:17:46,320 --> 09:17:49,377 now if you run this code again now it's 12510 09:17:49,377 --> 09:17:52,699 going to use it's gonna one 12511 09:17:52,800 --> 09:17:55,860 when we are typing user one and user two 12512 09:17:55,860 --> 09:17:57,839 is going to use the tostring function 12513 09:17:57,839 --> 09:18:00,480 that we defined inside your class and 12514 09:18:00,480 --> 09:18:01,737 it's gonna return the string 12515 09:18:01,737 --> 09:18:04,800 representation of that specific object 12516 09:18:04,800 --> 09:18:07,878 so if you run this code 12517 09:18:13,980 --> 09:18:17,218 we get user and get first name Alex last 12518 09:18:17,218 --> 09:18:19,500 name Adobe in h23 and get for the second 12519 09:18:19,500 --> 09:18:21,237 you use their first name Alex last name 12520 09:18:21,237 --> 09:18:23,460 domain and H22 so now get the string 12521 09:18:23,460 --> 09:18:25,737 representation of this class and this is 12522 09:18:25,737 --> 09:18:27,718 uh more beautiful because you don't have 12523 09:18:27,718 --> 09:18:31,282 to work to always type that user one 12524 09:18:31,282 --> 09:18:34,680 user1.a user1. last name the first name 12525 09:18:34,680 --> 09:18:37,140 and so on and for the second object two 12526 09:18:37,140 --> 09:18:40,140 because now it's using the 12527 09:18:40,140 --> 09:18:42,360 two string function with our own 12528 09:18:42,360 --> 09:18:44,820 implementation inside here so this is 12529 09:18:44,820 --> 09:18:46,820 how you can use the tostring function 12530 09:18:46,820 --> 09:18:49,557 and this also how you can use the 12531 09:18:49,557 --> 09:18:53,820 password but as I said hashcode is it's 12532 09:18:53,820 --> 09:18:55,199 a different discussion which you're 12533 09:18:55,199 --> 09:18:56,699 gonna have when we're going to talk 12534 09:18:56,699 --> 09:18:59,218 about collections for now just type 12535 09:18:59,218 --> 09:19:01,860 return 0 here and then we could all fine 12536 09:19:01,860 --> 09:19:04,500 and if you think about there is a lot of 12537 09:19:04,500 --> 09:19:08,160 code just to compare to two objects we 12538 09:19:08,160 --> 09:19:10,622 have almost 12539 09:19:10,622 --> 09:19:13,860 38 line of hold just to to determine if 12540 09:19:13,860 --> 09:19:16,400 two objects two user objects are equal 12541 09:19:16,400 --> 09:19:20,220 and we also have to override this not 12542 09:19:20,220 --> 09:19:22,020 necessary but it will also variety this 12543 09:19:22,020 --> 09:19:24,720 hash code and the two strings so there 12544 09:19:24,720 --> 09:19:27,300 is a lot of code just to do this uh 12545 09:19:27,300 --> 09:19:29,640 simple thing to compare it is to user 12546 09:19:29,640 --> 09:19:32,160 objects are equal and because kotlin is 12547 09:19:32,160 --> 09:19:34,620 a byte it's about conciseness we don't 12548 09:19:34,620 --> 09:19:36,416 actually need to do all of this stuff 12549 09:19:36,416 --> 09:19:39,860 but I I thought I thought that it is a 12550 09:19:39,860 --> 09:19:42,416 okay to explain to you what you'll need 12551 09:19:42,416 --> 09:19:43,916 to do in the past in order to understand 12552 09:19:43,916 --> 09:19:46,320 what now questioning is doing better 12553 09:19:46,320 --> 09:19:49,680 especially with data classes so you 12554 09:19:49,680 --> 09:19:51,660 don't need to 12555 09:19:51,660 --> 09:19:54,060 type yourself all of this code that you 12556 09:19:54,060 --> 09:19:56,400 have here you can just so if I delete 12557 09:19:56,400 --> 09:19:58,916 all of this code let's copy it first and 12558 09:19:58,916 --> 09:20:00,240 then delete it 12559 09:20:00,240 --> 09:20:04,256 so if I delete all of this code also the 12560 09:20:04,256 --> 09:20:07,580 and now if I run this 12561 09:20:08,220 --> 09:20:13,020 now we get false and we get those we get 12562 09:20:13,020 --> 09:20:15,416 user and this tag text because now we 12563 09:20:15,416 --> 09:20:16,980 don't have an implementation of the 12564 09:20:16,980 --> 09:20:19,200 equals of the two string of the hashcode 12565 09:20:19,200 --> 09:20:21,480 and 12566 09:20:21,480 --> 09:20:23,756 if you think as I said it's a lot of 12567 09:20:23,756 --> 09:20:26,040 code to implement all of that and in 12568 09:20:26,040 --> 09:20:29,040 kotlin you can just type the data 12569 09:20:29,040 --> 09:20:31,680 keyword in front of the class and all of 12570 09:20:31,680 --> 09:20:34,200 that code not specifically the same 12571 09:20:34,200 --> 09:20:35,936 logic but all of that code that we typed 12572 09:20:35,936 --> 09:20:37,220 there is going to be generated 12573 09:20:37,220 --> 09:20:40,140 automatically for you so the equals 12574 09:20:40,140 --> 09:20:42,120 method is going to be generated the hash 12575 09:20:42,120 --> 09:20:44,640 code and the tostring and uh 12576 09:20:44,640 --> 09:20:46,620 the properties which are going to be 12577 09:20:46,620 --> 09:20:48,416 included inside the implementation of 12578 09:20:48,416 --> 09:20:51,900 the equals of the inside the of the hash 12579 09:20:51,900 --> 09:20:53,700 code and inside of the tostring are 12580 09:20:53,700 --> 09:20:56,160 gonna beat all the properties defined 12581 09:20:56,160 --> 09:20:59,280 inside the property Constructor so all 12582 09:20:59,280 --> 09:21:00,720 the properties defined inside the 12583 09:21:00,720 --> 09:21:03,000 property inside the primary Constructor 12584 09:21:03,000 --> 09:21:04,560 they're going to be used inside the 12585 09:21:04,560 --> 09:21:06,180 implementation of the equals of the 12586 09:21:06,180 --> 09:21:09,360 hashcode and the tostring so we just by 12587 09:21:09,360 --> 09:21:11,460 putting here the data keyword all of 12588 09:21:11,460 --> 09:21:14,580 that code all of that logic is going to 12589 09:21:14,580 --> 09:21:17,880 be implemented for us so if you run this 12590 09:21:17,880 --> 09:21:18,900 code now 12591 09:21:18,900 --> 09:21:22,620 so look at this how concise is compared 12592 09:21:22,620 --> 09:21:24,300 to what happens just one line of code 12593 09:21:24,300 --> 09:21:27,060 and if if you run our code with the data 12594 09:21:27,060 --> 09:21:28,740 keyword 12595 09:21:28,740 --> 09:21:30,840 now as you can see we have true and we 12596 09:21:30,840 --> 09:21:33,480 have user first name Alex last name so 12597 09:21:33,480 --> 09:21:36,000 you have the same output as previously 12598 09:21:36,000 --> 09:21:38,640 and just by putting data keyword 12599 09:21:38,640 --> 09:21:42,000 all of that was all of these calls so 12600 09:21:42,000 --> 09:21:46,640 let's press Ctrl J all of this squad 12601 09:21:46,916 --> 09:21:48,960 not specifically 12602 09:21:48,960 --> 09:21:51,120 exactly this code but all of this code 12603 09:21:51,120 --> 09:21:53,096 all the implementation of the equals of 12604 09:21:53,096 --> 09:21:54,540 the harsh code entry was generated 12605 09:21:54,540 --> 09:21:58,560 automatically for us just by uh just by 12606 09:21:58,560 --> 09:22:00,480 putting the data keyword in front of our 12607 09:22:00,480 --> 09:22:01,380 class 12608 09:22:01,380 --> 09:22:04,020 and if you don't want to have a property 12609 09:22:04,020 --> 09:22:06,960 included in the implementation of the 12610 09:22:06,960 --> 09:22:10,620 of the equals or of the harsh code or of 12611 09:22:10,620 --> 09:22:13,200 the the two string you can just omit it 12612 09:22:13,200 --> 09:22:15,240 from here and you just you declare it 12613 09:22:15,240 --> 09:22:16,560 inside the 12614 09:22:16,560 --> 09:22:19,436 inside the class so if I put let's say 12615 09:22:19,436 --> 09:22:22,740 here so if I delete this because data 12616 09:22:22,740 --> 09:22:25,380 classes cannot have parameters 12617 09:22:25,380 --> 09:22:27,000 and I declared it inside the class 12618 09:22:27,000 --> 09:22:30,180 inside the data class 12619 09:22:30,180 --> 09:22:33,360 VAR age and it will sign a value because 12620 09:22:33,360 --> 09:22:35,276 we cannot let 12621 09:22:35,276 --> 09:22:38,776 the property uninitialized 12622 09:22:38,936 --> 09:22:42,080 let's delete this now 12623 09:22:42,776 --> 09:22:45,660 and if you run this now that property is 12624 09:22:45,660 --> 09:22:47,040 going to be excluded from the 12625 09:22:47,040 --> 09:22:48,660 implementation of the equals the hash 12626 09:22:48,660 --> 09:22:51,240 code and of the two string so we have 12627 09:22:51,240 --> 09:22:54,000 here true but we have a first name Alex 12628 09:22:54,000 --> 09:22:56,756 last name Domi so now the limitation for 12629 09:22:56,756 --> 09:22:58,436 the equals only includes those 12630 09:22:58,436 --> 09:22:59,700 properties the first name and the last 12631 09:22:59,700 --> 09:23:02,340 name and also for the to string function 12632 09:23:02,340 --> 09:23:03,840 we have only the first name and the last 12633 09:23:03,840 --> 09:23:06,720 name the age is excluded but 12634 09:23:06,720 --> 09:23:09,980 let's put this back 12635 09:23:11,096 --> 09:23:13,500 so this is what data classes are they 12636 09:23:13,500 --> 09:23:15,000 are generating 12637 09:23:15,000 --> 09:23:17,580 Auto automatically for you the 12638 09:23:17,580 --> 09:23:20,040 implementation of the equals the harsh 12639 09:23:20,040 --> 09:23:22,436 code the two string also for the copy 12640 09:23:22,436 --> 09:23:24,360 function and for the component function 12641 09:23:24,360 --> 09:23:25,860 but we're going to look at the copy of 12642 09:23:25,860 --> 09:23:28,620 copy function in a separate video and 12643 09:23:28,620 --> 09:23:30,960 they generate all of that code that we 12644 09:23:30,960 --> 09:23:33,020 saw previously that we talked about 12645 09:23:33,020 --> 09:23:35,096 automatically for you by just putting 12646 09:23:35,096 --> 09:23:37,256 the data keyword you put the properties 12647 09:23:37,256 --> 09:23:39,060 inside the primary Constructor and all 12648 09:23:39,060 --> 09:23:40,680 of that implementation is going to be 12649 09:23:40,680 --> 09:23:42,596 generated for you for all the properties 12650 09:23:42,596 --> 09:23:44,120 you find inside the primary construction 12651 09:23:44,120 --> 09:23:47,360 and just that just by one line of code 12652 09:23:47,360 --> 09:23:49,620 all of that logic is generated 12653 09:23:49,620 --> 09:23:52,020 automatically so this is what data 12654 09:23:52,020 --> 09:23:54,596 classes are see you in the next video so 12655 09:23:54,596 --> 09:23:55,800 now it's time to start a discussion 12656 09:23:55,800 --> 09:23:59,120 about interfaces so what are interfaces 12657 09:23:59,120 --> 09:24:00,720 interfaces 12658 09:24:00,720 --> 09:24:03,240 um are you used when you want to have a 12659 09:24:03,240 --> 09:24:06,540 commonly used Behavior shared among 12660 09:24:06,540 --> 09:24:10,020 different classes but you don't 12661 09:24:10,020 --> 09:24:12,480 want to provide the actual code that 12662 09:24:12,480 --> 09:24:15,060 goes inside the class which is going to 12663 09:24:15,060 --> 09:24:17,756 implement that interface but you just 12664 09:24:17,756 --> 09:24:20,756 want to define the the name of the 12665 09:24:20,756 --> 09:24:22,080 function and the parameter of the 12666 09:24:22,080 --> 09:24:22,916 function 12667 09:24:22,916 --> 09:24:26,880 the actual code that goes inside the the 12668 09:24:26,880 --> 09:24:28,916 functions that you define in inside the 12669 09:24:28,916 --> 09:24:30,256 interface 12670 09:24:30,256 --> 09:24:33,300 they are particular to each object that 12671 09:24:33,300 --> 09:24:35,756 implements that specific interface 12672 09:24:35,756 --> 09:24:37,200 so 12673 09:24:37,200 --> 09:24:40,200 let's say that you have 12674 09:24:40,200 --> 09:24:43,256 let's say that we have some classes 12675 09:24:43,256 --> 09:24:45,000 let's say that you have a class car a 12676 09:24:45,000 --> 09:24:47,880 class which is uh which represents a 12677 09:24:47,880 --> 09:24:49,380 truck and you have a class which 12678 09:24:49,380 --> 09:24:50,960 represents 12679 09:24:50,960 --> 09:24:54,480 represents a plane you know that all of 12680 09:24:54,480 --> 09:24:57,660 those three objects have in common the 12681 09:24:57,660 --> 09:25:00,300 same behavior of starting the engine 12682 09:25:00,300 --> 09:25:03,180 let's say and you don't know how each 12683 09:25:03,180 --> 09:25:05,160 particular class is going to start the 12684 09:25:05,160 --> 09:25:08,400 engine but you know that each class is 12685 09:25:08,400 --> 09:25:11,040 gonna have in common this function which 12686 09:25:11,040 --> 09:25:11,880 is 12687 09:25:11,880 --> 09:25:14,040 I'm gonna start the engine again you 12688 09:25:14,040 --> 09:25:16,436 don't care about the particular how each 12689 09:25:16,436 --> 09:25:19,740 particular object is gonna uh what code 12690 09:25:19,740 --> 09:25:23,520 goes inside the or what uh logic goes 12691 09:25:23,520 --> 09:25:25,560 inside The Stand start engine function 12692 09:25:25,560 --> 09:25:28,680 for each of those particular objects but 12693 09:25:28,680 --> 09:25:31,380 to care only about the fact that they 12694 09:25:31,380 --> 09:25:34,436 they share the same behavior they they 12695 09:25:34,436 --> 09:25:37,020 all start the engine but how they start 12696 09:25:37,020 --> 09:25:40,500 the engine is uh particular to 12697 09:25:40,500 --> 09:25:43,436 each class and this is what we do with 12698 09:25:43,436 --> 09:25:46,500 interfaces we only Define the what so 12699 09:25:46,500 --> 09:25:48,960 what what we Define in the interface 12700 09:25:48,960 --> 09:25:50,040 only the 12701 09:25:50,040 --> 09:25:54,000 the the what and the what represents the 12702 09:25:54,000 --> 09:25:58,680 what those classes are gonna have in 12703 09:25:58,680 --> 09:26:01,560 common so you know that they uh gonna 12704 09:26:01,560 --> 09:26:03,416 have a function called start engine but 12705 09:26:03,416 --> 09:26:04,500 you don't 12706 09:26:04,500 --> 09:26:08,756 don't know how each object how each 12707 09:26:08,756 --> 09:26:12,596 class is gonna Implement that logic and 12708 09:26:12,596 --> 09:26:15,240 to that to to for that I'm gonna use an 12709 09:26:15,240 --> 09:26:18,060 interface so we Define what needs to be 12710 09:26:18,060 --> 09:26:20,220 done but we don't Define how it's going 12711 09:26:20,220 --> 09:26:22,436 to be done so to declare an interface we 12712 09:26:22,436 --> 09:26:24,596 go down here at the enclosing calibrates 12713 09:26:24,596 --> 09:26:26,520 of the main function and we type 12714 09:26:26,520 --> 09:26:29,276 interface and while I'm typing you see 12715 09:26:29,276 --> 09:26:31,436 that we have this suggestion we type we 12716 09:26:31,436 --> 09:26:34,740 press enter we can type the entire word 12717 09:26:34,740 --> 09:26:37,560 if you want so interface and I'm going 12718 09:26:37,560 --> 09:26:39,416 to call this internal phase engine and 12719 09:26:39,416 --> 09:26:41,520 the name of the interface should be with 12720 09:26:41,520 --> 09:26:43,436 a capital letter to start a capital 12721 09:26:43,436 --> 09:26:45,300 letter it should be in Pascal case if it 12722 09:26:45,300 --> 09:26:46,916 has multiple words 12723 09:26:46,916 --> 09:26:49,800 and the interfaces cannot have 12724 09:26:49,800 --> 09:26:51,840 Constructors because they cannot be 12725 09:26:51,840 --> 09:26:54,240 instantiated you cannot create you 12726 09:26:54,240 --> 09:26:56,160 cannot create an object with an 12727 09:26:56,160 --> 09:26:59,640 interface interfaces are used only they 12728 09:26:59,640 --> 09:27:01,980 are created only to be implemented by 12729 09:27:01,980 --> 09:27:05,096 classes they cannot be instantiated so 12730 09:27:05,096 --> 09:27:07,500 we need to put curly braces and inside 12731 09:27:07,500 --> 09:27:08,640 the 12732 09:27:08,640 --> 09:27:10,620 the body inside the curly braces we 12733 09:27:10,620 --> 09:27:12,660 Define what needs to be done so we 12734 09:27:12,660 --> 09:27:14,340 Define only 12735 09:27:14,340 --> 09:27:17,640 the function we Define we type the name 12736 09:27:17,640 --> 09:27:18,720 of the function I'm going to call it 12737 09:27:18,720 --> 09:27:19,916 start engine 12738 09:27:19,916 --> 09:27:22,680 but we don't Define the actual code for 12739 09:27:22,680 --> 09:27:24,416 the function so we put parenthesis here 12740 09:27:24,416 --> 09:27:25,980 you can Define parameters if you want 12741 09:27:25,980 --> 09:27:28,680 but we don't put curly braces and we 12742 09:27:28,680 --> 09:27:31,200 Define our logic here because we only 12743 09:27:31,200 --> 09:27:34,080 care what about what needs to be done so 12744 09:27:34,080 --> 09:27:37,320 we only care that each class needs to 12745 09:27:37,320 --> 09:27:39,240 start the engine how it's going to start 12746 09:27:39,240 --> 09:27:41,400 the engine it's up to each particular 12747 09:27:41,400 --> 09:27:42,660 class 12748 09:27:42,660 --> 09:27:46,320 so we go down here we type class and I'm 12749 09:27:46,320 --> 09:27:48,180 going to create a class called car and 12750 09:27:48,180 --> 09:27:49,800 it's going to have a 12751 09:27:49,800 --> 09:27:52,020 a vowel name which is going to be a 12752 09:27:52,020 --> 09:27:53,040 string 12753 09:27:53,040 --> 09:27:56,276 and a vaal color which is also going to 12754 09:27:56,276 --> 09:27:58,140 be a string 12755 09:27:58,140 --> 09:28:00,900 and now to implement the interface we 12756 09:28:00,900 --> 09:28:04,200 put colon and we type engine 12757 09:28:04,200 --> 09:28:06,300 and as you can see we have this engine 12758 09:28:06,300 --> 09:28:09,060 we have this eye for interface here so 12759 09:28:09,060 --> 09:28:11,460 you press enter then we put curly braces 12760 09:28:11,460 --> 09:28:13,740 to define the body of the class and now 12761 09:28:13,740 --> 09:28:16,140 we have this underline here and this 12762 09:28:16,140 --> 09:28:18,480 underline is here to tell us that we 12763 09:28:18,480 --> 09:28:21,480 need to in now we need to implement the 12764 09:28:21,480 --> 09:28:23,220 we need to implement this function 12765 09:28:23,220 --> 09:28:27,540 because the the interface and the class 12766 09:28:27,540 --> 09:28:29,880 have a contract is like a contract and 12767 09:28:29,880 --> 09:28:32,460 the contract says that if you implement 12768 09:28:32,460 --> 09:28:34,800 an interface you need to you must 12769 09:28:34,800 --> 09:28:37,436 Implement you must override the the the 12770 09:28:37,436 --> 09:28:39,480 function so you need to implement the 12771 09:28:39,480 --> 09:28:42,000 function you still will this is why we 12772 09:28:42,000 --> 09:28:44,400 have here here an underline so if I 12773 09:28:44,400 --> 09:28:46,800 click on this underline what if I hover 12774 09:28:46,800 --> 09:28:48,416 over there and I click 12775 09:28:48,416 --> 09:28:50,460 on the light on the red bulb you see 12776 09:28:50,460 --> 09:28:52,140 that it says Implement members so click 12777 09:28:52,140 --> 09:28:54,240 on Implement and have our 12778 09:28:54,240 --> 09:28:57,360 start engine function we click OK and 12779 09:28:57,360 --> 09:29:00,720 now our functions was overrided here and 12780 09:29:00,720 --> 09:29:02,820 we have this to do which is not yet 12781 09:29:02,820 --> 09:29:05,340 implemented and this is there to to tell 12782 09:29:05,340 --> 09:29:07,620 us that this method this function is not 12783 09:29:07,620 --> 09:29:09,720 Yeti implemented so we need to define 12784 09:29:09,720 --> 09:29:11,640 the actual logic here 12785 09:29:11,640 --> 09:29:15,060 and let's create two other classes let's 12786 09:29:15,060 --> 09:29:15,960 um 12787 09:29:15,960 --> 09:29:19,580 call this class track 12788 09:29:21,360 --> 09:29:24,540 and it's also going to have a name a Val 12789 09:29:24,540 --> 09:29:26,520 going to be a string 12790 09:29:26,520 --> 09:29:30,619 and the color also string 12791 09:29:35,276 --> 09:29:37,800 and we're gonna also 12792 09:29:37,800 --> 09:29:40,320 implement the interface so I'm going to 12793 09:29:40,320 --> 09:29:43,140 put colon engine then we put curabase 12794 09:29:43,140 --> 09:29:45,120 now we need to implement because I said 12795 09:29:45,120 --> 09:29:47,580 there is a contact between the interface 12796 09:29:47,580 --> 09:29:49,800 and the class and the contract says that 12797 09:29:49,800 --> 09:29:52,080 you need to implement the function 12798 09:29:52,080 --> 09:29:54,240 so we Implement a function here and we 12799 09:29:54,240 --> 09:29:56,340 create another class 12800 09:29:56,340 --> 09:29:59,479 call plane 12801 09:29:59,820 --> 09:30:04,520 and it's also going to have a Val name 12802 09:30:05,160 --> 09:30:07,200 it's going to be a string 12803 09:30:07,200 --> 09:30:12,240 and the eval color also string 12804 09:30:12,240 --> 09:30:14,340 and here we put colon and we're gonna 12805 09:30:14,340 --> 09:30:16,680 also implement the interface interface 12806 09:30:16,680 --> 09:30:19,200 inside the plane class 12807 09:30:19,200 --> 09:30:22,320 and we put call on engine our interface 12808 09:30:22,320 --> 09:30:24,120 then curly braces and now we have the 12809 09:30:24,120 --> 09:30:26,460 underline because again we there is the 12810 09:30:26,460 --> 09:30:28,320 contract now between the interface and 12811 09:30:28,320 --> 09:30:30,060 the class and that says that we need to 12812 09:30:30,060 --> 09:30:32,820 implement this function inside our plane 12813 09:30:32,820 --> 09:30:34,256 class 12814 09:30:34,256 --> 09:30:36,416 and we have those to do and those to do 12815 09:30:36,416 --> 09:30:39,416 are here to tell us that we need to 12816 09:30:39,416 --> 09:30:41,700 we need to imple we need to provide some 12817 09:30:41,700 --> 09:30:44,640 logic to the to the actual function so 12818 09:30:44,640 --> 09:30:46,140 let's delete those and let's put some 12819 09:30:46,140 --> 09:30:47,160 code here 12820 09:30:47,160 --> 09:30:50,180 so let's delete this 12821 09:30:56,276 --> 09:30:58,740 and let's add the print line here which 12822 09:30:58,740 --> 09:31:03,079 is gonna output some text to the console 12823 09:31:08,756 --> 09:31:10,380 and we're gonna have the text the car 12824 09:31:10,380 --> 09:31:11,820 you 12825 09:31:11,820 --> 09:31:13,560 starting 12826 09:31:13,560 --> 09:31:16,340 the engine 12827 09:31:18,660 --> 09:31:21,900 and I'm gonna copy this because now I'm 12828 09:31:21,900 --> 09:31:24,416 gonna put here some text 12829 09:31:24,416 --> 09:31:27,660 I'm gonna say the 12830 09:31:27,660 --> 09:31:30,300 track is starting the engine 12831 09:31:30,300 --> 09:31:32,640 and here is going to say the plane 12832 09:31:32,640 --> 09:31:35,659 starting the engine 12833 09:31:39,020 --> 09:31:43,080 now what what we have done here and what 12834 09:31:43,080 --> 09:31:45,060 we did here is that 12835 09:31:45,060 --> 09:31:48,540 we know that each class respectively our 12836 09:31:48,540 --> 09:31:51,480 car our tracking and our plane they all 12837 09:31:51,480 --> 09:31:54,480 gonna start the engine so they all gonna 12838 09:31:54,480 --> 09:31:58,140 share this start engine Behavior the one 12839 09:31:58,140 --> 09:32:00,840 all all gonna have this functionality 12840 09:32:00,840 --> 09:32:05,520 what we don't know is how each class how 12841 09:32:05,520 --> 09:32:08,580 each how each object is gonna start the 12842 09:32:08,580 --> 09:32:11,936 engine because they uh they start the 12843 09:32:11,936 --> 09:32:14,580 engine in a different way the engine of 12844 09:32:14,580 --> 09:32:17,220 of a car is different from an engine of 12845 09:32:17,220 --> 09:32:20,756 a truck or form of Lane so we know that 12846 09:32:20,756 --> 09:32:23,040 each each is gonna 12847 09:32:23,040 --> 09:32:26,340 not that each class or each object is 12848 09:32:26,340 --> 09:32:29,220 gonna start the engine but we don't know 12849 09:32:29,220 --> 09:32:31,140 how they're gonna start the engine so 12850 09:32:31,140 --> 09:32:34,800 here we go inside the actual uh feelings 12851 09:32:34,800 --> 09:32:36,720 inside the the function which we type 12852 09:32:36,720 --> 09:32:38,700 the text the car is starting the engine 12853 09:32:38,700 --> 09:32:40,680 the truck is starting the engine and the 12854 09:32:40,680 --> 09:32:43,436 plane is starting the engine so they 12855 09:32:43,436 --> 09:32:45,540 differed in how they start the engine 12856 09:32:45,540 --> 09:32:48,180 but you know that they all gonna they 12857 09:32:48,180 --> 09:32:50,040 all gonna start the engine so this is 12858 09:32:50,040 --> 09:32:52,980 what interfaces allows us to do 12859 09:32:52,980 --> 09:32:55,800 you can define an interface if you know 12860 09:32:55,800 --> 09:33:00,620 that uh multiple uh related or unrelated 12861 09:33:00,620 --> 09:33:03,540 classes or objects are gonna have a 12862 09:33:03,540 --> 09:33:06,480 function shared between them but you 12863 09:33:06,480 --> 09:33:08,580 don't know how each particular class or 12864 09:33:08,580 --> 09:33:12,416 object is gonna em is gonna uh how is 12865 09:33:12,416 --> 09:33:14,400 gonna how it's gonna have the actual 12866 09:33:14,400 --> 09:33:16,916 quad uh how it's gonna write the actual 12867 09:33:16,916 --> 09:33:19,916 code for that uh that behavior so you 12868 09:33:19,916 --> 09:33:22,620 don't you don't care about the how you 12869 09:33:22,620 --> 09:33:24,596 want to care about the what so we know 12870 09:33:24,596 --> 09:33:26,276 that in this case they will start the 12871 09:33:26,276 --> 09:33:28,740 engine but they they start the engine in 12872 09:33:28,740 --> 09:33:31,980 a different way and you can have here 12873 09:33:31,980 --> 09:33:34,256 another class let's say 12874 09:33:34,256 --> 09:33:38,120 that a person which creates another 12875 09:33:38,120 --> 09:33:41,700 another class and the class is gonna be 12876 09:33:41,700 --> 09:33:44,400 called Tesla and in that case again you 12877 09:33:44,400 --> 09:33:46,256 can implement the interface because you 12878 09:33:46,256 --> 09:33:48,960 know that uh the Tesla is also going to 12879 09:33:48,960 --> 09:33:51,180 start the engine you don't know how it's 12880 09:33:51,180 --> 09:33:52,740 going to start the engine but you know 12881 09:33:52,740 --> 09:33:55,916 that uh is going to start the engine and 12882 09:33:55,916 --> 09:33:57,120 how it's going to start the engine is 12883 09:33:57,120 --> 09:34:00,416 specific to a Tesla car so this is what 12884 09:34:00,416 --> 09:34:02,040 interfaces allow us to do we can Define 12885 09:34:02,040 --> 09:34:05,300 uh some shared Behavior between 12886 09:34:05,300 --> 09:34:09,060 unrelated or related classes and that 12887 09:34:09,060 --> 09:34:10,980 the actual code you don't Define any 12888 09:34:10,980 --> 09:34:13,020 code inside the interface you can put 12889 09:34:13,020 --> 09:34:15,620 you actually can Define here a property 12890 09:34:15,620 --> 09:34:20,460 but uh uh usually you just Define uh the 12891 09:34:20,460 --> 09:34:22,740 the function the function name some 12892 09:34:22,740 --> 09:34:25,020 parameters and the actual code goes 12893 09:34:25,020 --> 09:34:27,200 inside the class which implements that 12894 09:34:27,200 --> 09:34:29,640 interface let's let's actually create 12895 09:34:29,640 --> 09:34:34,560 here a class Tesla so we put here 12896 09:34:34,560 --> 09:34:37,320 let's put this a little bit up 12897 09:34:37,320 --> 09:34:41,276 so we put here class Tesla is gonna have 12898 09:34:41,276 --> 09:34:43,916 also a Val name 12899 09:34:43,916 --> 09:34:46,020 it's going to be a string 12900 09:34:46,020 --> 09:34:48,776 Naval color 12901 09:34:48,776 --> 09:34:51,300 and this is also going to be a string so 12902 09:34:51,300 --> 09:34:53,520 now we can Implement our engine 12903 09:34:53,520 --> 09:34:56,160 interface so we don't know how Tesla is 12904 09:34:56,160 --> 09:34:58,140 going to start the engine but we know 12905 09:34:58,140 --> 09:34:59,880 that it's going to start the engine so 12906 09:34:59,880 --> 09:35:02,460 we we Implement our interface put call 12907 09:35:02,460 --> 09:35:05,220 on the name of the interface and now we 12908 09:35:05,220 --> 09:35:07,680 need to implement the start engine so we 12909 09:35:07,680 --> 09:35:09,240 click on that 12910 09:35:09,240 --> 09:35:11,756 and we have not yet implemented let's 12911 09:35:11,756 --> 09:35:13,740 put our print line here I'm going to 12912 09:35:13,740 --> 09:35:15,900 press Ctrl V 12913 09:35:15,900 --> 09:35:18,180 and here's gonna say that's like 12914 09:35:18,180 --> 09:35:19,980 starting the engine 12915 09:35:19,980 --> 09:35:24,240 so this is what interfaces are and 12916 09:35:24,240 --> 09:35:26,220 see you in the next video 12917 09:35:26,220 --> 09:35:29,400 because I'm going to end the video now 12918 09:35:29,400 --> 09:35:31,560 so here you should say a Tesla is 12919 09:35:31,560 --> 09:35:34,256 starting the engine not the Tesla 12920 09:35:34,256 --> 09:35:37,756 so I collected that 12921 09:35:38,460 --> 09:35:40,436 so I thought it's a good idea to show 12922 09:35:40,436 --> 09:35:42,300 you a practical example using Android 12923 09:35:42,300 --> 09:35:45,416 studio where interfaces are used because 12924 09:35:45,416 --> 09:35:48,080 the when I first learned about 12925 09:35:48,080 --> 09:35:50,520 interfaces and after classes I found 12926 09:35:50,520 --> 09:35:52,256 them very confusing because they are 12927 09:35:52,256 --> 09:35:56,220 very very abstract and you don't see 12928 09:35:56,220 --> 09:35:59,756 when when you somebody teaches you those 12929 09:35:59,756 --> 09:36:01,800 Concepts you don't see how where and 12930 09:36:01,800 --> 09:36:04,800 where you're gonna use those uh those 12931 09:36:04,800 --> 09:36:07,680 Concepts interfaces and Abstract classes 12932 09:36:07,680 --> 09:36:09,180 and for that I'm going to open Android 12933 09:36:09,180 --> 09:36:10,860 Studio 12934 09:36:10,860 --> 09:36:13,380 to show you a practical example using 12935 09:36:13,380 --> 09:36:16,256 interfaces and also organize Android 12936 09:36:16,256 --> 09:36:18,900 Studio to show you a practical example 12937 09:36:18,900 --> 09:36:21,596 where we're going to use 12938 09:36:21,596 --> 09:36:23,820 abstract classes so I opened Android 12939 09:36:23,820 --> 09:36:26,096 Studio you don't need to it looks 12940 09:36:26,096 --> 09:36:28,980 similar to our IntelliJ because this 12941 09:36:28,980 --> 09:36:31,800 IntelliJ IDEA just that is it's Android 12942 09:36:31,800 --> 09:36:34,560 Studio so we will click on my 12943 09:36:34,560 --> 09:36:36,979 application 12944 09:36:42,480 --> 09:36:45,540 and here we have this thing called main 12945 09:36:45,540 --> 09:36:47,276 activity we don't need to worry about 12946 09:36:47,276 --> 09:36:51,300 this because as I said I just want to 12947 09:36:51,300 --> 09:36:53,276 show you where interfaces are used in 12948 09:36:53,276 --> 09:36:56,220 Android Studio to get to get an idea 12949 09:36:56,220 --> 09:36:59,640 about where they are used because I said 12950 09:36:59,640 --> 09:37:01,980 they are very abstract 12951 09:37:01,980 --> 09:37:04,160 foreign 12952 09:37:10,400 --> 09:37:14,040 this thing called activity.main This is 12953 09:37:14,040 --> 09:37:16,620 where we put our 12954 09:37:16,620 --> 09:37:19,256 UI this is where we put our buttons this 12955 09:37:19,256 --> 09:37:22,740 is where we put our list views uh this 12956 09:37:22,740 --> 09:37:26,040 is where we put generally our UI so as 12957 09:37:26,040 --> 09:37:27,720 you can see we have two buttons here and 12958 09:37:27,720 --> 09:37:29,276 they have an ID 12959 09:37:29,276 --> 09:37:32,276 again I'm not gonna go into this but if 12960 09:37:32,276 --> 09:37:34,680 you click here on design you see that we 12961 09:37:34,680 --> 09:37:38,276 have a login button and a sign up button 12962 09:37:38,276 --> 09:37:42,000 so this is the UI part of our app now 12963 09:37:42,000 --> 09:37:43,980 I'm gonna click on this main activity 12964 09:37:43,980 --> 09:37:45,660 and this is the part where you're gonna 12965 09:37:45,660 --> 09:37:48,256 put our code and now I'm gonna link 12966 09:37:48,256 --> 09:37:51,596 those UI buttons so let's go to quad so 12967 09:37:51,596 --> 09:37:53,040 this is the code for creating those 12968 09:37:53,040 --> 09:37:55,580 buttons 12969 09:37:55,800 --> 09:37:57,840 and I'm gonna link those buttons because 12970 09:37:57,840 --> 09:38:00,300 they have this ID here login button so 12971 09:38:00,300 --> 09:38:02,640 you have here Android ID 12972 09:38:02,640 --> 09:38:04,500 and if you come from Android Studio you 12973 09:38:04,500 --> 09:38:06,720 already know all of this I'm gonna link 12974 09:38:06,720 --> 09:38:09,480 those UI buttons with some objects 12975 09:38:09,480 --> 09:38:12,240 buttons by typing some code so I'm gonna 12976 09:38:12,240 --> 09:38:15,480 Define up here 12977 09:38:15,480 --> 09:38:18,480 foreign 12978 09:38:19,860 --> 09:38:23,720 called the login button 12979 09:38:24,720 --> 09:38:26,756 and it's going to be of type button so 12980 09:38:26,756 --> 09:38:29,040 this is a class button 12981 09:38:29,040 --> 09:38:31,380 then I'm gonna Define another later need 12982 09:38:31,380 --> 09:38:33,720 because I'm not gonna initialize it 12983 09:38:33,720 --> 09:38:37,220 right there so Laten it again 12984 09:38:37,916 --> 09:38:41,060 sign up button 12985 09:38:42,540 --> 09:38:45,140 and it's gonna be also of type button so 12986 09:38:45,140 --> 09:38:48,360 types like we have types for uh for our 12987 09:38:48,360 --> 09:38:50,880 numbers or for our just that this is a 12988 09:38:50,880 --> 09:38:53,400 class and this rep this class is a 12989 09:38:53,400 --> 09:38:55,320 button objectives and now we need to 12990 09:38:55,320 --> 09:38:58,020 link those those objects that we created 12991 09:38:58,020 --> 09:38:59,340 here 12992 09:38:59,340 --> 09:39:02,040 with those buttons that we have here 12993 09:39:02,040 --> 09:39:05,400 those UI buttons so to link those login 12994 09:39:05,400 --> 09:39:07,256 button and sign up button 12995 09:39:07,256 --> 09:39:09,900 and to do that to go down here 12996 09:39:09,900 --> 09:39:12,240 and I'm gonna I'm gonna go I'm gonna do 12997 09:39:12,240 --> 09:39:14,700 this in the longer way and here we type 12998 09:39:14,700 --> 09:39:16,680 login button again you don't need to 12999 09:39:16,680 --> 09:39:18,900 understand all of this this is just to 13000 09:39:18,900 --> 09:39:21,540 illustrate how interfaces work I'm gonna 13001 09:39:21,540 --> 09:39:22,916 put equals 13002 09:39:22,916 --> 09:39:25,380 and now I'm going to type find view by D 13003 09:39:25,380 --> 09:39:27,776 and this function find view by ID is 13004 09:39:27,776 --> 09:39:30,660 gonna link our button object that we 13005 09:39:30,660 --> 09:39:33,360 created here with our UI button 13006 09:39:33,360 --> 09:39:35,580 that we have 13007 09:39:35,580 --> 09:39:38,276 that we have created here and it's going 13008 09:39:38,276 --> 09:39:40,860 to be linked by its ID by this ID that 13009 09:39:40,860 --> 09:39:43,680 we have here login button so if you go 13010 09:39:43,680 --> 09:39:46,800 here hero Type R so R is this thing 13011 09:39:46,800 --> 09:39:48,180 which uh 13012 09:39:48,180 --> 09:39:50,160 again don't need to understand all of 13013 09:39:50,160 --> 09:39:53,220 these arrays is used to to get that ID 13014 09:39:53,220 --> 09:39:55,500 to link our button object with our 13015 09:39:55,500 --> 09:39:59,640 button UI so if you put r dot ID that 13016 09:39:59,640 --> 09:40:03,596 and we have our uh our two IDs so we 13017 09:40:03,596 --> 09:40:05,936 choose login button 13018 09:40:05,936 --> 09:40:10,256 so now we have linked our UI button with 13019 09:40:10,256 --> 09:40:14,820 our button object so we linked the UI in 13020 09:40:14,820 --> 09:40:18,120 other words with the code now let's link 13021 09:40:18,120 --> 09:40:20,340 also the setting up button so we type 13022 09:40:20,340 --> 09:40:22,200 here saying a button 13023 09:40:22,200 --> 09:40:25,436 equals find view by ID you you also type 13024 09:40:25,436 --> 09:40:28,980 r dot ID Dot 13025 09:40:28,980 --> 09:40:31,140 sign up button again you don't need to 13026 09:40:31,140 --> 09:40:32,460 type this code because probably you 13027 09:40:32,460 --> 09:40:34,620 don't even have Android Studio this is 13028 09:40:34,620 --> 09:40:36,960 just to illustrate something 13029 09:40:36,960 --> 09:40:38,460 now 13030 09:40:38,460 --> 09:40:40,980 to illustrate how interfaces work let's 13031 09:40:40,980 --> 09:40:43,200 say that I want to 13032 09:40:43,200 --> 09:40:45,416 let's say that I want to 13033 09:40:45,416 --> 09:40:48,360 to do something when this login button 13034 09:40:48,360 --> 09:40:50,040 is clicked and I want to do also 13035 09:40:50,040 --> 09:40:51,776 something when design up button is 13036 09:40:51,776 --> 09:40:53,820 clicked and it and here interface is 13037 09:40:53,820 --> 09:40:55,320 coming to action 13038 09:40:55,320 --> 09:40:57,480 what you can do is you can say login 13039 09:40:57,480 --> 09:40:59,700 button 13040 09:40:59,700 --> 09:41:02,640 Dot and there is a function called set 13041 09:41:02,640 --> 09:41:05,520 on click listener and this function what 13042 09:41:05,520 --> 09:41:07,020 is doing is 13043 09:41:07,020 --> 09:41:11,276 basically setting a listener on our UI 13044 09:41:11,276 --> 09:41:13,436 button so that when you click on that 13045 09:41:13,436 --> 09:41:16,680 button the function called on click is 13046 09:41:16,680 --> 09:41:20,040 going to be executed and the code that 13047 09:41:20,040 --> 09:41:22,680 is going to be in that function is 13048 09:41:22,680 --> 09:41:27,660 particular to each to each button so 13049 09:41:27,660 --> 09:41:31,080 you you you're gonna set the interface 13050 09:41:31,080 --> 09:41:33,720 so you types don't click listener and 13051 09:41:33,720 --> 09:41:36,596 hero type also this illustrates uh the 13052 09:41:36,596 --> 09:41:40,380 object also this by by this example we 13053 09:41:40,380 --> 09:41:42,000 also illustrate how the Anonymous 13054 09:41:42,000 --> 09:41:44,520 classes work so you type object 13055 09:41:44,520 --> 09:41:47,936 and now I'm gonna inherit actually gonna 13056 09:41:47,936 --> 09:41:50,820 Implement our interface on click 13057 09:41:50,820 --> 09:41:52,320 listener and this only listener 13058 09:41:52,320 --> 09:41:56,640 interface has a function called on click 13059 09:41:56,640 --> 09:41:58,916 and here to put curly braces and press 13060 09:41:58,916 --> 09:42:00,720 enter and now here we have this 13061 09:42:00,720 --> 09:42:02,640 underline 13062 09:42:02,640 --> 09:42:05,096 and if you click on it you have this red 13063 09:42:05,096 --> 09:42:08,276 bulb because it wants us to implement 13064 09:42:08,276 --> 09:42:11,460 to implement that on click function so 13065 09:42:11,460 --> 09:42:13,860 we're going to implement that 13066 09:42:13,860 --> 09:42:15,720 and it says to do not get implemented 13067 09:42:15,720 --> 09:42:17,160 I'm not going to do the same thing for 13068 09:42:17,160 --> 09:42:18,840 the 13069 09:42:18,840 --> 09:42:22,140 for the sign up button so I'm gonna copy 13070 09:42:22,140 --> 09:42:24,436 this 13071 09:42:25,140 --> 09:42:28,820 I'm gonna paste it down here 13072 09:42:28,936 --> 09:42:31,380 and I'm going to change this to sign up 13073 09:42:31,380 --> 09:42:34,320 button so now each individual each 13074 09:42:34,320 --> 09:42:37,500 individual button has us on has its own 13075 09:42:37,500 --> 09:42:41,220 on click listener interface and the code 13076 09:42:41,220 --> 09:42:43,140 which is going to be executed inside the 13077 09:42:43,140 --> 09:42:44,700 one click method because this function 13078 09:42:44,700 --> 09:42:46,740 is going to be triggered when you click 13079 09:42:46,740 --> 09:42:48,416 on the button is particular to each 13080 09:42:48,416 --> 09:42:50,040 button so this button is going to log in 13081 09:42:50,040 --> 09:42:53,400 the user this button you're gonna put 13082 09:42:53,400 --> 09:42:55,200 here the code to sign up the user but 13083 09:42:55,200 --> 09:42:56,276 you're not going to do that we're just 13084 09:42:56,276 --> 09:42:57,180 gonna 13085 09:42:57,180 --> 09:43:01,200 we're gonna uh show some text on our 13086 09:43:01,200 --> 09:43:03,960 emulator so we type here to show the 13087 09:43:03,960 --> 09:43:05,880 text toast 13088 09:43:05,880 --> 09:43:09,000 dot make text here you need to pass the 13089 09:43:09,000 --> 09:43:12,436 context so don't worry about all of this 13090 09:43:12,436 --> 09:43:16,256 main.activity here you need to pass uh 13091 09:43:16,256 --> 09:43:19,320 how how long this is gonna show on the 13092 09:43:19,320 --> 09:43:24,060 screen so we choose a short 13093 09:43:24,060 --> 09:43:29,479 and we put that show to show this on the 13094 09:43:33,900 --> 09:43:36,120 actually here we need to pass the text 13095 09:43:36,120 --> 09:43:37,980 so what text is going to be shown on the 13096 09:43:37,980 --> 09:43:39,720 screen so here we're gonna say 13097 09:43:39,720 --> 09:43:42,776 login button 13098 09:43:42,776 --> 09:43:44,580 clicked 13099 09:43:44,580 --> 09:43:47,580 and now here we need to pass how long 13100 09:43:47,580 --> 09:43:50,756 it's going to slow so you type those Dot 13101 09:43:50,756 --> 09:43:54,596 short so if it's going to be shown for a 13102 09:43:54,596 --> 09:43:57,180 short time and then we need to call the 13103 09:43:57,180 --> 09:43:59,160 show functions to show this on the 13104 09:43:59,160 --> 09:44:03,180 screen and I'm going to press Ctrl uh C 13105 09:44:03,180 --> 09:44:05,400 to copy this and I'm gonna paste inside 13106 09:44:05,400 --> 09:44:09,416 our on click function in our interface 13107 09:44:09,416 --> 09:44:11,520 that we have here for the sign up button 13108 09:44:11,520 --> 09:44:13,620 and this is gonna say 13109 09:44:13,620 --> 09:44:16,380 that 13110 09:44:16,380 --> 09:44:21,020 sign up button was click so sign up 13111 09:44:22,800 --> 09:44:24,540 button 13112 09:44:24,540 --> 09:44:26,340 clicked 13113 09:44:26,340 --> 09:44:28,860 now in Android Studio we have what is 13114 09:44:28,860 --> 09:44:30,660 called an emulator and this is basically 13115 09:44:30,660 --> 09:44:35,460 a virtual phone and we can run you can 13116 09:44:35,460 --> 09:44:37,800 run our code and we can test our code 13117 09:44:37,800 --> 09:44:39,960 and we're gonna see when 13118 09:44:39,960 --> 09:44:43,200 on that emulator those buttons and when 13119 09:44:43,200 --> 09:44:44,880 you're going to click on those buttons 13120 09:44:44,880 --> 09:44:48,060 this code that we Define here for for 13121 09:44:48,060 --> 09:44:50,520 those buttons because we linked them so 13122 09:44:50,520 --> 09:44:52,080 we linked 13123 09:44:52,080 --> 09:44:54,360 those two buttons that have the login 13124 09:44:54,360 --> 09:44:56,400 button the sign up button so this is 13125 09:44:56,400 --> 09:45:00,660 their UI code this is their XML code 13126 09:45:00,660 --> 09:45:03,776 you we've linked them through code here 13127 09:45:03,776 --> 09:45:06,240 and when we click on them the Code in 13128 09:45:06,240 --> 09:45:08,460 each particular object is going to be 13129 09:45:08,460 --> 09:45:11,936 executed so now if you run our code go 13130 09:45:11,936 --> 09:45:15,000 up here and I'm gonna create now the 13131 09:45:15,000 --> 09:45:17,400 virtual font to see our buttons and I'm 13132 09:45:17,400 --> 09:45:18,900 going to click on them to see how it 13133 09:45:18,900 --> 09:45:20,460 works 13134 09:45:20,460 --> 09:45:23,596 I'm going to close those 13135 09:45:23,700 --> 09:45:27,200 now it's connecting to the emulator 13136 09:45:27,480 --> 09:45:31,320 and this is our virtual virtual phone so 13137 09:45:31,320 --> 09:45:34,200 this is the virtual phones and here we 13138 09:45:34,200 --> 09:45:38,479 have our UI that we created here 13139 09:45:38,880 --> 09:45:42,720 so this is what the users see so this 13140 09:45:42,720 --> 09:45:45,180 the what the user say and this is what 13141 09:45:45,180 --> 09:45:47,580 we see it's the this is what happens 13142 09:45:47,580 --> 09:45:49,680 under the covers and now if I click on 13143 09:45:49,680 --> 09:45:53,596 this login button look what happens 13144 09:45:54,720 --> 09:45:57,180 it says login button click so it 13145 09:45:57,180 --> 09:45:59,096 executes this on click function inside 13146 09:45:59,096 --> 09:46:03,180 the login button if I click on the 13147 09:46:03,180 --> 09:46:06,240 sign up button it says sign up button 13148 09:46:06,240 --> 09:46:08,756 click because it's executing the one 13149 09:46:08,756 --> 09:46:11,276 click function and the code inside the 13150 09:46:11,276 --> 09:46:13,020 one click function for the sign up 13151 09:46:13,020 --> 09:46:13,916 button 13152 09:46:13,916 --> 09:46:17,756 and they are as I said linked by the by 13153 09:46:17,756 --> 09:46:20,460 the ID so this is what happens under the 13154 09:46:20,460 --> 09:46:22,436 covers this is what the user see 13155 09:46:22,436 --> 09:46:23,936 now 13156 09:46:23,936 --> 09:46:26,400 this is where interfaces can be used 13157 09:46:26,400 --> 09:46:29,160 because if you think about you can have 13158 09:46:29,160 --> 09:46:33,060 multiple uis different uis text views 13159 09:46:33,060 --> 09:46:35,900 lists or 13160 09:46:35,900 --> 09:46:38,820 items into the items in a list which can 13161 09:46:38,820 --> 09:46:41,580 be clicked and for all of those you know 13162 09:46:41,580 --> 09:46:44,040 that always go all of them are gonna 13163 09:46:44,040 --> 09:46:48,240 have on click function all all all have 13164 09:46:48,240 --> 09:46:51,660 this shared functionality to respond to 13165 09:46:51,660 --> 09:46:56,936 a click you don't know how that is gonna 13166 09:46:56,936 --> 09:46:59,936 how that that one click function is 13167 09:46:59,936 --> 09:47:00,860 gonna 13168 09:47:00,860 --> 09:47:04,020 execute or what code that on click 13169 09:47:04,020 --> 09:47:05,520 function is going to have for each 13170 09:47:05,520 --> 09:47:10,020 particular UI uh UI but you know that 13171 09:47:10,020 --> 09:47:13,680 each each one is gonna have one click 13172 09:47:13,680 --> 09:47:16,800 function what is going to go inside the 13173 09:47:16,800 --> 09:47:18,480 one click function for you for each 13174 09:47:18,480 --> 09:47:19,640 particular 13175 09:47:19,640 --> 09:47:23,880 UI it's up to that specific UI so it's 13176 09:47:23,880 --> 09:47:26,040 up to a button it's up to a text views 13177 09:47:26,040 --> 09:47:29,640 and so on so this is a over interfaces 13178 09:47:29,640 --> 09:47:32,160 are used because we know that they're 13179 09:47:32,160 --> 09:47:34,140 going to have a shared functionality of 13180 09:47:34,140 --> 09:47:36,900 one click but you don't know how that 13181 09:47:36,900 --> 09:47:38,400 functionality is going to be implemented 13182 09:47:38,400 --> 09:47:40,916 by each particular uis in our case we do 13183 09:47:40,916 --> 09:47:43,980 we know that login button and sign up 13184 09:47:43,980 --> 09:47:45,000 button 13185 09:47:45,000 --> 09:47:47,276 both both are going to have a one click 13186 09:47:47,276 --> 09:47:49,620 both we're gonna are gonna respond to 13187 09:47:49,620 --> 09:47:51,660 click events but you don't know what 13188 09:47:51,660 --> 09:47:54,120 they're gonna do so this is why you 13189 09:47:54,120 --> 09:47:57,596 define the interface and the developers 13190 09:47:57,596 --> 09:47:59,640 who write code gonna Implement that 13191 09:47:59,640 --> 09:48:02,820 interface and you're gonna write the 13192 09:48:02,820 --> 09:48:04,680 actual implementation for that function 13193 09:48:04,680 --> 09:48:08,096 which is specific to that object so this 13194 09:48:08,096 --> 09:48:10,200 is our discussion and if you find this 13195 09:48:10,200 --> 09:48:11,596 confusing 13196 09:48:11,596 --> 09:48:15,240 particularly if I'm confusing the uh if 13197 09:48:15,240 --> 09:48:17,276 because I touch a little bit on Android 13198 09:48:17,276 --> 09:48:19,800 Studio in our discussion don't be 13199 09:48:19,800 --> 09:48:22,500 because I just wanted to show you a 13200 09:48:22,500 --> 09:48:25,020 practical example where interfaces are 13201 09:48:25,020 --> 09:48:28,020 used using Android studio so let's click 13202 09:48:28,020 --> 09:48:30,000 again on login button 13203 09:48:30,000 --> 09:48:33,120 we get our toast login button clicked 13204 09:48:33,120 --> 09:48:34,916 slang a button click I can create 13205 09:48:34,916 --> 09:48:38,340 another button which can be used for a 13206 09:48:38,340 --> 09:48:40,200 I don't know if you forgot the password 13207 09:48:40,200 --> 09:48:42,776 and you you're gonna implement the on 13208 09:48:42,776 --> 09:48:45,540 click listener you're gonna override the 13209 09:48:45,540 --> 09:48:46,800 you're gonna override the one click 13210 09:48:46,800 --> 09:48:49,256 function I'm gonna write some code to 13211 09:48:49,256 --> 09:48:53,220 to send uh to send to help help the user 13212 09:48:53,220 --> 09:48:56,340 to recover its uh puzzle and so on so we 13213 09:48:56,340 --> 09:48:58,080 can see how interfaces can be used 13214 09:48:58,080 --> 09:49:01,080 practically in this with this example 13215 09:49:01,080 --> 09:49:03,300 so this is our discussion see you in the 13216 09:49:03,300 --> 09:49:05,340 next video so now it's time to start a 13217 09:49:05,340 --> 09:49:07,380 discussion about object expression but 13218 09:49:07,380 --> 09:49:09,596 first let's go down here and let's 13219 09:49:09,596 --> 09:49:11,580 create a class to see why you'll need to 13220 09:49:11,580 --> 09:49:13,140 use object expression in the first place 13221 09:49:13,140 --> 09:49:14,936 so you type here class 13222 09:49:14,936 --> 09:49:16,380 and now and our class is going to be 13223 09:49:16,380 --> 09:49:17,700 called button 13224 09:49:17,700 --> 09:49:20,220 we put the parenthesis to create the 13225 09:49:20,220 --> 09:49:21,660 primary Constructor we Define some 13226 09:49:21,660 --> 09:49:23,340 properties here the first one is going 13227 09:49:23,340 --> 09:49:27,020 to be text is going to be of type string 13228 09:49:31,140 --> 09:49:33,120 the second one is going to be also 13229 09:49:33,120 --> 09:49:35,276 available it's going to be called ID 13230 09:49:35,276 --> 09:49:37,200 it's gonna be an integer 13231 09:49:37,200 --> 09:49:39,180 and the last one is going to be called 13232 09:49:39,180 --> 09:49:42,960 One Click listener 13233 09:49:42,960 --> 09:49:44,520 it's going to be of type on click 13234 09:49:44,520 --> 09:49:46,380 listener so this is an interface that 13235 09:49:46,380 --> 09:49:48,979 we're gonna create 13236 09:49:53,460 --> 09:49:54,900 so let's go down here to create the 13237 09:49:54,900 --> 09:49:58,620 interface we type interface on and while 13238 09:49:58,620 --> 09:50:00,180 I'm jumping you see that you have this 13239 09:50:00,180 --> 09:50:02,096 suggestion one click listener 13240 09:50:02,096 --> 09:50:04,916 to create the interface press enter 13241 09:50:04,916 --> 09:50:06,960 put curly braces and let's define a 13242 09:50:06,960 --> 09:50:09,416 function fun 13243 09:50:09,416 --> 09:50:11,640 on click 13244 09:50:11,640 --> 09:50:14,096 let's put parenthesis 13245 09:50:14,096 --> 09:50:15,900 now let's create some objects with our 13246 09:50:15,900 --> 09:50:18,120 class button to see why you'll need to 13247 09:50:18,120 --> 09:50:19,140 use a 13248 09:50:19,140 --> 09:50:22,020 object expression in the first place so 13249 09:50:22,020 --> 09:50:23,580 let's go here 13250 09:50:23,580 --> 09:50:28,860 and here we type Val let's call it login 13251 09:50:28,860 --> 09:50:31,020 button 13252 09:50:31,020 --> 09:50:32,820 put equal and now let's create our 13253 09:50:32,820 --> 09:50:35,460 object type button 13254 09:50:35,460 --> 09:50:37,560 with parenthesis now let's pass some 13255 09:50:37,560 --> 09:50:39,840 values to the properties of the primary 13256 09:50:39,840 --> 09:50:41,160 Constructor 13257 09:50:41,160 --> 09:50:45,256 and this is gonna have the text to login 13258 09:50:45,360 --> 09:50:48,240 for the ID we type some arbitrary 13259 09:50:48,240 --> 09:50:50,880 numbers here one two one three 13260 09:50:50,880 --> 09:50:52,256 two 13261 09:50:52,256 --> 09:50:53,820 and um 13262 09:50:53,820 --> 09:50:56,720 now here you need to pass the one click 13263 09:50:56,720 --> 09:50:59,340 listener interface but do not we cannot 13264 09:50:59,340 --> 09:51:01,800 type here one click listener because 13265 09:51:01,800 --> 09:51:04,320 we cannot instantiate the interface so 13266 09:51:04,320 --> 09:51:08,096 what can we do to solve this problem 13267 09:51:08,096 --> 09:51:10,500 one solution to this would be to create 13268 09:51:10,500 --> 09:51:13,140 a separate class here 13269 09:51:13,140 --> 09:51:14,880 let's put it actually above the 13270 09:51:14,880 --> 09:51:17,299 interface 13271 09:51:17,520 --> 09:51:19,860 let's call it click listener this class 13272 09:51:19,860 --> 09:51:21,180 class 13273 09:51:21,180 --> 09:51:23,756 click listener and this class is going 13274 09:51:23,756 --> 09:51:25,860 to implement the interface 13275 09:51:25,860 --> 09:51:29,880 so we put here colon and we call our on 13276 09:51:29,880 --> 09:51:32,276 click listener interface put curly 13277 09:51:32,276 --> 09:51:33,240 braces 13278 09:51:33,240 --> 09:51:35,340 and now we need to implement that one 13279 09:51:35,340 --> 09:51:37,860 click method that one click function 13280 09:51:37,860 --> 09:51:39,720 that we defined actually 13281 09:51:39,720 --> 09:51:41,580 inside our own click listen interface so 13282 09:51:41,580 --> 09:51:44,936 press ok to override to add on click 13283 09:51:44,936 --> 09:51:46,256 function 13284 09:51:46,256 --> 09:51:48,300 and we have that 13285 09:51:48,300 --> 09:51:50,460 to do we have this to do which says to 13286 09:51:50,460 --> 09:51:52,680 do not yet implemented so this is there 13287 09:51:52,680 --> 09:51:55,320 to tell us that it's not implemented yet 13288 09:51:55,320 --> 09:51:58,820 so let's delete this 13289 09:51:59,460 --> 09:52:01,380 all right so what we could do is you can 13290 09:52:01,380 --> 09:52:04,740 create here an instance of the 13291 09:52:04,740 --> 09:52:09,000 click listener so we type here Val 13292 09:52:09,000 --> 09:52:12,060 click listener 13293 09:52:12,060 --> 09:52:15,300 equals create the instance we'll create 13294 09:52:15,300 --> 09:52:16,560 an object 13295 09:52:16,560 --> 09:52:18,480 and now you can pass here the click 13296 09:52:18,480 --> 09:52:20,900 listener 13297 09:52:21,596 --> 09:52:24,840 so now the arrow disappeared but if I 13298 09:52:24,840 --> 09:52:29,596 create a second object let's call it the 13299 09:52:31,500 --> 09:52:34,140 sign up button 13300 09:52:34,140 --> 09:52:37,096 equals button 13301 09:52:38,400 --> 09:52:40,500 let's pause the text so 13302 09:52:40,500 --> 09:52:42,980 sign up 13303 09:52:44,640 --> 09:52:48,416 for the ID was an arbitrary number 13304 09:52:48,416 --> 09:52:50,580 now 13305 09:52:50,580 --> 09:52:54,540 in our imaginary or 13306 09:52:54,540 --> 09:52:57,200 example that we have here 13307 09:52:57,200 --> 09:52:59,880 actually if you think for real scenario 13308 09:52:59,880 --> 09:53:02,220 with this button and the one click 13309 09:53:02,220 --> 09:53:03,840 listen what we want to achieve here is 13310 09:53:03,840 --> 09:53:05,756 that if you have a graphical user 13311 09:53:05,756 --> 09:53:06,840 interface 13312 09:53:06,840 --> 09:53:10,320 you want that you want to detect that 13313 09:53:10,320 --> 09:53:12,660 click so you want to declare detect one 13314 09:53:12,660 --> 09:53:14,936 that specific button let's see that this 13315 09:53:14,936 --> 09:53:16,500 button object that we have here is 13316 09:53:16,500 --> 09:53:20,880 linked to an uh graphical button which 13317 09:53:20,880 --> 09:53:22,860 on which you click 13318 09:53:22,860 --> 09:53:24,660 because they are linked when you click 13319 09:53:24,660 --> 09:53:26,640 on that you want to 13320 09:53:26,640 --> 09:53:30,000 call that one click function that is 13321 09:53:30,000 --> 09:53:32,276 defined inside our interface inside here 13322 09:53:32,276 --> 09:53:34,320 but you want to 13323 09:53:34,320 --> 09:53:38,276 make that one click interface we want 13324 09:53:38,276 --> 09:53:40,460 you want to make that one click function 13325 09:53:40,460 --> 09:53:43,140 particular to particular to that button 13326 09:53:43,140 --> 09:53:46,880 which was clicked so we we want to to 13327 09:53:46,880 --> 09:53:51,060 implement that uh one click function to 13328 09:53:51,060 --> 09:53:52,980 that particular button which was clicked 13329 09:53:52,980 --> 09:53:55,020 and the code that goes inside that 13330 09:53:55,020 --> 09:53:56,820 particular button so let's say that you 13331 09:53:56,820 --> 09:53:58,800 click on the login button it's 13332 09:53:58,800 --> 09:54:01,140 particular to that button so that in the 13333 09:54:01,140 --> 09:54:02,340 next 13334 09:54:02,340 --> 09:54:04,800 when the next button is clicked so when 13335 09:54:04,800 --> 09:54:07,256 if the user clicks for when the sign up 13336 09:54:07,256 --> 09:54:10,436 button then you know you need to also 13337 09:54:10,436 --> 09:54:12,720 have the one click 13338 09:54:12,720 --> 09:54:16,200 method called but that one click that 13339 09:54:16,200 --> 09:54:18,776 one click function actually I I keep 13340 09:54:18,776 --> 09:54:21,360 saying method but the one click function 13341 09:54:21,360 --> 09:54:23,220 should should have its own 13342 09:54:23,220 --> 09:54:25,500 implementation a particular to the sign 13343 09:54:25,500 --> 09:54:27,720 up button so what can we do in that case 13344 09:54:27,720 --> 09:54:30,120 we cannot pass the same instance here to 13345 09:54:30,120 --> 09:54:31,916 click listener because even if it 13346 09:54:31,916 --> 09:54:33,540 creates here another instance of the 13347 09:54:33,540 --> 09:54:35,640 click listener it will still have the 13348 09:54:35,640 --> 09:54:38,276 same code which is here so if you put 13349 09:54:38,276 --> 09:54:40,080 here some code this is code blue it will 13350 09:54:40,080 --> 09:54:42,720 be shared among all the objects that are 13351 09:54:42,720 --> 09:54:45,300 created so what you can do in this case 13352 09:54:45,300 --> 09:54:49,320 is to use an object expression 13353 09:54:49,320 --> 09:54:52,560 or also called anonymous 13354 09:54:52,560 --> 09:54:54,960 anonymous anonymous classes so what you 13355 09:54:54,960 --> 09:54:58,020 can do here we can type object 13356 09:54:58,020 --> 09:55:01,080 we put colon and now we 13357 09:55:01,080 --> 09:55:04,980 are inheriting from our we are actually 13358 09:55:04,980 --> 09:55:08,340 implementing our own click listener 13359 09:55:08,340 --> 09:55:11,220 interface so we type object colon on 13360 09:55:11,220 --> 09:55:13,680 click listener and we put curly braces 13361 09:55:13,680 --> 09:55:17,400 because here we declare and we also 13362 09:55:17,400 --> 09:55:21,240 instantiate the the anonymous class so 13363 09:55:21,240 --> 09:55:24,799 we need to implement that right there 13364 09:55:25,800 --> 09:55:28,980 so by doing this now we where we made 13365 09:55:28,980 --> 09:55:33,540 the one click function particular to 13366 09:55:33,540 --> 09:55:36,300 each object Which object button so we 13367 09:55:36,300 --> 09:55:38,220 can do the same thing here we type 13368 09:55:38,220 --> 09:55:40,640 object 13369 09:55:43,256 --> 09:55:46,096 and we Implement our on click listener 13370 09:55:46,096 --> 09:55:48,900 interface we put curly braces now we 13371 09:55:48,900 --> 09:55:50,640 need to implement 13372 09:55:50,640 --> 09:55:52,800 for this particular object for the sign 13373 09:55:52,800 --> 09:55:55,040 up 13374 09:55:55,580 --> 09:55:58,020 or button objects we need to implement 13375 09:55:58,020 --> 09:56:01,140 that function so if we type we click we 13376 09:56:01,140 --> 09:56:03,240 click on this object 13377 09:56:03,240 --> 09:56:06,120 we go to the red light bulb and we type 13378 09:56:06,120 --> 09:56:07,880 we press here where it says Implement 13379 09:56:07,880 --> 09:56:09,960 members so implement the one click 13380 09:56:09,960 --> 09:56:13,020 function and now what we did here is we 13381 09:56:13,020 --> 09:56:16,020 made the one click 13382 09:56:16,020 --> 09:56:19,200 function particular to each object so 13383 09:56:19,200 --> 09:56:21,180 each object now we can put here some 13384 09:56:21,180 --> 09:56:22,320 code 13385 09:56:22,320 --> 09:56:25,096 code 13386 09:56:25,860 --> 09:56:30,680 or we can type here this sign 13387 09:56:33,416 --> 09:56:35,460 in 13388 09:56:35,460 --> 09:56:38,520 actually let's type login 13389 09:56:38,520 --> 09:56:41,756 that was it so now we can have code here 13390 09:56:41,756 --> 09:56:44,040 which actually log logs in if you have a 13391 09:56:44,040 --> 09:56:46,140 real graphical user interface which logs 13392 09:56:46,140 --> 09:56:49,020 logs in the user and here you can also 13393 09:56:49,020 --> 09:56:51,360 code to sign up the user when that 13394 09:56:51,360 --> 09:56:54,916 specific button is clicked 13395 09:56:56,520 --> 09:56:58,436 so by doing by using the object 13396 09:56:58,436 --> 09:57:00,840 expression also called Anonymous class 13397 09:57:00,840 --> 09:57:03,416 because you don't have 13398 09:57:03,416 --> 09:57:06,300 reference you don't have a name for the 13399 09:57:06,300 --> 09:57:08,756 class here you just it's just Anonymous 13400 09:57:08,756 --> 09:57:10,500 for this 13401 09:57:10,500 --> 09:57:13,680 is anonymous so we cannot it does not 13402 09:57:13,680 --> 09:57:15,180 have a name so we cannot call it later 13403 09:57:15,180 --> 09:57:18,240 or do something because it's created and 13404 09:57:18,240 --> 09:57:20,640 also instantiating right there this is 13405 09:57:20,640 --> 09:57:21,900 why I need to implement the one click 13406 09:57:21,900 --> 09:57:23,460 function and now we made the one click 13407 09:57:23,460 --> 09:57:26,340 function particular to each button and 13408 09:57:26,340 --> 09:57:27,776 you have more 13409 09:57:27,776 --> 09:57:30,300 objects more objects of created with the 13410 09:57:30,300 --> 09:57:31,320 class 13411 09:57:31,320 --> 09:57:33,720 button they'll have their own 13412 09:57:33,720 --> 09:57:36,120 implementation of the one click 13413 09:57:36,120 --> 09:57:38,340 function which comes from the interface 13414 09:57:38,340 --> 09:57:40,256 on click listener 13415 09:57:40,256 --> 09:57:42,120 so this is why you need to use the 13416 09:57:42,120 --> 09:57:44,220 anonymous 13417 09:57:44,220 --> 09:57:46,680 class or also called object expression 13418 09:57:46,680 --> 09:57:49,080 and it's an expression because the value 13419 09:57:49,080 --> 09:57:52,580 that the because the object is always 13420 09:57:52,580 --> 09:57:56,276 it's created and instantiate it is 13421 09:57:56,276 --> 09:57:58,200 actually declared and instantiated at 13422 09:57:58,200 --> 09:58:01,080 the same time it's also assigned to the 13423 09:58:01,080 --> 09:58:03,300 property that we Define here so this is 13424 09:58:03,300 --> 09:58:06,060 why it's called an expression 13425 09:58:06,060 --> 09:58:09,060 so see you in the next video 13426 09:58:09,060 --> 09:58:10,620 all right so now it's time to start a 13427 09:58:10,620 --> 09:58:12,180 discussion about delegation but first 13428 09:58:12,180 --> 09:58:14,520 I'm going to add the main function here 13429 09:58:14,520 --> 09:58:17,880 now what is delegation delegation means 13430 09:58:17,880 --> 09:58:20,276 giving power given Authority from one 13431 09:58:20,276 --> 09:58:22,620 instance from one class to another class 13432 09:58:22,620 --> 09:58:26,580 and delegation is usually used in in 13433 09:58:26,580 --> 09:58:29,040 scenarios where inheritance starts to 13434 09:58:29,040 --> 09:58:32,040 break so when inheritance starts to 13435 09:58:32,040 --> 09:58:34,020 break when you want let's say it for 13436 09:58:34,020 --> 09:58:36,060 whatever reason to inherit from two 13437 09:58:36,060 --> 09:58:39,000 classes in that case you can't use 13438 09:58:39,000 --> 09:58:41,160 inheritance because with inheritance you 13439 09:58:41,160 --> 09:58:44,756 can you inherit just from only one class 13440 09:58:44,756 --> 09:58:47,756 but with delegation you can plug in 13441 09:58:47,756 --> 09:58:49,220 multiple 13442 09:58:49,220 --> 09:58:51,900 implementation of classes in your own 13443 09:58:51,900 --> 09:58:54,360 class and let's see how we can do that 13444 09:58:54,360 --> 09:58:55,560 so I'm going to create first an 13445 09:58:55,560 --> 09:58:58,980 interface called a and it's going to 13446 09:58:58,980 --> 09:59:02,520 have a function called print 13447 09:59:02,520 --> 09:59:03,720 then I'm going to create another 13448 09:59:03,720 --> 09:59:06,660 interface called B and it's going to 13449 09:59:06,660 --> 09:59:10,040 have a function print though 13450 09:59:11,340 --> 09:59:13,620 then I'm going to create a class so I'm 13451 09:59:13,620 --> 09:59:15,240 going to type here class and it's going 13452 09:59:15,240 --> 09:59:17,040 to be called first delegate and it's 13453 09:59:17,040 --> 09:59:19,256 going to implement the fun the interface 13454 09:59:19,256 --> 09:59:21,000 a here 13455 09:59:21,000 --> 09:59:23,040 and I'm gonna press Ctrl o to override 13456 09:59:23,040 --> 09:59:25,140 the function print here 13457 09:59:25,140 --> 09:59:27,020 and I'm going to create another class 13458 09:59:27,020 --> 09:59:30,840 called second delegate 13459 09:59:30,840 --> 09:59:32,640 and this is going to implement the 13460 09:59:32,640 --> 09:59:33,960 interface B 13461 09:59:33,960 --> 09:59:35,820 so I'm going to place Ctrl to override 13462 09:59:35,820 --> 09:59:36,776 the 13463 09:59:36,776 --> 09:59:39,540 function print tool 13464 09:59:39,540 --> 09:59:41,460 now I'm going to create up here a class 13465 09:59:41,460 --> 09:59:43,680 called up 13466 09:59:43,680 --> 09:59:46,320 so class up 13467 09:59:46,320 --> 09:59:48,416 now let's say that I want here to 13468 09:59:48,416 --> 09:59:51,000 inherit from the first delegate and from 13469 09:59:51,000 --> 09:59:52,380 the second regulator so I'm going to 13470 09:59:52,380 --> 09:59:54,776 inform the single delegate and if I type 13471 09:59:54,776 --> 09:59:57,120 here first delegate 13472 09:59:57,120 --> 09:59:59,520 I put parenthesis opening parentheses 13473 09:59:59,520 --> 10:00:01,860 closing parentheses 13474 10:00:01,860 --> 10:00:04,916 if I put curly braces now I have another 13475 10:00:04,916 --> 10:00:07,276 line because I should Mark this as open 13476 10:00:07,276 --> 10:00:11,520 so mark this as open and also this as 13477 10:00:11,520 --> 10:00:15,000 open because I want to show you that so 13478 10:00:15,000 --> 10:00:17,096 now if I want to inherit from the second 13479 10:00:17,096 --> 10:00:19,680 class called from the second delegate 13480 10:00:19,680 --> 10:00:21,596 because let's say that I want to use the 13481 10:00:21,596 --> 10:00:24,120 the print to function in my class for 13482 10:00:24,120 --> 10:00:25,916 whatever reason 13483 10:00:25,916 --> 10:00:28,860 I can't because I can't only inherit 13484 10:00:28,860 --> 10:00:31,320 from one class so if I hover over here 13485 10:00:31,320 --> 10:00:34,020 over this underline it says only only 13486 10:00:34,020 --> 10:00:36,060 one class may appear in a super type 13487 10:00:36,060 --> 10:00:37,740 list so in other words saying that you 13488 10:00:37,740 --> 10:00:40,380 can only inherit from one class and that 13489 10:00:40,380 --> 10:00:43,080 is all we cannot inherit from multiple 13490 10:00:43,080 --> 10:00:46,200 classes now with delegation we can plug 13491 10:00:46,200 --> 10:00:49,080 in multiple implementations in our own 13492 10:00:49,080 --> 10:00:52,800 class so what I can do here I can say 13493 10:00:52,800 --> 10:00:54,060 hey 13494 10:00:54,060 --> 10:00:56,460 implement the interface a so I'm going 13495 10:00:56,460 --> 10:00:58,680 to inherit it from the interface a but 13496 10:00:58,680 --> 10:01:01,380 you're gonna say use for the interface a 13497 10:01:01,380 --> 10:01:04,140 the implementation of the first delegate 13498 10:01:04,140 --> 10:01:05,936 class so this is what we're saying here 13499 10:01:05,936 --> 10:01:09,660 a and the keyword by first delegate 13500 10:01:09,660 --> 10:01:12,596 we're saying hey use for the interface a 13501 10:01:12,596 --> 10:01:15,240 the implementation which is provided by 13502 10:01:15,240 --> 10:01:17,400 the first delegate class and that will 13503 10:01:17,400 --> 10:01:18,240 work 13504 10:01:18,240 --> 10:01:19,916 then I'm going to put a comma here and 13505 10:01:19,916 --> 10:01:22,800 I'm going to say use also for the class 13506 10:01:22,800 --> 10:01:25,380 B so I'm going to type for the interface 13507 10:01:25,380 --> 10:01:28,500 B so we're gonna type B and we're gonna 13508 10:01:28,500 --> 10:01:30,360 use the by key you're also going to type 13509 10:01:30,360 --> 10:01:31,980 here by 13510 10:01:31,980 --> 10:01:34,436 second delegate so you're saying use for 13511 10:01:34,436 --> 10:01:37,800 the interface B the second delegate 13512 10:01:37,800 --> 10:01:40,380 implementation and it's gonna use that 13513 10:01:40,380 --> 10:01:42,180 and it's gonna be him 13514 10:01:42,180 --> 10:01:43,916 happy with that so you're gonna see that 13515 10:01:43,916 --> 10:01:47,340 we get no error and now I can override 13516 10:01:47,340 --> 10:01:50,460 here both the print function and also 13517 10:01:50,460 --> 10:01:52,916 the print to function so if I press Ctrl 13518 10:01:52,916 --> 10:01:55,140 o now I can override also the print too 13519 10:01:55,140 --> 10:01:58,080 and this is inherently very powerful 13520 10:01:58,080 --> 10:02:00,660 because we can plug in here multiple as 13521 10:02:00,660 --> 10:02:03,300 many implementations as you want in your 13522 10:02:03,300 --> 10:02:07,256 class so this is in the if you think 13523 10:02:07,256 --> 10:02:10,500 about in contrast with the inheritance 13524 10:02:10,500 --> 10:02:12,840 this is very powerful because here you 13525 10:02:12,840 --> 10:02:15,300 can plug in as many implementations as 13526 10:02:15,300 --> 10:02:18,240 you want and this is very powerful so 13527 10:02:18,240 --> 10:02:20,040 this is how you can use the delegation 13528 10:02:20,040 --> 10:02:21,720 in kotlin and next we're going to see 13529 10:02:21,720 --> 10:02:23,096 how you can use delegation with 13530 10:02:23,096 --> 10:02:24,596 properties 13531 10:02:24,596 --> 10:02:26,756 so to use delegation on properties first 13532 10:02:26,756 --> 10:02:28,380 I'm going to paste some code here so I'm 13533 10:02:28,380 --> 10:02:30,060 going to paste this code here and what 13534 10:02:30,060 --> 10:02:31,916 we are having here is a class format 13535 10:02:31,916 --> 10:02:33,776 delegate which is inheriting from the 13536 10:02:33,776 --> 10:02:35,580 read and write property 13537 10:02:35,580 --> 10:02:38,040 and it's overriding the get value and 13538 10:02:38,040 --> 10:02:40,320 the set value so we are overriding the 13539 10:02:40,320 --> 10:02:42,180 setter and the getter inside our class 13540 10:02:42,180 --> 10:02:44,276 and we provide our own implementation so 13541 10:02:44,276 --> 10:02:46,140 whenever you 13542 10:02:46,140 --> 10:02:48,240 you use the setter to set a value or to 13543 10:02:48,240 --> 10:02:51,120 get as a get a value we we are 13544 10:02:51,120 --> 10:02:52,620 overriding those functions and we 13545 10:02:52,620 --> 10:02:54,660 provide our own implementation next I'm 13546 10:02:54,660 --> 10:02:56,400 going to create here up here a class but 13547 10:02:56,400 --> 10:02:58,436 first I'm going to delete the code and 13548 10:02:58,436 --> 10:03:00,060 the interface that we have here because 13549 10:03:00,060 --> 10:03:01,740 we don't need this code 13550 10:03:01,740 --> 10:03:03,416 so I'm gonna delete this code and I'm 13551 10:03:03,416 --> 10:03:05,936 going to create another class 13552 10:03:05,936 --> 10:03:07,500 and this class is going to be called 13553 10:03:07,500 --> 10:03:10,620 user I'm going to put curly braces and 13554 10:03:10,620 --> 10:03:11,700 I'm going to define the properties 13555 10:03:11,700 --> 10:03:12,840 inside the class so I'm going to type 13556 10:03:12,840 --> 10:03:14,340 here VAR 13557 10:03:14,340 --> 10:03:17,580 first name so what we are doing now is 13558 10:03:17,580 --> 10:03:19,916 we're creating a property first it's 13559 10:03:19,916 --> 10:03:21,360 called first name and I'm going to use 13560 10:03:21,360 --> 10:03:23,520 the by queue we're just going to say buy 13561 10:03:23,520 --> 10:03:25,560 format delegate so what we are saying 13562 10:03:25,560 --> 10:03:28,160 now is use the 13563 10:03:28,160 --> 10:03:31,740 use the the we use the code which is 13564 10:03:31,740 --> 10:03:33,660 inside the format delegate Whenever you 13565 10:03:33,660 --> 10:03:35,460 set the value or get the value of the 13566 10:03:35,460 --> 10:03:36,776 first name I'm going to put the 13567 10:03:36,776 --> 10:03:39,180 parenthesis here next I'm going to type 13568 10:03:39,180 --> 10:03:42,000 another VAR last name and I'm going to 13569 10:03:42,000 --> 10:03:44,220 type here again bye and I'm going to 13570 10:03:44,220 --> 10:03:47,640 type format delegate so format delegate 13571 10:03:47,640 --> 10:03:49,380 and we put parentheses 13572 10:03:49,380 --> 10:03:51,480 so not now what is going to happen is 13573 10:03:51,480 --> 10:03:54,180 that whenever you set the first name 13574 10:03:54,180 --> 10:03:55,916 properly or we get the name the first 13575 10:03:55,916 --> 10:03:57,240 name property is going to use our 13576 10:03:57,240 --> 10:03:59,096 implementation that we provided here 13577 10:03:59,096 --> 10:04:00,840 this is what you're gonna do and the 13578 10:04:00,840 --> 10:04:02,220 same it will be true for the last name 13579 10:04:02,220 --> 10:04:04,080 because we are using the 13580 10:04:04,080 --> 10:04:07,080 delegation here on properties so I'm 13581 10:04:07,080 --> 10:04:08,520 going to create here a user so I'm going 13582 10:04:08,520 --> 10:04:09,900 to type here Val 13583 10:04:09,900 --> 10:04:14,640 user want to type equals to user 13584 10:04:14,640 --> 10:04:17,640 and I'm gonna type here with 13585 10:04:17,640 --> 10:04:20,936 I'm gonna type user this is the 13586 10:04:20,936 --> 10:04:22,740 scope function I'm going to put curly 13587 10:04:22,740 --> 10:04:24,840 curly braces and I'm going to type here 13588 10:04:24,840 --> 10:04:27,540 first name 13589 10:04:27,540 --> 10:04:29,936 equals Alex 13590 10:04:29,936 --> 10:04:32,276 and the last name 13591 10:04:32,276 --> 10:04:34,560 equals uh 13592 10:04:34,560 --> 10:04:36,120 dominga 13593 10:04:36,120 --> 10:04:37,980 now if I output those values in the 13594 10:04:37,980 --> 10:04:39,540 console look what happens so if I tap it 13595 10:04:39,540 --> 10:04:42,380 here print line 13596 10:04:42,620 --> 10:04:45,840 user that first name 13597 10:04:45,840 --> 10:04:48,680 and print line let's actually use again 13598 10:04:48,680 --> 10:04:53,460 with the scope function type here with 13599 10:04:53,460 --> 10:04:57,960 just pass here user curly braces 13600 10:04:57,960 --> 10:05:00,180 print line 13601 10:05:00,180 --> 10:05:01,680 first name 13602 10:05:01,680 --> 10:05:03,840 and print line 13603 10:05:03,840 --> 10:05:05,400 last name 13604 10:05:05,400 --> 10:05:06,900 now if you run this code you're gonna 13605 10:05:06,900 --> 10:05:07,860 see 13606 10:05:07,860 --> 10:05:11,276 that it's using the overriding functions 13607 10:05:11,276 --> 10:05:13,380 that we provide in our format delegate 13608 10:05:13,380 --> 10:05:15,180 so we have in uppercase letters like we 13609 10:05:15,180 --> 10:05:17,700 defined here we're gonna wanna whenever 13610 10:05:17,700 --> 10:05:20,160 we set about is going to be set it with 13611 10:05:20,160 --> 10:05:21,660 the it's going to change the value to 13612 10:05:21,660 --> 10:05:24,540 lowercase letter so get Alex and obinka 13613 10:05:24,540 --> 10:05:27,120 in lowercase letters so this is how you 13614 10:05:27,120 --> 10:05:30,596 can use the delegation on properties and 13615 10:05:30,596 --> 10:05:34,096 see you in the next video 13616 10:05:37,500 --> 10:05:39,416 so now it's time to start a discussion 13617 10:05:39,416 --> 10:05:41,276 about collections but first i'm going to 13618 10:05:41,276 --> 10:05:43,020 create a new project I'm going to call 13619 10:05:43,020 --> 10:05:45,916 it collections 13620 10:05:46,380 --> 10:05:48,300 for the language select link for the 13621 10:05:48,300 --> 10:05:50,096 build system IntelliJ make sure to have 13622 10:05:50,096 --> 10:05:52,560 the jdk selected and also check this box 13623 10:05:52,560 --> 10:05:54,720 to have the main function Auto generated 13624 10:05:54,720 --> 10:05:56,580 for us so I'm going to click on create 13625 10:05:56,580 --> 10:05:59,416 to create the project 13626 10:06:01,080 --> 10:06:02,340 now I'm going to delete this code 13627 10:06:02,340 --> 10:06:04,080 because you don't need this code 13628 10:06:04,080 --> 10:06:05,520 and also I'm going to hide the project 13629 10:06:05,520 --> 10:06:06,720 pane 13630 10:06:06,720 --> 10:06:09,180 now what are collections collections are 13631 10:06:09,180 --> 10:06:12,120 a group of objects stored together in a 13632 10:06:12,120 --> 10:06:14,096 single variable they are similar to 13633 10:06:14,096 --> 10:06:16,500 array but there are there are different 13634 10:06:16,500 --> 10:06:19,500 in some sense and 13635 10:06:19,500 --> 10:06:21,540 they are mainly three collections that 13636 10:06:21,540 --> 10:06:23,400 we're gonna look at and this those are 13637 10:06:23,400 --> 10:06:28,020 the list the set and the map and the and 13638 10:06:28,020 --> 10:06:29,720 those among those 13639 10:06:29,720 --> 10:06:32,400 collections they are separated in two 13640 10:06:32,400 --> 10:06:36,436 groups so there are multiple multiple 13641 10:06:36,436 --> 10:06:40,436 lists and set air maps and immutable 13642 10:06:40,436 --> 10:06:44,520 lists set and map and in the motable 13643 10:06:44,520 --> 10:06:48,000 means that you can write to that 13644 10:06:48,000 --> 10:06:51,240 collection so you can add or or remove 13645 10:06:51,240 --> 10:06:53,936 elements to it so the that collection 13646 10:06:53,936 --> 10:06:56,820 can grow or Shrink as you add or remove 13647 10:06:56,820 --> 10:06:59,880 elements to it and immutable means that 13648 10:06:59,880 --> 10:07:03,720 you can only add elements when it when 13649 10:07:03,720 --> 10:07:05,580 you instantiate that or we're going to 13650 10:07:05,580 --> 10:07:07,620 create that collection you cannot add 13651 10:07:07,620 --> 10:07:10,500 later elements to it the size is fixed 13652 10:07:10,500 --> 10:07:12,900 so we cannot add or remove element so 13653 10:07:12,900 --> 10:07:14,700 this is the difference between immutable 13654 10:07:14,700 --> 10:07:16,916 and multiple and we're gonna look at all 13655 10:07:16,916 --> 10:07:19,500 of those in this video so we're gonna 13656 10:07:19,500 --> 10:07:21,180 look at the list collection first and 13657 10:07:21,180 --> 10:07:23,160 we're gonna look at an immutable list so 13658 10:07:23,160 --> 10:07:25,320 we type Val I'm going to create a 13659 10:07:25,320 --> 10:07:28,740 variable names and here we're gonna type 13660 10:07:28,740 --> 10:07:32,220 to create an immutable list type list of 13661 10:07:32,220 --> 10:07:34,140 and here we need to specify the type 13662 10:07:34,140 --> 10:07:37,200 what kind of data I'm going to put here 13663 10:07:37,200 --> 10:07:41,096 and do type name one 13664 10:07:41,096 --> 10:07:43,820 name two 13665 10:07:46,256 --> 10:07:49,020 and name three now we cannot add or 13666 10:07:49,020 --> 10:07:51,300 remove elements from this list because 13667 10:07:51,300 --> 10:07:53,340 it's an immutable list but we can get on 13668 10:07:53,340 --> 10:07:55,320 Elemental output in the console and to 13669 10:07:55,320 --> 10:07:57,660 do that to type it in the line 13670 10:07:57,660 --> 10:08:00,540 we type names we put square brackets and 13671 10:08:00,540 --> 10:08:02,460 we get the element by its index and the 13672 10:08:02,460 --> 10:08:04,500 index also starts at zero so let's get 13673 10:08:04,500 --> 10:08:06,540 the first element 13674 10:08:06,540 --> 10:08:08,400 so if you're on this code you'll see 13675 10:08:08,400 --> 10:08:12,320 name one I'll put it in a new console 13676 10:08:13,800 --> 10:08:16,140 so you get name one so this is an 13677 10:08:16,140 --> 10:08:17,880 immutable list we cannot add or remove 13678 10:08:17,880 --> 10:08:20,416 elements from it after we 13679 10:08:20,416 --> 10:08:23,880 instantiate it here what now now let's 13680 10:08:23,880 --> 10:08:27,240 look at an mutably so a list which can 13681 10:08:27,240 --> 10:08:29,040 within which you can add and remove 13682 10:08:29,040 --> 10:08:31,916 elements to it so to do that we just 13683 10:08:31,916 --> 10:08:34,500 change this from a list of to a mutable 13684 10:08:34,500 --> 10:08:35,640 list of 13685 10:08:35,640 --> 10:08:37,980 so we type here multiple 13686 10:08:37,980 --> 10:08:40,140 list of 13687 10:08:40,140 --> 10:08:43,980 we can also remove the explicit type 13688 10:08:43,980 --> 10:08:46,436 here the explicit type declaration 13689 10:08:46,436 --> 10:08:48,360 because it's gray out 13690 10:08:48,360 --> 10:08:49,860 so remove that because it can be 13691 10:08:49,860 --> 10:08:53,096 inferred and to this mutable list now we 13692 10:08:53,096 --> 10:08:56,220 can add or remove the elements so I can 13693 10:08:56,220 --> 10:08:58,080 type here names 13694 10:08:58,080 --> 10:09:00,840 and we have this add function so you can 13695 10:09:00,840 --> 10:09:02,520 type add 13696 10:09:02,520 --> 10:09:05,460 and here we can 13697 10:09:05,460 --> 10:09:07,500 you can pass a new element so we pass 13698 10:09:07,500 --> 10:09:10,580 here let's say name 13699 10:09:10,580 --> 10:09:14,580 4. now if I uh let's type here names 13700 10:09:14,580 --> 10:09:17,700 that for each 13701 10:09:17,700 --> 10:09:20,596 print line 13702 10:09:21,660 --> 10:09:24,416 not in this footage 13703 10:09:24,416 --> 10:09:26,096 name start 13704 10:09:26,096 --> 10:09:29,756 for each this for each 13705 10:09:29,756 --> 10:09:32,596 opening the line 13706 10:09:34,140 --> 10:09:38,060 now if you run this 13707 10:09:38,756 --> 10:09:40,860 now you see you have name one name two 13708 10:09:40,860 --> 10:09:42,776 name three and name four so name four 13709 10:09:42,776 --> 10:09:44,520 was added to our Multiple List because 13710 10:09:44,520 --> 10:09:46,320 our list is multiple we can add or 13711 10:09:46,320 --> 10:09:50,096 remove new elements so down here I can 13712 10:09:50,096 --> 10:09:54,240 I can type names dot remove 13713 10:09:54,240 --> 10:09:56,520 and we can remove an element and we can 13714 10:09:56,520 --> 10:09:59,096 remove the element I think by 13715 10:09:59,096 --> 10:10:00,900 you can remove you can use this function 13716 10:10:00,900 --> 10:10:02,700 remove art so we can remove the element 13717 10:10:02,700 --> 10:10:05,096 by its index so we can type here the 13718 10:10:05,096 --> 10:10:06,180 index 13719 10:10:06,180 --> 10:10:07,860 and that is going to remove the first 13720 10:10:07,860 --> 10:10:11,480 element so if you run this code 13721 10:10:12,060 --> 10:10:15,860 name one is not going to be showed 13722 10:10:17,276 --> 10:10:19,436 so as you can see name one was removed 13723 10:10:19,436 --> 10:10:21,240 because you can you will remove it by 13724 10:10:21,240 --> 10:10:23,460 its index and we can also remove an 13725 10:10:23,460 --> 10:10:24,416 element 13726 10:10:24,416 --> 10:10:27,960 using this function by typing uh 13727 10:10:27,960 --> 10:10:30,060 the elements so we type here let's say 13728 10:10:30,060 --> 10:10:32,400 name 13729 10:10:32,400 --> 10:10:34,080 two 13730 10:10:34,080 --> 10:10:35,756 name two elements and now it's going to 13731 10:10:35,756 --> 10:10:39,860 be removed so now if you run this 13732 10:10:41,580 --> 10:10:43,560 name two was removed so those are two 13733 10:10:43,560 --> 10:10:45,060 functions with which you can remove 13734 10:10:45,060 --> 10:10:47,756 elements from the Multiple List so this 13735 10:10:47,756 --> 10:10:49,916 is a multiple list you can add and 13736 10:10:49,916 --> 10:10:51,960 remove elements to it and the list is 13737 10:10:51,960 --> 10:10:54,840 gonna grow and Shrink as you add or 13738 10:10:54,840 --> 10:10:56,580 remove elements to it so this is the 13739 10:10:56,580 --> 10:10:57,900 Multiple List 13740 10:10:57,900 --> 10:10:59,400 now 13741 10:10:59,400 --> 10:11:01,860 we're gonna look at a set and I set this 13742 10:11:01,860 --> 10:11:04,680 a collection which can store only unique 13743 10:11:04,680 --> 10:11:06,060 unique elements so you cannot have 13744 10:11:06,060 --> 10:11:10,860 duplicates so if I delete this 13745 10:11:10,860 --> 10:11:13,500 so in our 13746 10:11:13,500 --> 10:11:16,080 in our Multiple List you can put an 13747 10:11:16,080 --> 10:11:19,880 element which is the same 13748 10:11:20,580 --> 10:11:22,256 so we can have two elements which are 13749 10:11:22,256 --> 10:11:26,096 the same so if you run this 13750 10:11:26,096 --> 10:11:28,620 we have two elements which are the same 13751 10:11:28,620 --> 10:11:31,020 going to see name one at the end I have 13752 10:11:31,020 --> 10:11:32,580 name one name two name three and name 13753 10:11:32,580 --> 10:11:35,640 one again so you can have uh duplicates 13754 10:11:35,640 --> 10:11:37,980 in our collection on our multiple list 13755 10:11:37,980 --> 10:11:41,460 but in a set you can't so but first 13756 10:11:41,460 --> 10:11:43,380 we're gonna look at uh first we're gonna 13757 10:11:43,380 --> 10:11:45,960 look at the immutable set and then add 13758 10:11:45,960 --> 10:11:50,240 automotive ball set so we type here uh 13759 10:11:51,000 --> 10:11:53,936 let's delete this 13760 10:11:53,936 --> 10:11:57,596 we type here set off 13761 10:11:57,596 --> 10:12:01,560 and here you need to buy string 13762 10:12:01,560 --> 10:12:05,000 now if you run this 13763 10:12:05,880 --> 10:12:08,460 you will see that name one is not shown 13764 10:12:08,460 --> 10:12:09,776 two times here it's going to be shown 13765 10:12:09,776 --> 10:12:11,340 only one time so you have name one name 13766 10:12:11,340 --> 10:12:13,916 two and name three this is because the 13767 10:12:13,916 --> 10:12:17,820 set Seto the set is not allowing 13768 10:12:17,820 --> 10:12:21,960 duplicates in its uh in its list so 13769 10:12:21,960 --> 10:12:24,596 and this is a new multiple set set off 13770 10:12:24,596 --> 10:12:28,200 so if I type here names dot add we 13771 10:12:28,200 --> 10:12:30,660 cannot add or remove new elements but 13772 10:12:30,660 --> 10:12:32,220 you can also use 13773 10:12:32,220 --> 10:12:35,276 a multiple set of so we type here 13774 10:12:35,276 --> 10:12:38,540 Moto motable 13775 10:12:40,500 --> 10:12:42,360 set off 13776 10:12:42,360 --> 10:12:44,400 and now we can add and remove new 13777 10:12:44,400 --> 10:12:47,460 elements so you can type names that add 13778 10:12:47,460 --> 10:12:49,320 so here you can add a new element let's 13779 10:12:49,320 --> 10:12:50,640 say 13780 10:12:50,640 --> 10:12:53,300 name 13781 10:12:56,580 --> 10:12:59,900 so now if you run this 13782 10:13:03,240 --> 10:13:05,580 I'm going to name one name two name 13783 10:13:05,580 --> 10:13:07,080 three name four 13784 10:13:07,080 --> 10:13:10,140 and our duplicate is not strong because 13785 10:13:10,140 --> 10:13:13,200 I said a set can only 13786 10:13:13,200 --> 10:13:15,240 can only store unique elements not 13787 10:13:15,240 --> 10:13:18,060 duplicates but if I add objects that we 13788 10:13:18,060 --> 10:13:19,740 create look what happens so let's create 13789 10:13:19,740 --> 10:13:21,900 some user objects but first let's create 13790 10:13:21,900 --> 10:13:24,480 the class user 13791 10:13:24,480 --> 10:13:26,220 and it's gonna have a 13792 10:13:26,220 --> 10:13:30,360 Val name is going to be of type string 13793 10:13:30,360 --> 10:13:32,580 no let's create some user objects here 13794 10:13:32,580 --> 10:13:35,340 and we're gonna add to our multiple set 13795 10:13:35,340 --> 10:13:36,660 so we type here 13796 10:13:36,660 --> 10:13:39,720 wall user one 13797 10:13:39,720 --> 10:13:42,540 I'm gonna type here 13798 10:13:42,540 --> 10:13:44,820 user 13799 10:13:44,820 --> 10:13:46,500 I'm going to pass some generic names 13800 10:13:46,500 --> 10:13:49,460 like 13801 10:13:51,360 --> 10:13:53,880 name one and let's control the a few 13802 10:13:53,880 --> 10:13:55,320 times 13803 10:13:55,320 --> 10:13:58,680 let's change this to name two 13804 10:13:58,680 --> 10:13:59,700 five 13805 10:13:59,700 --> 10:14:03,500 and let's add here a Val 13806 10:14:05,040 --> 10:14:07,200 user 13807 10:14:07,200 --> 10:14:08,596 six 13808 10:14:08,596 --> 10:14:11,160 equals user 13809 10:14:11,160 --> 10:14:14,580 and let's type here Alex 13810 10:14:14,580 --> 10:14:16,860 let's press Ctrl d 13811 10:14:16,860 --> 10:14:18,840 and let's change this to 13812 10:14:18,840 --> 10:14:20,640 user 7. 13813 10:14:20,640 --> 10:14:23,520 now let's add those to our 13814 10:14:23,520 --> 10:14:25,740 multiple cells so let's change now the 13815 10:14:25,740 --> 10:14:27,979 type 13816 10:14:28,560 --> 10:14:33,079 so it's gonna store user objects 13817 10:14:36,256 --> 10:14:40,220 so we're gonna pass here 13818 10:14:42,120 --> 10:14:44,820 user one I'm going to speed up now this 13819 10:14:44,820 --> 10:14:46,916 a little bit 13820 10:14:46,916 --> 10:14:49,080 I'm gonna press Ctrl tilde format the 13821 10:14:49,080 --> 10:14:51,200 code 13822 10:14:51,300 --> 10:14:53,700 now we can add this we cannot add here a 13823 10:14:53,700 --> 10:14:57,416 name for because now the type is user 13824 10:14:57,416 --> 10:14:59,276 now if you're on this let's actually 13825 10:14:59,276 --> 10:15:04,020 change this print to it dot name 13826 10:15:04,020 --> 10:15:07,880 and let's let's run our code now 13827 10:15:08,640 --> 10:15:10,980 and we have name one name two name three 13828 10:15:10,980 --> 10:15:13,256 name four Nerf name five and have Alex 13829 10:15:13,256 --> 10:15:15,180 two times so why you have duplicates 13830 10:15:15,180 --> 10:15:18,060 even though I said that sets can only 13831 10:15:18,060 --> 10:15:19,740 store unique elements this is because 13832 10:15:19,740 --> 10:15:23,040 previously the set actually what is 13833 10:15:23,040 --> 10:15:26,460 doing to determine to if to if it has 13834 10:15:26,460 --> 10:15:28,500 duplicates is using the equals to 13835 10:15:28,500 --> 10:15:30,120 function that we talked about in our 13836 10:15:30,120 --> 10:15:32,580 video about data classes so when we 13837 10:15:32,580 --> 10:15:36,416 typed only strings then it was using the 13838 10:15:36,416 --> 10:15:39,120 equals to function which was overridden 13839 10:15:39,120 --> 10:15:42,060 inside the string class to determine if 13840 10:15:42,060 --> 10:15:46,080 there are uh if there are equal strings 13841 10:15:46,080 --> 10:15:48,596 but now because our class doesn't have 13842 10:15:48,596 --> 10:15:50,756 the equals to function over it then it's 13843 10:15:50,756 --> 10:15:54,240 in its class it's not it's using the 13844 10:15:54,240 --> 10:15:56,340 equals to function which is inside the 13845 10:15:56,340 --> 10:15:58,800 ending class so you cannot determine if 13846 10:15:58,800 --> 10:16:02,220 two users are are equal and to change 13847 10:16:02,220 --> 10:16:05,820 that just let's hide the console 13848 10:16:05,820 --> 10:16:08,340 and just put the data keyword here 13849 10:16:08,340 --> 10:16:10,680 because now this will generate the 13850 10:16:10,680 --> 10:16:13,436 equals to function the the the two 13851 10:16:13,436 --> 10:16:14,936 string and 13852 10:16:14,936 --> 10:16:19,096 the harsh code so now if you run this 13853 10:16:24,300 --> 10:16:26,276 now we have Alex only one time because 13854 10:16:26,276 --> 10:16:27,900 now it's using the equals to function 13855 10:16:27,900 --> 10:16:32,040 which was uh Auto generated by by uh by 13856 10:16:32,040 --> 10:16:34,436 the class user because you put here the 13857 10:16:34,436 --> 10:16:38,400 data key or so this is a good thing to 13858 10:16:38,400 --> 10:16:39,900 have in mind that 13859 10:16:39,900 --> 10:16:42,060 this model set is using the equals to 13860 10:16:42,060 --> 10:16:44,460 function to determine if the object that 13861 10:16:44,460 --> 10:16:48,240 you add to to yourself are equal so 13862 10:16:48,240 --> 10:16:51,240 having this in mind now let's look at 13863 10:16:51,240 --> 10:16:55,256 Maps so I'm going to close this 13864 10:16:55,256 --> 10:16:58,460 I'm gonna delete this 13865 10:16:59,276 --> 10:17:01,916 let's delete this 13866 10:17:01,916 --> 10:17:05,416 let's delete this also 13867 10:17:09,120 --> 10:17:10,620 now let's look at the next collection 13868 10:17:10,620 --> 10:17:12,240 that you can use in kotlin and that is 13869 10:17:12,240 --> 10:17:13,860 the map collection and use collection 13870 10:17:13,860 --> 10:17:16,560 when you want to store key value pairs 13871 10:17:16,560 --> 10:17:18,180 so I'm going to create a variable it's 13872 10:17:18,180 --> 10:17:20,040 going to be called users 13873 10:17:20,040 --> 10:17:22,436 and I'm going to put equals and I'm 13874 10:17:22,436 --> 10:17:24,240 going to create first animated bottom up 13875 10:17:24,240 --> 10:17:27,120 so we type map off 13876 10:17:27,120 --> 10:17:28,380 map 13877 10:17:28,380 --> 10:17:32,160 of and here you need to specify what 13878 10:17:32,160 --> 10:17:34,080 kind of type is going to be the key so 13879 10:17:34,080 --> 10:17:35,400 the key is going to be an integer but 13880 10:17:35,400 --> 10:17:37,380 you can put any class you want here 13881 10:17:37,380 --> 10:17:39,660 and the value is going to be a string so 13882 10:17:39,660 --> 10:17:42,060 I'm going to store the ID and the name 13883 10:17:42,060 --> 10:17:43,916 of an user 13884 10:17:43,916 --> 10:17:47,400 and to Define our element in pairs here 13885 10:17:47,400 --> 10:17:49,980 we type first the key and then we use 13886 10:17:49,980 --> 10:17:54,360 the to keyword to map our kit to its 13887 10:17:54,360 --> 10:17:57,660 element so 2 is value actually so type 1 13888 10:17:57,660 --> 10:18:00,960 2 and now the value let's first type 13889 10:18:00,960 --> 10:18:02,580 Maria so 13890 10:18:02,580 --> 10:18:07,080 now we have one element stored in paired 13891 10:18:07,080 --> 10:18:09,680 and the key is one the value is Maria 13892 10:18:09,680 --> 10:18:12,596 next I'm gonna do the same for the next 13893 10:18:12,596 --> 10:18:14,580 one so I'm gonna type here 13894 10:18:14,580 --> 10:18:16,436 two 13895 10:18:16,436 --> 10:18:17,820 two 13896 10:18:17,820 --> 10:18:19,916 the keyword tool 13897 10:18:19,916 --> 10:18:21,180 Alex 13898 10:18:21,180 --> 10:18:23,880 and then 13899 10:18:23,880 --> 10:18:26,276 I'm gonna type three 13900 10:18:26,276 --> 10:18:28,200 the keyword tool 13901 10:18:28,200 --> 10:18:30,300 so inside you need to type the value now 13902 10:18:30,300 --> 10:18:33,740 now type John 13903 10:18:35,880 --> 10:18:39,240 now to get one element from this map 13904 10:18:39,240 --> 10:18:41,640 with I print line 13905 10:18:41,640 --> 10:18:43,916 we type here users 13906 10:18:43,916 --> 10:18:47,060 square brackets 13907 10:18:47,756 --> 10:18:50,220 and we type we get the element by its 13908 10:18:50,220 --> 10:18:53,400 key as you can see by this hint key so 13909 10:18:53,400 --> 10:18:57,300 we get let's say the element one 13910 10:18:57,300 --> 10:19:00,740 so now if you run this code 13911 10:19:04,916 --> 10:19:07,800 we get Maria because Maria has because 13912 10:19:07,800 --> 10:19:11,220 this key code is for the for the key one 13913 10:19:11,220 --> 10:19:13,620 the value mapped the value which is 13914 10:19:13,620 --> 10:19:16,080 corresponding to this key is Maria and 13915 10:19:16,080 --> 10:19:18,900 if you change this to two 13916 10:19:18,900 --> 10:19:23,480 gonna get Alex if you run this 13917 10:19:26,640 --> 10:19:29,096 so this is an immutable map in this map 13918 10:19:29,096 --> 10:19:31,380 you cannot add or remove elements but if 13919 10:19:31,380 --> 10:19:33,000 I declare this as 13920 10:19:33,000 --> 10:19:35,340 a multiple map so let's change this to a 13921 10:19:35,340 --> 10:19:36,900 multiple map 13922 10:19:36,900 --> 10:19:39,180 so we type here mutable 13923 10:19:39,180 --> 10:19:41,960 map off 13924 10:19:42,660 --> 10:19:45,980 let's delete this from here 13925 10:19:47,756 --> 10:19:52,256 and let's pass here and 13926 10:19:52,256 --> 10:19:54,960 and string 13927 10:19:54,960 --> 10:19:58,340 now here now 13928 10:19:58,860 --> 10:20:02,840 so you need to put this in parenthesis 13929 10:20:03,120 --> 10:20:05,276 here we can add new elements so we can 13930 10:20:05,276 --> 10:20:08,180 type users 13931 10:20:09,120 --> 10:20:11,720 actually first let's Loop through our 13932 10:20:11,720 --> 10:20:13,740 mob to see 13933 10:20:13,740 --> 10:20:16,256 our elements and I'm going to use the 13934 10:20:16,256 --> 10:20:17,756 for each function to Loop so we type 13935 10:20:17,756 --> 10:20:21,900 here users dot for each 13936 10:20:21,900 --> 10:20:25,276 and we choose this one 13937 10:20:25,980 --> 10:20:28,436 and inside our Rook without we are the 13938 10:20:28,436 --> 10:20:29,640 print line 13939 10:20:29,640 --> 10:20:31,200 and 13940 10:20:31,200 --> 10:20:32,700 I'm going to put quotation marks and 13941 10:20:32,700 --> 10:20:34,740 here we're going to put dollar sign I'm 13942 10:20:34,740 --> 10:20:39,480 going to type T and T is the key and 13943 10:20:39,480 --> 10:20:42,240 dollar sign U and U is the value so now 13944 10:20:42,240 --> 10:20:45,020 if you're on this 13945 10:20:50,276 --> 10:20:52,680 you get one and Maria to and Alex Three 13946 10:20:52,680 --> 10:20:55,140 Angels so we have the key and we have 13947 10:20:55,140 --> 10:20:57,540 the corresponding values and you cannot 13948 10:20:57,540 --> 10:20:59,400 have duplicated keys and this is very 13949 10:20:59,400 --> 10:21:01,680 important to know you can also add or 13950 10:21:01,680 --> 10:21:03,180 remove elements 13951 10:21:03,180 --> 10:21:06,480 from this map so to add an element you 13952 10:21:06,480 --> 10:21:08,400 just type users 13953 10:21:08,400 --> 10:21:11,276 and we need to specify the key 13954 10:21:11,276 --> 10:21:14,040 so we put square brackets so we type 13955 10:21:14,040 --> 10:21:15,960 user square brackets and then put a key 13956 10:21:15,960 --> 10:21:17,756 let's say five 13957 10:21:17,756 --> 10:21:21,960 and we put here equals to and here we 13958 10:21:21,960 --> 10:21:23,700 need to pass a value so the value is 13959 10:21:23,700 --> 10:21:25,200 going to be let's say 13960 10:21:25,200 --> 10:21:29,360 Vlog now if you run this 13961 10:21:34,080 --> 10:21:37,436 we get one and Maria two and Alex three 13962 10:21:37,436 --> 10:21:40,080 and John N5 and the corresponding value 13963 10:21:40,080 --> 10:21:43,980 Vlad so Vlad has the Kiwi the key value 13964 10:21:43,980 --> 10:21:45,300 five 13965 10:21:45,300 --> 10:21:47,400 so you can also remove an element from 13966 10:21:47,400 --> 10:21:51,360 our map so we type users dot remove 13967 10:21:51,360 --> 10:21:54,180 you know you you here for this remove 13968 10:21:54,180 --> 10:21:57,416 function you pass the key of the of the 13969 10:21:57,416 --> 10:21:59,160 element that you want to be removed and 13970 10:21:59,160 --> 10:22:01,620 let's say that I remove two so I remove 13971 10:22:01,620 --> 10:22:04,916 Alex from our map so if I run this quad 13972 10:22:04,916 --> 10:22:06,900 now Alex you know it's not going to be 13973 10:22:06,900 --> 10:22:08,096 shown here 13974 10:22:08,096 --> 10:22:10,980 because it was removed on the line for 13975 10:22:10,980 --> 10:22:13,380 so Alex was removed so this is how you 13976 10:22:13,380 --> 10:22:16,320 can use the immutable and multiple Maps 13977 10:22:16,320 --> 10:22:18,360 this is how you can add and remove 13978 10:22:18,360 --> 10:22:21,360 elements and this is how you can Loop 13979 10:22:21,360 --> 10:22:23,520 through our map and this is our 13980 10:22:23,520 --> 10:22:25,800 discussion about Collections and see you 13981 10:22:25,800 --> 10:22:27,540 in the next video now it's time to start 13982 10:22:27,540 --> 10:22:29,220 a discussion about collections 13983 10:22:29,220 --> 10:22:30,660 operations and we're going to look at 13984 10:22:30,660 --> 10:22:32,220 the first operation and that is 13985 10:22:32,220 --> 10:22:34,200 Transformations and we're going to start 13986 10:22:34,200 --> 10:22:36,060 with the first one called mapping so I'm 13987 10:22:36,060 --> 10:22:39,020 going to create a new project 13988 10:22:39,060 --> 10:22:41,160 I'm going to call it 13989 10:22:41,160 --> 10:22:44,040 collections 13990 10:22:44,040 --> 10:22:46,936 operations 13991 10:22:47,240 --> 10:22:51,320 IntelliJ then click on create 13992 10:22:54,540 --> 10:22:57,596 we go on the left hand side 13993 10:22:57,596 --> 10:23:00,060 click on kotlin right click new I'm 13994 10:23:00,060 --> 10:23:01,560 going to create a new file I'm going to 13995 10:23:01,560 --> 10:23:03,720 call it Main 13996 10:23:03,720 --> 10:23:05,220 I'm going to create the main function 13997 10:23:05,220 --> 10:23:08,180 now 13998 10:23:08,520 --> 10:23:11,160 what are transformations as the name 13999 10:23:11,160 --> 10:23:14,220 implies Transformations are some 14000 10:23:14,220 --> 10:23:18,120 functions which which you can change a 14001 10:23:18,120 --> 10:23:20,096 specific collection 14002 10:23:20,096 --> 10:23:22,080 and we're going to look at the first one 14003 10:23:22,080 --> 10:23:24,360 which is called mapping so I'm going to 14004 10:23:24,360 --> 10:23:26,460 create first 14005 10:23:26,460 --> 10:23:29,040 a collection so I'm going to type Val 14006 10:23:29,040 --> 10:23:31,680 numbers and it's going to be a set so 14007 10:23:31,680 --> 10:23:33,300 I'm going to type numbers you're gonna 14008 10:23:33,300 --> 10:23:36,020 equals to set off 14009 10:23:36,020 --> 10:23:38,460 and here we're gonna Define some values 14010 10:23:38,460 --> 10:23:41,460 like one two 14011 10:23:41,460 --> 10:23:44,840 three four five 14012 10:23:45,776 --> 10:23:48,596 let's press Ctrl alt format the code 14013 10:23:48,596 --> 10:23:51,960 now I'm gonna add the print line here 14014 10:23:51,960 --> 10:23:55,256 now to use the mapping collection 14015 10:23:55,256 --> 10:23:58,560 transformation we just type numbers 14016 10:23:58,560 --> 10:24:01,620 and one important thing to know here is 14017 10:24:01,620 --> 10:24:03,416 that those 14018 10:24:03,416 --> 10:24:06,240 transformation functions are Lambda 14019 10:24:06,240 --> 10:24:08,460 functions so if you don't know what 14020 10:24:08,460 --> 10:24:11,276 Lambda functions are I suggest you to 14021 10:24:11,276 --> 10:24:13,256 watch the section which is called Lambda 14022 10:24:13,256 --> 10:24:14,936 function to First understand what our 14023 10:24:14,936 --> 10:24:17,820 Lambda functions because it will be it 14024 10:24:17,820 --> 10:24:19,140 will be very hard for you to understand 14025 10:24:19,140 --> 10:24:21,776 how this how those Transformations work 14026 10:24:21,776 --> 10:24:23,400 without understanding how Lambda 14027 10:24:23,400 --> 10:24:24,720 functions work 14028 10:24:24,720 --> 10:24:26,220 now 14029 10:24:26,220 --> 10:24:29,480 here we're gonna call our our 14030 10:24:29,480 --> 10:24:32,096 transformation function which is the 14031 10:24:32,096 --> 10:24:34,980 first one which as I said is mapping and 14032 10:24:34,980 --> 10:24:37,140 the function is called map I'm going to 14033 10:24:37,140 --> 10:24:38,756 explain immediately what it's doing and 14034 10:24:38,756 --> 10:24:40,560 as you can see it's a Lambda function so 14035 10:24:40,560 --> 10:24:42,060 we're going to press enter 14036 10:24:42,060 --> 10:24:44,640 and we we are inside the curly braces 14037 10:24:44,640 --> 10:24:46,560 and here we're gonna Define our 14038 10:24:46,560 --> 10:24:48,960 transformation so what we are saying 14039 10:24:48,960 --> 10:24:50,700 basically here is 14040 10:24:50,700 --> 10:24:55,680 do whatever I I type here in this those 14041 10:24:55,680 --> 10:24:56,756 uh 14042 10:24:56,756 --> 10:25:00,660 curly braces apply to every element of 14043 10:25:00,660 --> 10:25:03,240 the of this cell of this collection so 14044 10:25:03,240 --> 10:25:07,320 if I if uh now to refer to to refer to 14045 10:25:07,320 --> 10:25:10,620 the parameter uh uh to the specific 14046 10:25:10,620 --> 10:25:14,640 parameter from the numbers uh uh set we 14047 10:25:14,640 --> 10:25:16,560 type it 14048 10:25:16,560 --> 10:25:19,800 and I'm gonna type it times 14049 10:25:19,800 --> 10:25:24,916 10. so what we are saying here is 14050 10:25:24,960 --> 10:25:29,220 do this transformation so change this uh 14051 10:25:29,220 --> 10:25:34,680 change this uh this set by uh more 14052 10:25:34,680 --> 10:25:37,800 multiplying every element inside the 14053 10:25:37,800 --> 10:25:40,916 this uh this collection inside this set 14054 10:25:40,916 --> 10:25:44,580 by 10. so if you run this now every 14055 10:25:44,580 --> 10:25:46,620 number is going to be multiplied by 10. 14056 10:25:46,620 --> 10:25:49,400 so if I run this 14057 10:25:56,700 --> 10:26:00,060 now we get instead of one two three four 14058 10:26:00,060 --> 10:26:03,840 and five we get 10 20 30 45 so what this 14059 10:26:03,840 --> 10:26:07,200 map function and what this uh it times 14060 10:26:07,200 --> 10:26:10,380 10 did is is it transformed it changed 14061 10:26:10,380 --> 10:26:15,900 our set into into what it it was changed 14062 10:26:15,900 --> 10:26:18,240 by the expression that we defined here 14063 10:26:18,240 --> 10:26:21,660 so we said that I want every element we 14064 10:26:21,660 --> 10:26:23,936 said here to be multiplied by 10 and 14065 10:26:23,936 --> 10:26:26,276 then we display them here 14066 10:26:26,276 --> 10:26:28,916 we can go even further we can put here 14067 10:26:28,916 --> 10:26:32,220 an if statement so we can type here if 14068 10:26:32,220 --> 10:26:35,640 let's say it so it refers to the to a 14069 10:26:35,640 --> 10:26:38,220 specific element in that list in that 14070 10:26:38,220 --> 10:26:42,660 set so if it equals equals so if equals 14071 10:26:42,660 --> 10:26:46,256 three I'm gonna say 14072 10:26:46,256 --> 10:26:48,120 we're gonna say here 14073 10:26:48,120 --> 10:26:50,340 it 14074 10:26:50,340 --> 10:26:51,840 times 14075 10:26:51,840 --> 10:26:53,580 100. 14076 10:26:53,580 --> 10:26:57,060 so what we're saying here if one of 14077 10:26:57,060 --> 10:27:00,596 those elements inside these numbers set 14078 10:27:00,596 --> 10:27:04,740 is equal to 3 then multiply it change it 14079 10:27:04,740 --> 10:27:08,400 so change it by by the by this 14080 10:27:08,400 --> 10:27:11,520 expression so multiply it by 100 and you 14081 10:27:11,520 --> 10:27:13,080 can also put the else part so we put 14082 10:27:13,080 --> 10:27:17,936 else you want to put it times 14083 10:27:17,936 --> 10:27:21,060 so if the else part is true if the else 14084 10:27:21,060 --> 10:27:23,160 part is executed we're saying if the 14085 10:27:23,160 --> 10:27:25,820 number is not equal to 3 from this list 14086 10:27:25,820 --> 10:27:30,540 then multiply the number multiply the 14087 10:27:30,540 --> 10:27:35,416 number by uh by 10. so now if I run this 14088 10:27:36,060 --> 10:27:39,799 look what we have here 14089 10:27:41,880 --> 10:27:45,120 so we have 10 20 and we have 300 here 14090 10:27:45,120 --> 10:27:47,880 this is because this transformation now 14091 10:27:47,880 --> 10:27:51,180 is using the if and else statements that 14092 10:27:51,180 --> 10:27:53,936 you define here so this is what the map 14093 10:27:53,936 --> 10:27:57,000 Transformations allows you to do you can 14094 10:27:57,000 --> 10:27:59,096 change you can transform a specific 14095 10:27:59,096 --> 10:28:01,500 collection by an expression that we 14096 10:28:01,500 --> 10:28:03,416 defined inside the curly braces of the 14097 10:28:03,416 --> 10:28:05,040 map function 14098 10:28:05,040 --> 10:28:06,360 all right 14099 10:28:06,360 --> 10:28:10,140 now let's look at how we can can use the 14100 10:28:10,140 --> 10:28:12,256 map transformation to with another 14101 10:28:12,256 --> 10:28:14,936 collection unless you'll see how can you 14102 10:28:14,936 --> 10:28:17,820 can use the map transformation with a 14103 10:28:17,820 --> 10:28:20,520 map so with uh with the with the map 14104 10:28:20,520 --> 10:28:23,820 which has a key and a value so to do 14105 10:28:23,820 --> 10:28:26,640 that we just type here 14106 10:28:26,640 --> 10:28:27,900 Val 14107 10:28:27,900 --> 10:28:31,200 numbers map so I'm going to type file 14108 10:28:31,200 --> 10:28:32,660 numbers 14109 10:28:32,660 --> 10:28:35,756 equals I'm going to type map off 14110 10:28:35,756 --> 10:28:38,936 and here I'm gonna type 14111 10:28:38,936 --> 10:28:40,320 key 14112 10:28:40,320 --> 10:28:42,720 one 14113 10:28:42,720 --> 10:28:43,860 two 14114 10:28:43,860 --> 10:28:45,900 one 14115 10:28:45,900 --> 10:28:47,040 then 14116 10:28:47,040 --> 10:28:48,660 key 14117 10:28:48,660 --> 10:28:50,276 tool 14118 10:28:50,276 --> 10:28:53,276 two two then 14119 10:28:53,276 --> 10:28:55,620 key three 14120 10:28:55,620 --> 10:28:57,180 two 14121 10:28:57,180 --> 10:28:58,436 three 14122 10:28:58,436 --> 10:29:00,060 and 14123 10:29:00,060 --> 10:29:01,916 finally key 14124 10:29:01,916 --> 10:29:03,480 for 14125 10:29:03,480 --> 10:29:04,620 two 14126 10:29:04,620 --> 10:29:05,640 four 14127 10:29:05,640 --> 10:29:09,500 now if you wanna use the map map 14128 10:29:09,500 --> 10:29:13,080 transfer transformation function on the 14129 10:29:13,080 --> 10:29:17,640 map map collection you you you can use 14130 10:29:17,640 --> 10:29:21,480 it either on the keys so you can use it 14131 10:29:21,480 --> 10:29:24,596 on the keys or on the values so we can 14132 10:29:24,596 --> 10:29:27,480 type here print line 14133 10:29:27,480 --> 10:29:29,340 number map 14134 10:29:29,340 --> 10:29:33,140 number map so we choose here 14135 10:29:33,180 --> 10:29:37,080 Dot and we choose map Keys now so choose 14136 10:29:37,080 --> 10:29:40,860 this one map key Lambda expression so 14137 10:29:40,860 --> 10:29:45,180 map keys that one and here to type eat 14138 10:29:45,180 --> 10:29:50,340 dot key so get the key Dot uppercase so 14139 10:29:50,340 --> 10:29:52,620 that is going to upper case all the keys 14140 10:29:52,620 --> 10:29:55,256 inside our map then I'm gonna add an 14141 10:29:55,256 --> 10:29:57,540 another print line here 14142 10:29:57,540 --> 10:30:00,860 I'm gonna type number map 14143 10:30:01,320 --> 10:30:03,060 Dot 14144 10:30:03,060 --> 10:30:04,256 map 14145 10:30:04,256 --> 10:30:05,880 now choose 14146 10:30:05,880 --> 10:30:08,880 map values 14147 10:30:08,880 --> 10:30:12,660 and we type here it dot value 14148 10:30:12,660 --> 10:30:16,500 so the Value Plus 14149 10:30:16,756 --> 10:30:21,776 it dot key dot length 14150 10:30:21,776 --> 10:30:24,360 now if you run this though this 14151 10:30:24,360 --> 10:30:26,520 expression is going to be applied to all 14152 10:30:26,520 --> 10:30:28,860 of our keys inside our numbers map that 14153 10:30:28,860 --> 10:30:30,720 we Define above and the second 14154 10:30:30,720 --> 10:30:32,276 transformation is going to be applied to 14155 10:30:32,276 --> 10:30:33,960 all the values that we defined inside 14156 10:30:33,960 --> 10:30:36,120 our numbers map so if you run this code 14157 10:30:36,120 --> 10:30:38,096 now I'm going to see the 14158 10:30:38,096 --> 10:30:40,680 the keys being first uppercase and then 14159 10:30:40,680 --> 10:30:42,480 we're going to see the keys plus plus 14160 10:30:42,480 --> 10:30:45,060 the length so you see 14161 10:30:45,060 --> 10:30:50,096 first I get 10 20 300 for 40 and 50 for 14162 10:30:50,096 --> 10:30:53,040 our first transformation that we have 14163 10:30:53,040 --> 10:30:54,060 here 14164 10:30:54,060 --> 10:30:58,140 then we have our second then we have our 14165 10:30:58,140 --> 10:31:00,240 transformation on the map on the map 14166 10:31:00,240 --> 10:31:02,340 keys so we have we have the key and then 14167 10:31:02,340 --> 10:31:04,500 we have the value we have he and the the 14168 10:31:04,500 --> 10:31:07,680 key is uppercase because we said uh to 14169 10:31:07,680 --> 10:31:10,256 to do that by using this expression it 14170 10:31:10,256 --> 10:31:12,480 so it uppercase all the keys as you can 14171 10:31:12,480 --> 10:31:13,980 see here and then you see the 14172 10:31:13,980 --> 10:31:16,140 corresponding value for the key 14173 10:31:16,140 --> 10:31:18,120 after that you see 14174 10:31:18,120 --> 10:31:20,640 you have numbers map map that value so 14175 10:31:20,640 --> 10:31:23,220 you have it that Value Plus e dot key 14176 10:31:23,220 --> 10:31:25,436 dot length so we have key 14177 10:31:25,436 --> 10:31:29,276 and I have one and we have six we have 14178 10:31:29,276 --> 10:31:32,700 key two and half seven so the key the 14179 10:31:32,700 --> 10:31:36,060 value of the key is uh is uh to the 14180 10:31:36,060 --> 10:31:38,580 value of the key it's added the length 14181 10:31:38,580 --> 10:31:40,380 of the 14182 10:31:40,380 --> 10:31:44,880 so to the value that we have here we we 14183 10:31:44,880 --> 10:31:49,140 have added the the value length of the 14184 10:31:49,140 --> 10:31:51,596 string key that we defined here so this 14185 10:31:51,596 --> 10:31:55,080 way I have uh we have uh two then we 14186 10:31:55,080 --> 10:31:57,540 have seven then we have eight and then 14187 10:31:57,540 --> 10:31:58,800 we have nine 14188 10:31:58,800 --> 10:32:02,276 so this is our and you can also 14189 10:32:02,276 --> 10:32:05,640 you can also do for the uh map for the 14190 10:32:05,640 --> 10:32:07,680 map transformation and we type here 14191 10:32:07,680 --> 10:32:09,360 numbers 14192 10:32:09,360 --> 10:32:12,060 Dot map 14193 10:32:12,060 --> 10:32:14,220 and we choose this one index not node 14194 10:32:14,220 --> 10:32:17,460 and here we choose we have two 14195 10:32:17,460 --> 10:32:20,220 parameters first is the index and second 14196 10:32:20,220 --> 10:32:22,740 is the value so I'm gonna choose the 14197 10:32:22,740 --> 10:32:24,776 name for the first one index and for the 14198 10:32:24,776 --> 10:32:26,756 second one let's call it uh 14199 10:32:26,756 --> 10:32:29,756 value then you have the auto and here we 14200 10:32:29,756 --> 10:32:31,096 have we're going to Define our 14201 10:32:31,096 --> 10:32:33,840 transformation of our expression which 14202 10:32:33,840 --> 10:32:36,120 is going to be applied on our numbers so 14203 10:32:36,120 --> 10:32:38,460 here I can use the index so again I'm 14204 10:32:38,460 --> 10:32:40,140 going to type here an effects if a 14205 10:32:40,140 --> 10:32:42,960 statement so if 14206 10:32:42,960 --> 10:32:45,916 index 14207 10:32:46,080 --> 10:32:50,880 equals equals zero then I'm gonna 14208 10:32:50,880 --> 10:32:53,400 assign so I'm going to transform to the 14209 10:32:53,400 --> 10:32:55,080 to that we're going to put the value 14210 10:32:55,080 --> 10:32:57,120 null 14211 10:32:57,120 --> 10:33:00,916 to the index 0 else 14212 10:33:01,020 --> 10:33:04,700 I'm gonna type index 14213 10:33:05,276 --> 10:33:07,800 times value 14214 10:33:07,800 --> 10:33:10,860 so else you're gonna uh multiply the 14215 10:33:10,860 --> 10:33:13,200 index time times the value so if you run 14216 10:33:13,200 --> 10:33:15,500 this 14217 10:33:19,220 --> 10:33:24,120 we get 2 6 12 and 20. so for for the 14218 10:33:24,120 --> 10:33:26,820 first one we get nothing because we for 14219 10:33:26,820 --> 10:33:29,340 the first index we signed null 14220 10:33:29,340 --> 10:33:31,916 and then uh it's using the else part to 14221 10:33:31,916 --> 10:33:35,220 to transform our numbers list and it's 14222 10:33:35,220 --> 10:33:38,460 multiplying the index by the value so we 14223 10:33:38,460 --> 10:33:40,200 we first uh 14224 10:33:40,200 --> 10:33:43,620 we first have this indexed uh 14225 10:33:43,620 --> 10:33:47,040 zero multiply the here we have null for 14226 10:33:47,040 --> 10:33:48,900 this because we checked here so we have 14227 10:33:48,900 --> 10:33:51,480 not so at the index two we have one 14228 10:33:51,480 --> 10:33:54,840 times two and uh we have two then here 14229 10:33:54,840 --> 10:33:57,720 we have add indexed two we have two 14230 10:33:57,720 --> 10:34:00,060 times three so the index is two the 14231 10:34:00,060 --> 10:34:04,380 value is three and uh we we get uh we 14232 10:34:04,380 --> 10:34:07,500 get six here and so on so this is how we 14233 10:34:07,500 --> 10:34:09,416 can use also the index if you want in 14234 10:34:09,416 --> 10:34:11,640 your transformation now we're going to 14235 10:34:11,640 --> 10:34:13,256 look at the next transformation that you 14236 10:34:13,256 --> 10:34:14,936 can do in our collection and that is 14237 10:34:14,936 --> 10:34:17,540 zipping 14238 10:34:17,936 --> 10:34:19,436 now it's time to look at the 14239 10:34:19,436 --> 10:34:21,960 transformation function called zipping 14240 10:34:21,960 --> 10:34:23,820 so I'm going to add a comment here 14241 10:34:23,820 --> 10:34:25,380 called the 14242 10:34:25,380 --> 10:34:27,540 zipping 14243 10:34:27,540 --> 10:34:30,000 and I'm going to declared to list I'm 14244 10:34:30,000 --> 10:34:34,140 gonna call the first one colors 14245 10:34:34,140 --> 10:34:36,916 no particles 14246 10:34:37,560 --> 10:34:40,820 I'm gonna type please stop 14247 10:34:42,480 --> 10:34:45,380 I'm gonna type red 14248 10:34:46,980 --> 10:34:49,640 Brown 14249 10:34:49,680 --> 10:34:52,256 and 14250 10:34:52,256 --> 10:34:54,800 Gray 14251 10:34:55,256 --> 10:34:57,416 and I'm going to create another list 14252 10:34:57,416 --> 10:35:00,300 called the animals 14253 10:35:00,300 --> 10:35:03,300 stuff 14254 10:35:04,080 --> 10:35:08,460 I'm gonna type here Fox 14255 10:35:08,460 --> 10:35:10,256 beer 14256 10:35:10,256 --> 10:35:12,660 quotation marks 14257 10:35:12,660 --> 10:35:14,220 and uh 14258 10:35:14,220 --> 10:35:16,700 wolf 14259 10:35:17,880 --> 10:35:19,436 now 14260 10:35:19,436 --> 10:35:23,596 what does the zipping or the zip 14261 10:35:23,596 --> 10:35:27,200 functions allow us to do is to create 14262 10:35:27,200 --> 10:35:30,240 pairs of elements with the same position 14263 10:35:30,240 --> 10:35:33,360 in both collections so we can create 14264 10:35:33,360 --> 10:35:36,900 pairs with red we can create a pair of 14265 10:35:36,900 --> 10:35:40,916 fox and red a pair of bear and brown and 14266 10:35:40,916 --> 10:35:42,776 a pair of wolf and gray 14267 10:35:42,776 --> 10:35:45,060 and you can do that just by typing here 14268 10:35:45,060 --> 10:35:46,800 print line 14269 10:35:46,800 --> 10:35:48,840 and we have you can do this in two ways 14270 10:35:48,840 --> 10:35:51,720 to type colors 14271 10:35:51,720 --> 10:35:54,660 and you can type Dot 14272 10:35:54,660 --> 10:36:00,200 dot zip and you type here animals 14273 10:36:00,360 --> 10:36:01,680 and 14274 10:36:01,680 --> 10:36:04,080 if you run this 14275 10:36:04,080 --> 10:36:06,416 you'll see them in pairs so you're gonna 14276 10:36:06,416 --> 10:36:08,276 see Fox thread 14277 10:36:08,276 --> 10:36:12,416 bear brown and so on so you see here red 14278 10:36:12,416 --> 10:36:18,120 fox brown bear gray also the zip and the 14279 10:36:18,120 --> 10:36:20,640 zipping is used to create Pairs and the 14280 10:36:20,640 --> 10:36:24,776 pairs are at the same position in both 14281 10:36:24,776 --> 10:36:27,256 on both collections 14282 10:36:27,256 --> 10:36:30,360 or if you don't want to use this Z 14283 10:36:30,360 --> 10:36:32,820 function with parenthesis you can type 14284 10:36:32,820 --> 10:36:37,140 here colors zip and animals 14285 10:36:37,140 --> 10:36:39,240 and type here any models 14286 10:36:39,240 --> 10:36:40,800 now if you run this code you're going to 14287 10:36:40,800 --> 10:36:43,580 see the same output 14288 10:36:51,360 --> 10:36:54,120 now if you want to use a transformation 14289 10:36:54,120 --> 10:36:56,096 with the Z function you can type here 14290 10:36:56,096 --> 10:36:57,776 print line 14291 10:36:57,776 --> 10:37:02,220 you can type colors dot zip 14292 10:37:02,220 --> 10:37:04,380 and I'm going to use the parenthesis 14293 10:37:04,380 --> 10:37:07,140 animals 14294 10:37:07,140 --> 10:37:10,620 and animals and now I'm going to put 14295 10:37:10,620 --> 10:37:13,020 curly braces so the Lambda function here 14296 10:37:13,020 --> 10:37:14,520 I'm going to type 14297 10:37:14,520 --> 10:37:17,119 animal 14298 10:37:18,596 --> 10:37:22,436 for a let's put for scholar 14299 10:37:22,436 --> 10:37:26,756 and animal I'm gonna put the arrow sign 14300 10:37:26,756 --> 10:37:29,520 and here I'm gonna type day I'm gonna 14301 10:37:29,520 --> 10:37:32,160 but I'm gonna put first 14302 10:37:32,160 --> 10:37:35,160 quotation marks and quite the 14303 10:37:35,160 --> 10:37:38,220 and I'm gonna put dollar sign 14304 10:37:38,220 --> 10:37:41,820 and we're going to refer our animal uh 14305 10:37:41,820 --> 10:37:44,040 parameter that we defined here so we 14306 10:37:44,040 --> 10:37:45,416 type here there 14307 10:37:45,416 --> 10:37:47,220 animal 14308 10:37:47,220 --> 10:37:51,416 Dot and we call the replace first chart 14309 10:37:51,416 --> 10:37:53,936 function so type replace first chart and 14310 10:37:53,936 --> 10:37:56,520 as you can see this also has uh I think 14311 10:37:56,520 --> 10:37:57,680 along the 14312 10:37:57,680 --> 10:38:03,320 function and inside of this we type it 14313 10:38:03,320 --> 10:38:06,300 dot uppercase 14314 10:38:06,300 --> 10:38:08,756 uppercase and this is gonna uppercase 14315 10:38:08,756 --> 10:38:13,020 the first letter of the animal 14316 10:38:13,020 --> 10:38:14,640 uh 14317 10:38:14,640 --> 10:38:17,700 with the animal that is defined here and 14318 10:38:17,700 --> 10:38:20,400 I'm going to type yes 14319 10:38:20,400 --> 10:38:22,200 dollar sign 14320 10:38:22,200 --> 10:38:23,580 color 14321 10:38:23,580 --> 10:38:27,320 right now if you run this 14322 10:38:27,480 --> 10:38:30,900 you're gonna see the force and the f is 14323 10:38:30,900 --> 10:38:34,436 uppercase because it's uppercase by this 14324 10:38:34,436 --> 10:38:36,660 expression that we have here 14325 10:38:36,660 --> 10:38:39,720 it replaces and this it refers to the 14326 10:38:39,720 --> 10:38:43,980 animal uh an animal uh variable animal 14327 10:38:43,980 --> 10:38:45,960 variable that is defined here and it 14328 10:38:45,960 --> 10:38:48,660 refers to the first letter so it says 14329 10:38:48,660 --> 10:38:50,520 eat that uppercase so uppercase the 14330 10:38:50,520 --> 10:38:51,720 first letter 14331 10:38:51,720 --> 10:38:55,500 then uh then uh return that to the to 14332 10:38:55,500 --> 10:38:58,800 the animal uh any more variable and we 14333 10:38:58,800 --> 10:39:01,200 have the fox is red so I have the color 14334 10:39:01,200 --> 10:39:04,140 and then we have the bear is brown the 14335 10:39:04,140 --> 10:39:06,960 wolf is gray and all the animals start 14336 10:39:06,960 --> 10:39:08,936 the capital letter like we Define here 14337 10:39:08,936 --> 10:39:11,096 in this 14338 10:39:11,096 --> 10:39:13,200 in this expression 14339 10:39:13,200 --> 10:39:15,416 so this is the zip function so this is 14340 10:39:15,416 --> 10:39:16,916 how you can use the Z function to create 14341 10:39:16,916 --> 10:39:20,700 Pairs and also you can use the unzip 14342 10:39:20,700 --> 10:39:24,000 function to unzip a list of pairs so 14343 10:39:24,000 --> 10:39:26,276 first let's create a list of pairs so 14344 10:39:26,276 --> 10:39:28,080 I'm going to type Val 14345 10:39:28,080 --> 10:39:31,220 number pairs 14346 10:39:34,860 --> 10:39:36,416 equals 14347 10:39:36,416 --> 10:39:39,619 I'm gonna type list of 14348 10:39:41,340 --> 10:39:44,539 and here we're going to put 14349 10:39:44,880 --> 10:39:47,776 one 14350 10:39:48,300 --> 10:39:50,700 two 14351 10:39:50,700 --> 10:39:53,360 one 14352 10:39:53,880 --> 10:39:55,680 two 14353 10:39:55,680 --> 10:39:57,680 two 14354 10:39:59,460 --> 10:40:01,140 three 14355 10:40:01,140 --> 10:40:03,140 two 14356 10:40:04,560 --> 10:40:06,979 three 14357 10:40:07,560 --> 10:40:09,240 four 14358 10:40:09,240 --> 10:40:10,436 two 14359 10:40:10,436 --> 10:40:13,916 four now if you want to unzip those 14360 10:40:13,916 --> 10:40:15,776 pairs that we created you can just type 14361 10:40:15,776 --> 10:40:18,680 here print line 14362 10:40:19,380 --> 10:40:23,160 number pairs let's first display them as 14363 10:40:23,160 --> 10:40:24,540 pairs 14364 10:40:24,540 --> 10:40:27,960 and the necklace print line and so let's 14365 10:40:27,960 --> 10:40:30,740 type number pairs 14366 10:40:30,740 --> 10:40:33,120 dot unzip 14367 10:40:33,120 --> 10:40:34,980 now if you run this you're gonna see 14368 10:40:34,980 --> 10:40:36,540 them first in pairs and then you're 14369 10:40:36,540 --> 10:40:39,480 gonna see them unpaired because we use 14370 10:40:39,480 --> 10:40:41,520 here the anzi function so as you can see 14371 10:40:41,520 --> 10:40:44,220 here you have first one and you have one 14372 10:40:44,220 --> 10:40:46,436 two two three three four four so you 14373 10:40:46,436 --> 10:40:48,120 have in pairs first 14374 10:40:48,120 --> 10:40:50,756 and then we have them separated so we 14375 10:40:50,756 --> 10:40:52,980 have first one two three four 14376 10:40:52,980 --> 10:40:54,840 and then we have the numbers one two 14377 10:40:54,840 --> 10:40:57,300 three four so did you we unzip them we 14378 10:40:57,300 --> 10:40:58,800 separated them they're not pairs now 14379 10:40:58,800 --> 10:41:00,960 anymore with this unzip function this so 14380 10:41:00,960 --> 10:41:04,080 this is how you can use the NZ function 14381 10:41:04,080 --> 10:41:05,400 now it's time to look at the next 14382 10:41:05,400 --> 10:41:07,200 transformation that we can use and that 14383 10:41:07,200 --> 10:41:10,256 is Association now what is association 14384 10:41:10,256 --> 10:41:12,720 association Transformations allows you 14385 10:41:12,720 --> 10:41:14,640 to build maps from The Collection 14386 10:41:14,640 --> 10:41:16,980 elements and certain values associated 14387 10:41:16,980 --> 10:41:18,120 with them 14388 10:41:18,120 --> 10:41:21,000 and different Association types the 14389 10:41:21,000 --> 10:41:23,820 elements can be either keys or values in 14390 10:41:23,820 --> 10:41:25,200 the association map I'm going to see 14391 10:41:25,200 --> 10:41:27,596 what this means and the basic 14392 10:41:27,596 --> 10:41:30,916 Association function is the association 14393 10:41:30,916 --> 10:41:34,320 associated with which creates a mob on 14394 10:41:34,320 --> 10:41:35,756 which the elements of the original 14395 10:41:35,756 --> 10:41:39,360 collection are keys and the values are 14396 10:41:39,360 --> 10:41:41,700 produced from them by the given 14397 10:41:41,700 --> 10:41:43,500 transformation function so let's see an 14398 10:41:43,500 --> 10:41:46,080 example with uh with this so I'm going 14399 10:41:46,080 --> 10:41:48,360 to delete this code because 14400 10:41:48,360 --> 10:41:50,820 it will make a doubt but hard to 14401 10:41:50,820 --> 10:41:52,256 understand we could have so many things 14402 10:41:52,256 --> 10:41:53,820 here 14403 10:41:53,820 --> 10:41:56,220 and I'm going to type Val 14404 10:41:56,220 --> 10:41:59,820 numbers you know put equals 14405 10:41:59,820 --> 10:42:03,080 I'm gonna type really stuff 14406 10:42:03,360 --> 10:42:06,436 nanotype here 14407 10:42:06,900 --> 10:42:09,620 one 14408 10:42:13,256 --> 10:42:15,320 two 14409 10:42:16,256 --> 10:42:18,256 three 14410 10:42:18,776 --> 10:42:21,000 and four 14411 10:42:21,000 --> 10:42:24,480 now I'm going to type here print line 14412 10:42:24,480 --> 10:42:27,776 numbers and we are going to use the 14413 10:42:27,776 --> 10:42:30,660 association associate with the function 14414 10:42:30,660 --> 10:42:34,256 so we type associated with 14415 10:42:34,256 --> 10:42:36,900 and this is going to produce as I said 14416 10:42:36,900 --> 10:42:39,540 previously it's going to produce a map 14417 10:42:39,540 --> 10:42:41,460 in which the elements of the original 14418 10:42:41,460 --> 10:42:45,120 collection are keys so the keys of this 14419 10:42:45,120 --> 10:42:48,660 map are going to be those elements and 14420 10:42:48,660 --> 10:42:49,580 the 14421 10:42:49,580 --> 10:42:52,080 values are going to maybe are going to 14422 10:42:52,080 --> 10:42:54,180 be produced by what expression we Define 14423 10:42:54,180 --> 10:42:56,040 here inside the parenthesis so I'm going 14424 10:42:56,040 --> 10:42:57,960 to Define here 14425 10:42:57,960 --> 10:43:01,276 it so I'm going to type it 14426 10:43:01,276 --> 10:43:03,840 dot length 14427 10:43:03,840 --> 10:43:05,756 so now if you run this you're gonna see 14428 10:43:05,756 --> 10:43:07,860 the keys being one and then I'm gonna 14429 10:43:07,860 --> 10:43:10,256 see the value being the length of that 14430 10:43:10,256 --> 10:43:14,300 specific element so if you run this 14431 10:43:18,000 --> 10:43:21,300 we get the key one the value three the 14432 10:43:21,300 --> 10:43:23,820 key two the value three then you get 14433 10:43:23,820 --> 10:43:26,220 three the value is five then you get 14434 10:43:26,220 --> 10:43:30,540 four and the value is four so what has 14435 10:43:30,540 --> 10:43:32,580 happened now is that the associate with 14436 10:43:32,580 --> 10:43:35,400 function created the map in which the 14437 10:43:35,400 --> 10:43:39,416 elements the key the key are the 14438 10:43:39,416 --> 10:43:42,240 elements of the collections so those are 14439 10:43:42,240 --> 10:43:45,540 the keys inside our map and the values 14440 10:43:45,540 --> 10:43:49,080 of uh those uh with the association with 14441 10:43:49,080 --> 10:43:52,680 the associated with those keys are 14442 10:43:52,680 --> 10:43:55,020 produced by the expression that we 14443 10:43:55,020 --> 10:43:56,756 Define here by its dot length so 14444 10:43:56,756 --> 10:43:59,220 basically gonna use the length of that 14445 10:43:59,220 --> 10:44:01,380 specific element and it's going to be 14446 10:44:01,380 --> 10:44:04,080 the value so this is why I have one 14447 10:44:04,080 --> 10:44:09,900 three two three three five and so on now 14448 10:44:09,900 --> 10:44:10,980 for 14449 10:44:10,980 --> 10:44:13,560 building maps with collection elements 14450 10:44:13,560 --> 10:44:16,620 as values there is a function called 14451 10:44:16,620 --> 10:44:19,560 associate by it takes a function that 14452 10:44:19,560 --> 10:44:21,776 returns a key based on an element's 14453 10:44:21,776 --> 10:44:24,900 value and if two elements keys are equal 14454 10:44:24,900 --> 10:44:28,800 only that last one remains in the map so 14455 10:44:28,800 --> 10:44:30,120 let's look at this one so I'm going to 14456 10:44:30,120 --> 10:44:31,980 type here 14457 10:44:31,980 --> 10:44:34,936 print line 14458 10:44:36,060 --> 10:44:39,560 I'm going to put here numbers 14459 10:44:41,580 --> 10:44:43,860 so we type numbers 14460 10:44:43,860 --> 10:44:46,740 dot associate by 14461 10:44:46,740 --> 10:44:48,720 and now here going to define the 14462 10:44:48,720 --> 10:44:50,820 expression which is going to build the 14463 10:44:50,820 --> 10:44:54,480 key because now the the associate by 14464 10:44:54,480 --> 10:44:56,096 function is going to build a correlation 14465 10:44:56,096 --> 10:44:58,916 which the elements now are not Keys like 14466 10:44:58,916 --> 10:45:01,080 we had previously but they are values so 14467 10:45:01,080 --> 10:45:02,640 here in the expression need now to 14468 10:45:02,640 --> 10:45:05,700 Define how the key for those respective 14469 10:45:05,700 --> 10:45:07,080 values that we have here is going to be 14470 10:45:07,080 --> 10:45:10,860 created so we type here it 14471 10:45:10,860 --> 10:45:13,436 DOT first 14472 10:45:13,436 --> 10:45:15,596 dot uppercase 14473 10:45:15,596 --> 10:45:17,756 so this is what what is this is gonna do 14474 10:45:17,756 --> 10:45:20,220 it's gonna create a key 14475 10:45:20,220 --> 10:45:22,500 it's gonna use one let's say that we 14476 10:45:22,500 --> 10:45:23,936 have element one 14477 10:45:23,936 --> 10:45:27,060 and it's gonna take your element one 14478 10:45:27,060 --> 10:45:29,096 it's gonna take the first letter of the 14479 10:45:29,096 --> 10:45:31,200 element one and it's gonna uppercase it 14480 10:45:31,200 --> 10:45:34,800 and you're gonna have o and as the key 14481 10:45:34,800 --> 10:45:37,380 we're gonna have o and as the value 14482 10:45:37,380 --> 10:45:40,500 we're gonna have one and uh for the next 14483 10:45:40,500 --> 10:45:43,200 ones uh it will be the same so now if I 14484 10:45:43,200 --> 10:45:45,916 run this quad 14485 10:45:46,860 --> 10:45:49,820 foreign 14486 10:45:56,240 --> 10:45:59,340 elements are the values here so they are 14487 10:45:59,340 --> 10:46:00,776 the values previously The Collection 14488 10:46:00,776 --> 10:46:03,960 elements what the key is and the the 14489 10:46:03,960 --> 10:46:06,300 keys were builded by the expression that 14490 10:46:06,300 --> 10:46:09,480 we defined here by this so we have o and 14491 10:46:09,480 --> 10:46:12,776 we have t and we have four and we we 14492 10:46:12,776 --> 10:46:17,700 have uh t uh only one time and we don't 14493 10:46:17,700 --> 10:46:20,096 have two entry because uh you cannot 14494 10:46:20,096 --> 10:46:22,436 have duplicated keys in a map so this is 14495 10:46:22,436 --> 10:46:25,500 why you only have one only one t here 14496 10:46:25,500 --> 10:46:27,720 so uh 14497 10:46:27,720 --> 10:46:30,776 this is associated so this is 14498 10:46:30,776 --> 10:46:34,436 Association now associate by can also be 14499 10:46:34,436 --> 10:46:36,300 called with a value transformation 14500 10:46:36,300 --> 10:46:39,360 function and to do to do that you just 14501 10:46:39,360 --> 10:46:41,880 type here print line 14502 10:46:41,880 --> 10:46:44,479 numbers 14503 10:46:44,936 --> 10:46:48,480 that associate by 14504 10:46:48,480 --> 10:46:51,240 now we need to 14505 10:46:51,240 --> 10:46:54,480 apply a transformation now on the key 14506 10:46:54,480 --> 10:46:57,360 and on the value and how gonna how gonna 14507 10:46:57,360 --> 10:46:58,436 do that 14508 10:46:58,436 --> 10:47:01,320 we can type here actually let's delete 14509 10:47:01,320 --> 10:47:03,240 this because we don't need curly braces 14510 10:47:03,240 --> 10:47:07,200 we put parentheses and we type here key 14511 10:47:07,200 --> 10:47:09,596 and we have a suggestion key selector so 14512 10:47:09,596 --> 10:47:11,040 we're gonna apply the First 14513 10:47:11,040 --> 10:47:14,756 Transformation on the key so we type key 14514 10:47:14,756 --> 10:47:17,580 selector that named argument and we put 14515 10:47:17,580 --> 10:47:18,960 curly braces 14516 10:47:18,960 --> 10:47:25,160 and here we type it so the key DOT first 14517 10:47:25,160 --> 10:47:27,776 dot uppercase 14518 10:47:27,776 --> 10:47:30,360 so now what we're saying here apply this 14519 10:47:30,360 --> 10:47:31,640 transformation 14520 10:47:31,640 --> 10:47:34,200 for every key 14521 10:47:34,200 --> 10:47:37,320 in in in the map that is going to be 14522 10:47:37,320 --> 10:47:39,900 built so apply this transformation for 14523 10:47:39,900 --> 10:47:41,880 every key that is in the map that is 14524 10:47:41,880 --> 10:47:44,220 going to be built so we're saying to 14525 10:47:44,220 --> 10:47:46,620 uppercase uh to take the first letter 14526 10:47:46,620 --> 10:47:50,040 and uppercase it then we also need to 14527 10:47:50,040 --> 10:47:52,080 apply transformation for the value we 14528 10:47:52,080 --> 10:47:52,800 put 14529 10:47:52,800 --> 10:47:57,240 comma here at the end of our curly brace 14530 10:47:57,240 --> 10:47:59,220 and we type here 14531 10:47:59,220 --> 10:48:00,660 value 14532 10:48:00,660 --> 10:48:03,776 you also have a name argument you put 14533 10:48:03,776 --> 10:48:06,480 again curly braces 14534 10:48:06,480 --> 10:48:10,020 and here we're gonna type it dot length 14535 10:48:10,020 --> 10:48:11,580 so this is the transformation that is 14536 10:48:11,580 --> 10:48:12,840 going to be applied 14537 10:48:12,840 --> 10:48:15,416 to all the values in the map that is 14538 10:48:15,416 --> 10:48:17,276 going to be built so this is the 14539 10:48:17,276 --> 10:48:20,340 transformation so this is how all the 14540 10:48:20,340 --> 10:48:23,756 keys and the the in the map that is 14541 10:48:23,756 --> 10:48:26,276 going to be built are gonna be uh change 14542 10:48:26,276 --> 10:48:28,980 so the first letter is going to take 14543 10:48:28,980 --> 10:48:30,540 this thing is going to take the first 14544 10:48:30,540 --> 10:48:32,880 letter of the keys and it's going to 14545 10:48:32,880 --> 10:48:34,500 uppercase it 14546 10:48:34,500 --> 10:48:36,596 this will uh 14547 10:48:36,596 --> 10:48:40,916 we'll take the length of the the all the 14548 10:48:40,916 --> 10:48:43,500 values in the in the the in the in the 14549 10:48:43,500 --> 10:48:45,300 map that is going to be built and it's 14550 10:48:45,300 --> 10:48:47,400 gonna display the the length of that 14551 10:48:47,400 --> 10:48:51,380 element so if you run this 14552 10:48:51,960 --> 10:48:58,256 we get 0 3 T 5 F4 so we have the first 14553 10:48:58,256 --> 10:49:01,140 letter uppercase and then we have the 14554 10:49:01,140 --> 10:49:04,020 length three then we have t we have the 14555 10:49:04,020 --> 10:49:05,580 letter upper case and we have the value 14556 10:49:05,580 --> 10:49:08,820 five the length of that uh uh element 14557 10:49:08,820 --> 10:49:13,500 then we have F so we have a four and we 14558 10:49:13,500 --> 10:49:16,860 have the the the transformation apply on 14559 10:49:16,860 --> 10:49:21,000 that element on this element on on four 14560 10:49:21,000 --> 10:49:22,916 and we have the length and we have four 14561 10:49:22,916 --> 10:49:25,460 so this is how we can use transformation 14562 10:49:25,460 --> 10:49:27,840 Transformations with the associate by 14563 10:49:27,840 --> 10:49:30,416 function and the next we're going to 14564 10:49:30,416 --> 10:49:33,060 look at the next transformation fact 14565 10:49:33,060 --> 10:49:35,276 function and that is flattened now it's 14566 10:49:35,276 --> 10:49:36,540 time to look at the next transformation 14567 10:49:36,540 --> 10:49:38,580 function that you can use and that is 14568 10:49:38,580 --> 10:49:40,500 the flattened function but first let's 14569 10:49:40,500 --> 10:49:41,640 see why you'll need to use this 14570 10:49:41,640 --> 10:49:43,560 flattened function in the first place 14571 10:49:43,560 --> 10:49:45,300 so I'm gonna add the comment here called 14572 10:49:45,300 --> 10:49:47,840 flatten 14573 10:49:50,160 --> 10:49:52,080 and I'm going to create a variable here 14574 10:49:52,080 --> 10:49:53,820 so I'm going to die Val it's going to be 14575 10:49:53,820 --> 10:49:55,320 called numbers 14576 10:49:55,320 --> 10:49:58,860 sets and this is going to be a list with 14577 10:49:58,860 --> 10:50:01,256 list so I'm gonna type here 14578 10:50:01,256 --> 10:50:04,140 list of 14579 10:50:04,140 --> 10:50:06,540 and inside the list I'm gonna Pro I'm 14580 10:50:06,540 --> 10:50:09,300 gonna type set off so we have a list 14581 10:50:09,300 --> 10:50:12,000 inside the inside of our list 14582 10:50:12,000 --> 10:50:13,740 and I'm going to provide some values to 14583 10:50:13,740 --> 10:50:17,756 our set of like one two three 14584 10:50:17,756 --> 10:50:21,320 then another set of 14585 10:50:22,256 --> 10:50:25,436 four five and six 14586 10:50:25,436 --> 10:50:28,936 then another set of 14587 10:50:32,220 --> 10:50:34,740 seven 14588 10:50:34,740 --> 10:50:38,096 eight and nine so 14589 10:50:38,096 --> 10:50:41,096 what we are having now is we have a list 14590 10:50:41,096 --> 10:50:44,756 within a list where the index of this 14591 10:50:44,756 --> 10:50:48,480 list is it's its own list so we have a 14592 10:50:48,480 --> 10:50:51,000 list of sets of ins 14593 10:50:51,000 --> 10:50:53,220 now if you want to Loop through this 14594 10:50:53,220 --> 10:50:55,140 list if you want to display the elements 14595 10:50:55,140 --> 10:50:57,776 in this list uh it is not that easy 14596 10:50:57,776 --> 10:51:00,960 because you will first need to Loop 14597 10:51:00,960 --> 10:51:04,500 because let's say that because what we 14598 10:51:04,500 --> 10:51:05,936 have here 14599 10:51:05,936 --> 10:51:08,756 is what is called a multi-dimensional 14600 10:51:08,756 --> 10:51:11,700 array so more specifically what we have 14601 10:51:11,700 --> 10:51:14,040 here is a two-dimensional array and at 14602 10:51:14,040 --> 10:51:17,096 the index 0 we have a list at the index 14603 10:51:17,096 --> 10:51:19,500 one we have another list and at the 14604 10:51:19,500 --> 10:51:21,840 index two we have another list 14605 10:51:21,840 --> 10:51:24,180 and to navigate to this list we need to 14606 10:51:24,180 --> 10:51:28,200 first get inside that list that is at 14607 10:51:28,200 --> 10:51:30,596 that other specific index to Loop to 14608 10:51:30,596 --> 10:51:32,700 that list and then we need to move to 14609 10:51:32,700 --> 10:51:35,400 the next index and to to look to that 14610 10:51:35,400 --> 10:51:38,276 list to that next list which is that 14611 10:51:38,276 --> 10:51:39,416 index 14612 10:51:39,416 --> 10:51:41,096 but now let's see first how we can 14613 10:51:41,096 --> 10:51:43,380 access one individual element from our 14614 10:51:43,380 --> 10:51:45,720 two-dimensional array so from our array 14615 10:51:45,720 --> 10:51:48,060 of arrays and to do that to type here 14616 10:51:48,060 --> 10:51:49,740 print line 14617 10:51:49,740 --> 10:51:52,800 gonna put numbers set 14618 10:51:52,800 --> 10:51:56,820 and if we we put square brackets and we 14619 10:51:56,820 --> 10:52:00,060 Define the index from uh which from 14620 10:52:00,060 --> 10:52:01,680 which list we want to access the element 14621 10:52:01,680 --> 10:52:03,720 and now I want to access the element 14622 10:52:03,720 --> 10:52:06,300 nine and the element 9 is stored at the 14623 10:52:06,300 --> 10:52:09,240 index two so we put two 14624 10:52:09,240 --> 10:52:12,416 and let's say that I want and I want to 14625 10:52:12,416 --> 10:52:15,180 access as I said the the element 9 now 14626 10:52:15,180 --> 10:52:17,276 to access the element line I need to go 14627 10:52:17,276 --> 10:52:19,800 within this list and use the index to 14628 10:52:19,800 --> 10:52:22,080 get the element I and element 9 is 14629 10:52:22,080 --> 10:52:23,580 stored at the index two so I'm going to 14630 10:52:23,580 --> 10:52:25,436 put square brackets two 14631 10:52:25,436 --> 10:52:27,596 but this for whatever reason doesn't 14632 10:52:27,596 --> 10:52:29,880 work with a list of sets and we need to 14633 10:52:29,880 --> 10:52:31,980 change this to array of 14634 10:52:31,980 --> 10:52:35,400 array of also those 14635 10:52:35,400 --> 10:52:39,140 also change it to array of 14636 10:52:40,436 --> 10:52:43,140 array of also array of an hour 14637 10:52:43,140 --> 10:52:45,240 everything will work fine 14638 10:52:45,240 --> 10:52:47,756 so array of 14639 10:52:47,756 --> 10:52:50,040 so if you run this so now we are 14640 10:52:50,040 --> 10:52:51,776 accessing the 14641 10:52:51,776 --> 10:52:56,340 index tool where the array is stored 14642 10:52:56,340 --> 10:52:58,500 where our number is stored and we get 14643 10:52:58,500 --> 10:53:01,500 the element two we get element nine so 14644 10:53:01,500 --> 10:53:03,360 now if I run this we're gonna see nine 14645 10:53:03,360 --> 10:53:06,800 autopilot in the console 14646 10:53:08,180 --> 10:53:10,916 so we get nine so this is how you can 14647 10:53:10,916 --> 10:53:13,460 access one individual element from our 14648 10:53:13,460 --> 10:53:15,180 multi-dimensional array so I'm going to 14649 10:53:15,180 --> 10:53:16,500 delete this I'm going to change it back 14650 10:53:16,500 --> 10:53:17,520 to 14651 10:53:17,520 --> 10:53:20,756 to a list of and to set off 14652 10:53:20,756 --> 10:53:24,080 so let's type a list of 14653 10:53:26,756 --> 10:53:29,416 set off 14654 10:53:33,180 --> 10:53:35,960 set off 14655 10:53:37,500 --> 10:53:40,340 and set off 14656 10:53:44,040 --> 10:53:45,900 and now let's see how we can Loop 14657 10:53:45,900 --> 10:53:48,960 through this type to the dimensional 14658 10:53:48,960 --> 10:53:52,500 array and to do that to type here four 14659 10:53:52,500 --> 10:53:55,620 and type numbers I type specific numbers 14660 10:53:55,620 --> 10:54:00,240 because this is gonna as as you can see 14661 10:54:00,240 --> 10:54:02,756 um gonna hold on hold the numbers the 14662 10:54:02,756 --> 10:54:05,040 set of numbers four numbers in number 14663 10:54:05,040 --> 10:54:07,700 sets 14664 10:54:08,040 --> 10:54:10,436 so now you we are you we are getting the 14665 10:54:10,436 --> 10:54:12,960 as you can see here this is of type is 14666 10:54:12,960 --> 10:54:16,916 the the type set of ins so this uh 14667 10:54:16,916 --> 10:54:19,256 basically gets the element of the number 14668 10:54:19,256 --> 10:54:22,620 set index let's say zero and it gets the 14669 10:54:22,620 --> 10:54:24,480 first set and now we need to Loop 14670 10:54:24,480 --> 10:54:27,960 through that set so another four 14671 10:54:27,960 --> 10:54:29,580 number 14672 10:54:29,580 --> 10:54:32,820 in numbers so now we are looping 14673 10:54:32,820 --> 10:54:36,120 through the list through the set which 14674 10:54:36,120 --> 10:54:38,520 is inside the list of the index zero I 14675 10:54:38,520 --> 10:54:40,500 didn't zero 14676 10:54:40,500 --> 10:54:43,256 and you can add the print line to add to 14677 10:54:43,256 --> 10:54:45,060 do output that number I'm going to type 14678 10:54:45,060 --> 10:54:47,276 here number and also I'm gonna add the 14679 10:54:47,276 --> 10:54:48,480 space here so I'm gonna add the print 14680 10:54:48,480 --> 10:54:50,820 line here and I'm gonna type here four 14681 10:54:50,820 --> 10:54:52,320 or slash 14682 10:54:52,320 --> 10:54:53,936 n 14683 10:54:53,936 --> 10:54:57,140 now if you run this 14684 10:55:00,180 --> 10:55:03,060 as you can see you have on the output 14685 10:55:03,060 --> 10:55:06,540 one two three then we have a space four 14686 10:55:06,540 --> 10:55:10,020 five seven six then seven eight and nine 14687 10:55:10,020 --> 10:55:13,740 but uh we achieved this by using a loop 14688 10:55:13,740 --> 10:55:16,140 and then we use another loop so we have 14689 10:55:16,140 --> 10:55:17,756 a loop inside the loop we have a nested 14690 10:55:17,756 --> 10:55:21,240 Loop in order to get to the 14691 10:55:21,240 --> 10:55:23,840 to this uh two dimensional array because 14692 10:55:23,840 --> 10:55:28,256 as I said the in the index of this array 14693 10:55:28,256 --> 10:55:31,200 is its own list so it contains the list 14694 10:55:31,200 --> 10:55:34,380 now with the flattened uh with the 14695 10:55:34,380 --> 10:55:36,360 flattened function what you can do is 14696 10:55:36,360 --> 10:55:39,660 that we can convert this two-dimensional 14697 10:55:39,660 --> 10:55:42,180 array in one dimensional array so you 14698 10:55:42,180 --> 10:55:44,756 don't need to use this nested for Loops 14699 10:55:44,756 --> 10:55:47,220 in order to Loop through to them it will 14700 10:55:47,220 --> 10:55:50,300 return the nested the nested 14701 10:55:50,300 --> 10:55:52,980 uh is it to private it will provide 14702 10:55:52,980 --> 10:55:55,620 access to the nasty collection elements 14703 10:55:55,620 --> 10:55:59,840 without you needing to Loop through this 14704 10:55:59,840 --> 10:56:02,276 two-dimensional Ray it will it will 14705 10:56:02,276 --> 10:56:03,660 basically 14706 10:56:03,660 --> 10:56:06,240 take that two-dimensional array and it 14707 10:56:06,240 --> 10:56:08,756 will convert it in one dimensionally and 14708 10:56:08,756 --> 10:56:11,400 you will not need to create to have this 14709 10:56:11,400 --> 10:56:13,560 nested Loop inside you have to have this 14710 10:56:13,560 --> 10:56:15,416 Loop inside the loop in order to to 14711 10:56:15,416 --> 10:56:18,240 display the number so to do that to type 14712 10:56:18,240 --> 10:56:20,460 here uh Val 14713 10:56:20,460 --> 10:56:24,660 numbers let's say flatten 14714 10:56:24,660 --> 10:56:26,276 equals to 14715 10:56:26,276 --> 10:56:30,416 number sales so our our list to it list 14716 10:56:30,416 --> 10:56:32,096 that 14717 10:56:32,096 --> 10:56:34,935 flatten 14718 10:56:34,980 --> 10:56:37,560 now if you look at the type here now 14719 10:56:37,560 --> 10:56:39,720 this is a list of fins you no longer 14720 10:56:39,720 --> 10:56:44,520 have a list of sets of ins so this is a 14721 10:56:44,520 --> 10:56:47,580 very very uh informative because it 14722 10:56:47,580 --> 10:56:49,620 tells us that this now is just simply a 14723 10:56:49,620 --> 10:56:52,860 list of ins is not a list with the set 14724 10:56:52,860 --> 10:56:56,700 which is which is uh which itself is 14725 10:56:56,700 --> 10:57:01,140 also or a list of events so 14726 10:57:01,140 --> 10:57:03,900 this converts that two-dimensional array 14727 10:57:03,900 --> 10:57:06,360 in one dimensional array and because of 14728 10:57:06,360 --> 10:57:07,980 that now it's much easier to get one 14729 10:57:07,980 --> 10:57:09,720 element from our list because now our 14730 10:57:09,720 --> 10:57:11,880 list is not a two-dimensional array and 14731 10:57:11,880 --> 10:57:14,096 it's just one dimensional array so if I 14732 10:57:14,096 --> 10:57:16,080 type here print line 14733 10:57:16,080 --> 10:57:17,700 and the 14734 10:57:17,700 --> 10:57:20,220 let's say that I want to get the element 14735 10:57:20,220 --> 10:57:23,096 nine I'm going to type here uh 14736 10:57:23,096 --> 10:57:26,040 numbers flatten 14737 10:57:26,040 --> 10:57:28,080 square brackets and I'm going to type 14738 10:57:28,080 --> 10:57:30,180 here eight because it's stored at the 14739 10:57:30,180 --> 10:57:34,936 index 8. so now if I run this 14740 10:57:36,480 --> 10:57:39,596 so we get 9 here and as you can see this 14741 10:57:39,596 --> 10:57:43,200 is much easier than uh with the the two 14742 10:57:43,200 --> 10:57:45,300 that dimensional array so this is what 14743 10:57:45,300 --> 10:57:46,740 the flattening function is doing is 14744 10:57:46,740 --> 10:57:48,060 converting the 14745 10:57:48,060 --> 10:57:51,256 the two dimensional array or if a 14746 10:57:51,256 --> 10:57:53,276 multi-dimensional array in into one 14747 10:57:53,276 --> 10:57:55,200 dimensional array so now let's look how 14748 10:57:55,200 --> 10:57:58,680 we can Loop through our uh one dimension 14749 10:57:58,680 --> 10:57:59,520 array 14750 10:57:59,520 --> 10:58:02,596 so let's delete this 14751 10:58:04,200 --> 10:58:06,660 and if you want to to Loop through this 14752 10:58:06,660 --> 10:58:09,416 you just type 4 14753 10:58:09,416 --> 10:58:10,916 number 14754 10:58:10,916 --> 10:58:12,596 in 14755 10:58:12,596 --> 10:58:15,660 numbers flatten 14756 10:58:15,660 --> 10:58:19,020 and we either here a print line 14757 10:58:19,020 --> 10:58:21,560 number 14758 10:58:22,800 --> 10:58:24,180 and 14759 10:58:24,180 --> 10:58:27,320 let's comment this code 14760 10:58:30,960 --> 10:58:34,040 and if you run this code 14761 10:58:37,880 --> 10:58:41,820 now get the number displayed one after 14762 10:58:41,820 --> 10:58:43,380 another so get one two three four five 14763 10:58:43,380 --> 10:58:46,080 six seven nine and we got this because 14764 10:58:46,080 --> 10:58:49,020 our function our flattened function our 14765 10:58:49,020 --> 10:58:52,320 transformation function is transforming 14766 10:58:52,320 --> 10:58:55,080 it's converting our two-dimensional 14767 10:58:55,080 --> 10:58:57,060 array in one dimensional array so it's 14768 10:58:57,060 --> 10:58:59,220 easier for us to get the elements into 14769 10:58:59,220 --> 10:59:01,800 loop loop through them so this is what 14770 10:59:01,800 --> 10:59:04,916 the flatten function is doing and next 14771 10:59:04,916 --> 10:59:07,860 we're going to look at the 14772 10:59:07,860 --> 10:59:10,500 string representation and the functions 14773 10:59:10,500 --> 10:59:13,320 that you can use uh what we for the for 14774 10:59:13,320 --> 10:59:15,960 the string representation 14775 10:59:15,960 --> 10:59:17,880 I'm going to close the console 14776 10:59:17,880 --> 10:59:19,380 now it's time to start a discussion 14777 10:59:19,380 --> 10:59:21,840 about string representation so if you 14778 10:59:21,840 --> 10:59:23,460 need to retrieve the collection content 14779 10:59:23,460 --> 10:59:25,080 in a readable format you can use 14780 10:59:25,080 --> 10:59:27,180 functions that transform the collections 14781 10:59:27,180 --> 10:59:29,340 to Strings and there are two functions 14782 10:59:29,340 --> 10:59:31,680 to do that the join to string and the 14783 10:59:31,680 --> 10:59:34,916 join two The Joint to string build a 14784 10:59:34,916 --> 10:59:36,240 single string from the Collision 14785 10:59:36,240 --> 10:59:38,756 elements based on the provided arguments 14786 10:59:38,756 --> 10:59:42,000 the join 2 does the same but appends the 14787 10:59:42,000 --> 10:59:43,800 result of the given appendable object 14788 10:59:43,800 --> 10:59:45,480 and what that means is that it's gonna 14789 10:59:45,480 --> 10:59:49,140 add the text to the object on which that 14790 10:59:49,140 --> 10:59:51,300 function The Joint function is called 14791 10:59:51,300 --> 10:59:52,860 you're going to see an example with this 14792 10:59:52,860 --> 10:59:54,360 immediately 14793 10:59:54,360 --> 10:59:57,480 and now I'm going to create a variable 14794 10:59:57,480 --> 11:00:00,480 called numbers while numbers 14795 11:00:00,480 --> 11:00:02,220 I'm going to put equals I'm going to 14796 11:00:02,220 --> 11:00:05,700 call it actually numbers strings 14797 11:00:05,700 --> 11:00:06,900 equal 14798 11:00:06,900 --> 11:00:10,640 and I'm gonna type here list off 14799 11:00:12,180 --> 11:00:14,400 and I'm gonna put here 14800 11:00:14,400 --> 11:00:17,480 the text one 14801 11:00:17,700 --> 11:00:19,700 two 14802 11:00:21,000 --> 11:00:23,000 three 14803 11:00:25,080 --> 11:00:26,460 and four 14804 11:00:26,460 --> 11:00:28,980 now if you want to retrieve the 14805 11:00:28,980 --> 11:00:31,020 this if you want to get the string 14806 11:00:31,020 --> 11:00:32,936 representation of this list 14807 11:00:32,936 --> 11:00:35,756 if you just type here print line 14808 11:00:35,756 --> 11:00:38,220 and type you put your numbers strings 14809 11:00:38,220 --> 11:00:41,416 and if you run this 14810 11:00:44,160 --> 11:00:48,416 we get one two three and four but this I 14811 11:00:48,416 --> 11:00:49,980 I think is calling the two string 14812 11:00:49,980 --> 11:00:51,960 function it's not because we're not 14813 11:00:51,960 --> 11:00:54,900 using the joint to string function and I 14814 11:00:54,900 --> 11:00:56,820 don't like those 14815 11:00:56,820 --> 11:00:59,096 square brackets I want them to be 14816 11:00:59,096 --> 11:01:02,096 separated by comma with spaces to get 14817 11:01:02,096 --> 11:01:04,916 the to get data just type here 14818 11:01:04,916 --> 11:01:07,820 print line 14819 11:01:08,820 --> 11:01:10,800 so let's 14820 11:01:10,800 --> 11:01:14,000 put here print line 14821 11:01:16,680 --> 11:01:20,040 and to type numbers to string 14822 11:01:20,040 --> 11:01:22,756 Dot 14823 11:01:23,040 --> 11:01:25,200 joint to string 14824 11:01:25,200 --> 11:01:28,560 and uh we don't we delete this and put 14825 11:01:28,560 --> 11:01:31,276 parenthesis 14826 11:01:31,436 --> 11:01:34,256 now you throw on this 14827 11:01:34,256 --> 11:01:36,540 you'll see now the single presentation 14828 11:01:36,540 --> 11:01:38,340 of our list they are going to be 14829 11:01:38,340 --> 11:01:42,300 separated by comma when we do is going 14830 11:01:42,300 --> 11:01:44,880 to have a it's going to have 14831 11:01:44,880 --> 11:01:46,916 spaces now let's look at the Joint 2 14832 11:01:46,916 --> 11:01:49,800 function and for that 14833 11:01:49,800 --> 11:01:52,860 I'm gonna Define another list down here 14834 11:01:52,860 --> 11:01:54,360 so I'm going to type here let's press 14835 11:01:54,360 --> 11:01:57,980 Ctrl alt for model code 14836 11:01:58,140 --> 11:02:01,256 so I'm going to type here uh 14837 11:02:01,256 --> 11:02:02,640 Val 14838 11:02:02,640 --> 11:02:06,540 is going to be called list string 14839 11:02:06,540 --> 11:02:07,860 and now I'm going to use something 14840 11:02:07,860 --> 11:02:09,120 called 14841 11:02:09,120 --> 11:02:12,060 a string buffer and this is like the 14842 11:02:12,060 --> 11:02:14,400 string type is a little bit different 14843 11:02:14,400 --> 11:02:18,436 but it's gonna allow us to do 14844 11:02:18,436 --> 11:02:21,360 is going to allow us to present how the 14845 11:02:21,360 --> 11:02:24,360 joint function over so string buffer and 14846 11:02:24,360 --> 11:02:26,220 we put parenthesis and here we Define 14847 11:02:26,220 --> 11:02:29,276 our text and we're gonna type the list 14848 11:02:29,276 --> 11:02:31,916 of numbers I'm going to put colon so the 14849 11:02:31,916 --> 11:02:34,256 type here the list 14850 11:02:34,256 --> 11:02:37,340 of numbers 14851 11:02:37,800 --> 11:02:39,416 colon 14852 11:02:39,416 --> 11:02:43,256 now to use the join 2 function just type 14853 11:02:43,256 --> 11:02:46,640 here another print line 14854 11:02:46,640 --> 11:02:50,040 we we type number string 14855 11:02:50,040 --> 11:02:54,480 number strings dot join tool 14856 11:02:54,480 --> 11:02:56,700 and as you can see this receives a 14857 11:02:56,700 --> 11:02:58,740 buffer and this is the buffer that we 14858 11:02:58,740 --> 11:03:01,020 have created here so I'm gonna 14859 11:03:01,020 --> 11:03:03,776 press enter and we're going to type I'm 14860 11:03:03,776 --> 11:03:06,840 gonna give us the argument the listing 14861 11:03:06,840 --> 11:03:08,276 that we created 14862 11:03:08,276 --> 11:03:10,916 and what is this this is gonna do is 14863 11:03:10,916 --> 11:03:14,520 gonna append this text that we have here 14864 11:03:14,520 --> 11:03:17,880 in front of all of our elements that we 14865 11:03:17,880 --> 11:03:19,980 have in our list number strings so now 14866 11:03:19,980 --> 11:03:23,000 if you run the squad 14867 11:03:26,180 --> 11:03:29,520 as you can see it added the text the 14868 11:03:29,520 --> 11:03:32,276 list of numbers that we have here to our 14869 11:03:32,276 --> 11:03:34,140 number strings in front so we have the 14870 11:03:34,140 --> 11:03:36,660 list of numbers one two three and four 14871 11:03:36,660 --> 11:03:38,040 now 14872 11:03:38,040 --> 11:03:40,680 if you want to build the 14873 11:03:40,680 --> 11:03:43,560 custom string representation you can 14874 11:03:43,560 --> 11:03:46,020 specify its parameters in a function 14875 11:03:46,020 --> 11:03:49,200 arguments which are the separator prefix 14876 11:03:49,200 --> 11:03:52,020 and postfix and the result will start 14877 11:03:52,020 --> 11:03:55,320 with the prefix and end with the with 14878 11:03:55,320 --> 11:03:57,840 the postfix and the separator will come 14879 11:03:57,840 --> 11:04:00,120 will come after each element except the 14880 11:04:00,120 --> 11:04:02,460 last so let's see how we can do that 14881 11:04:02,460 --> 11:04:03,840 so 14882 11:04:03,840 --> 11:04:06,979 um I'm gonna use 14883 11:04:08,160 --> 11:04:10,200 I'm going to use the number numbers to 14884 11:04:10,200 --> 11:04:12,180 string so I'm going to type here print 14885 11:04:12,180 --> 11:04:14,460 line 14886 11:04:14,460 --> 11:04:17,160 numbers to string 14887 11:04:17,160 --> 11:04:20,596 and we type join 14888 11:04:20,820 --> 11:04:23,276 join to string and we select this one 14889 11:04:23,276 --> 11:04:25,140 with the you select this one with the 14890 11:04:25,140 --> 11:04:27,960 which with the separator parameter 14891 11:04:27,960 --> 11:04:30,540 and the type is separator so now we 14892 11:04:30,540 --> 11:04:33,120 Define the what is going to be the S the 14893 11:04:33,120 --> 11:04:36,060 separator here and we're gonna put 14894 11:04:36,060 --> 11:04:37,916 quotation marks 14895 11:04:37,916 --> 11:04:39,900 and we're going to put 14896 11:04:39,900 --> 11:04:41,460 this so this is going to be our 14897 11:04:41,460 --> 11:04:44,220 separator between our elements next 14898 11:04:44,220 --> 11:04:46,020 we're going to define the prefix so we 14899 11:04:46,020 --> 11:04:48,360 put we put here 14900 11:04:48,360 --> 11:04:51,900 comma type prefix and now I'm going to 14901 11:04:51,900 --> 11:04:53,640 put the prefix and the prefix is going 14902 11:04:53,640 --> 11:04:56,460 to be the text start and colon so I put 14903 11:04:56,460 --> 11:04:59,700 quotation marks start 14904 11:04:59,700 --> 11:05:01,436 and colon 14905 11:05:01,436 --> 11:05:04,680 and we also need to provide the post fix 14906 11:05:04,680 --> 11:05:08,160 and to do that we put another comma here 14907 11:05:08,160 --> 11:05:11,340 so those are named the arguments which 14908 11:05:11,340 --> 11:05:12,200 are 14909 11:05:12,200 --> 11:05:14,400 defined in this files and join the 14910 11:05:14,400 --> 11:05:16,560 string so we type here 14911 11:05:16,560 --> 11:05:19,200 postfix 14912 11:05:19,200 --> 11:05:22,276 and we put 14913 11:05:22,380 --> 11:05:23,880 colon 14914 11:05:23,880 --> 11:05:25,436 and 14915 11:05:25,436 --> 11:05:27,300 now if you run this you're going to see 14916 11:05:27,300 --> 11:05:29,640 them separated by this separator that we 14917 11:05:29,640 --> 11:05:31,256 Define here is going to have the prefix 14918 11:05:31,256 --> 11:05:33,240 start and it's going to have at the end 14919 11:05:33,240 --> 11:05:35,640 this post fix which is comma and N so 14920 11:05:35,640 --> 11:05:38,840 now if you run this quad 14921 11:05:42,900 --> 11:05:44,880 as you can see we have start and we have 14922 11:05:44,880 --> 11:05:46,860 one then it's separated by the sign that 14923 11:05:46,860 --> 11:05:50,520 we defined here then we have 2 3 4 and 14924 11:05:50,520 --> 11:05:53,640 we have the end so we have our post fix 14925 11:05:53,640 --> 11:05:56,220 that had defined here now 14926 11:05:56,220 --> 11:05:58,620 for bigger Coalition you may want to 14927 11:05:58,620 --> 11:05:59,880 specify 14928 11:05:59,880 --> 11:06:03,120 limit so a number of elements that will 14929 11:06:03,120 --> 11:06:05,936 be included into the result and if the 14930 11:06:05,936 --> 11:06:08,700 collection exists that size with that 14931 11:06:08,700 --> 11:06:10,620 limit all the other elements will be 14932 11:06:10,620 --> 11:06:13,020 replaced with a single value called the 14933 11:06:13,020 --> 11:06:15,540 truncated argument so let's see what all 14934 11:06:15,540 --> 11:06:18,000 of this means so let's say that I have a 14935 11:06:18,000 --> 11:06:19,620 list called the 14936 11:06:19,620 --> 11:06:23,460 numbers let's type here Val numbers and 14937 11:06:23,460 --> 11:06:26,460 I'm going to create a range from one 14938 11:06:26,460 --> 11:06:29,040 to 100 so what we have here is 100 14939 11:06:29,040 --> 11:06:30,180 numbers 14940 11:06:30,180 --> 11:06:32,640 and let's say that I type here print 14941 11:06:32,640 --> 11:06:33,960 line 14942 11:06:33,960 --> 11:06:36,720 but first let's convert this to a list 14943 11:06:36,720 --> 11:06:38,520 so I'm going to put this in parentheses 14944 11:06:38,520 --> 11:06:41,520 so you put this in parenthesis 14945 11:06:41,520 --> 11:06:45,240 and you put at the end that to list 14946 11:06:45,240 --> 11:06:47,276 and this is going to convert now our 14947 11:06:47,276 --> 11:06:49,436 range into a list and as you can see the 14948 11:06:49,436 --> 11:06:53,160 the type is a list of ins and now if I 14949 11:06:53,160 --> 11:06:55,916 type here numbers 14950 11:06:55,916 --> 11:06:58,620 and if I run this code 14951 11:06:58,620 --> 11:07:03,300 when I get all of our numbers displayed 14952 11:07:07,740 --> 11:07:11,580 now with as I said with the join join 14953 11:07:11,580 --> 11:07:13,860 tostring function we can specify a limit 14954 11:07:13,860 --> 11:07:17,040 and also a truncated this is going to be 14955 11:07:17,040 --> 11:07:19,256 the thing which is going to be sold out 14956 11:07:19,256 --> 11:07:22,916 after our number limits so what I can 14957 11:07:22,916 --> 11:07:27,120 type here is I can type join to string 14958 11:07:27,120 --> 11:07:31,560 and uh which is also 14959 11:07:31,560 --> 11:07:34,500 one with parenthesis so hero type limit 14960 11:07:34,500 --> 11:07:37,320 the named argument limit and let's say 14961 11:07:37,320 --> 11:07:39,960 that I don't want to get all the numbers 14962 11:07:39,960 --> 11:07:43,140 as you can see here from to 100 and I 14963 11:07:43,140 --> 11:07:46,500 want to get only the first 15 elements 14964 11:07:46,500 --> 11:07:50,220 to do this you just type here limit 15. 14965 11:07:50,220 --> 11:07:53,700 then we type truncated and that is going 14966 11:07:53,700 --> 11:07:57,240 to be that this trunky thing is going to 14967 11:07:57,240 --> 11:08:00,240 be showed after our limit number so I'm 14968 11:08:00,240 --> 11:08:03,596 gonna put here uh quotation marks 14969 11:08:03,596 --> 11:08:08,936 less than um sine dot dot and uh 14970 11:08:08,936 --> 11:08:11,756 greater than PSI so now if you run this 14971 11:08:11,756 --> 11:08:16,276 look at what we have in the output 14972 11:08:16,980 --> 11:08:20,040 now we get we only have the first 15 14973 11:08:20,040 --> 11:08:23,276 elements and then for the next elements 14974 11:08:23,276 --> 11:08:25,460 we only have this uh 14975 11:08:25,460 --> 11:08:28,320 string that we typed here this is this 14976 11:08:28,320 --> 11:08:30,120 expression that we have here and we can 14977 11:08:30,120 --> 11:08:31,620 change this if you can change this let's 14978 11:08:31,620 --> 11:08:35,040 say to 25 so if you run this now you'll 14979 11:08:35,040 --> 11:08:39,300 see only the first 25 elements displays 14980 11:08:39,300 --> 11:08:41,480 foreign 14981 11:08:41,840 --> 11:08:44,580 to string with the limit name argument 14982 11:08:44,580 --> 11:08:48,720 and the truncated now let's say that you 14983 11:08:48,720 --> 11:08:50,700 want to customize the representation of 14984 11:08:50,700 --> 11:08:53,460 elements of the elements themselves you 14985 11:08:53,460 --> 11:08:55,916 need to in that case you need to provide 14986 11:08:55,916 --> 11:08:58,620 the transform function so what we can do 14987 11:08:58,620 --> 11:09:02,936 is that we can type here print line 14988 11:09:02,936 --> 11:09:04,500 let's say 14989 11:09:04,500 --> 11:09:08,460 and I'm going to type number strings 14990 11:09:08,460 --> 11:09:12,060 put dot join to string and choose this 14991 11:09:12,060 --> 11:09:13,916 one with curly braces 14992 11:09:13,916 --> 11:09:16,800 and here I'm gonna Define how this uh 14993 11:09:16,800 --> 11:09:18,620 this uh 14994 11:09:18,620 --> 11:09:21,480 list is going to be transform so how's 14995 11:09:21,480 --> 11:09:22,800 going to be changed so I'm going to type 14996 11:09:22,800 --> 11:09:24,060 here 14997 11:09:24,060 --> 11:09:27,540 quotation marks element 14998 11:09:27,540 --> 11:09:29,700 and I'm gonna put 14999 11:09:29,700 --> 11:09:31,500 colon 15000 11:09:31,500 --> 11:09:34,800 we're going to put the dollar sign 15001 11:09:34,800 --> 11:09:36,300 and put it 15002 11:09:36,300 --> 11:09:39,800 dot uppercase so we're gonna uppercase 15003 11:09:39,800 --> 11:09:42,776 all the elements 15004 11:09:42,776 --> 11:09:46,400 so if I run the code now 15005 11:09:51,120 --> 11:09:54,540 get element and get one 15006 11:09:54,540 --> 11:09:57,840 so you can get one uppercase an element 15007 11:09:57,840 --> 11:10:00,240 to get two uppercase element three 15008 11:10:00,240 --> 11:10:03,960 uppercase element four uppercase so this 15009 11:10:03,960 --> 11:10:06,360 is how you can use the transformation uh 15010 11:10:06,360 --> 11:10:09,060 we can this I can transform your list we 15011 11:10:09,060 --> 11:10:11,220 using the joint to string function 15012 11:10:11,220 --> 11:10:13,500 so this is our discussion about the 15013 11:10:13,500 --> 11:10:15,776 string representation and now I'm going 15014 11:10:15,776 --> 11:10:19,080 to move to the next sections so I'm 15015 11:10:19,080 --> 11:10:21,000 gonna end the video now now it's time to 15016 11:10:21,000 --> 11:10:23,400 start a discussion about filtering so 15017 11:10:23,400 --> 11:10:26,276 what is filtering filtering is one of 15018 11:10:26,276 --> 11:10:28,080 the most popular tasks in collection 15019 11:10:28,080 --> 11:10:31,020 processing in kotlin filtering 15020 11:10:31,020 --> 11:10:33,660 conditions are defined by predicates and 15021 11:10:33,660 --> 11:10:36,416 that is Lambda function that take a 15022 11:10:36,416 --> 11:10:39,300 collection element and return a Boolean 15023 11:10:39,300 --> 11:10:41,520 value true means that the given element 15024 11:10:41,520 --> 11:10:44,756 matches the predicate false means the 15025 11:10:44,756 --> 11:10:45,720 opposite 15026 11:10:45,720 --> 11:10:47,880 the standard Library contains a group of 15027 11:10:47,880 --> 11:10:49,620 extension functions that lets you filter 15028 11:10:49,620 --> 11:10:51,960 collection in a single call those 15029 11:10:51,960 --> 11:10:53,460 functions leave the original collection 15030 11:10:53,460 --> 11:10:56,220 unchanged so they are available for both 15031 11:10:56,220 --> 11:10:59,640 multiple and read-only collections to 15032 11:10:59,640 --> 11:11:01,916 operate the filtering result you should 15033 11:11:01,916 --> 11:11:04,020 assign it to a variable or change the 15034 11:11:04,020 --> 11:11:06,120 function after filtering 15035 11:11:06,120 --> 11:11:09,180 now the basic filtering function is 15036 11:11:09,180 --> 11:11:12,060 filter when called with a predicate 15037 11:11:12,060 --> 11:11:14,276 filter Returns the collection elements 15038 11:11:14,276 --> 11:11:18,240 that match it for both list and set the 15039 11:11:18,240 --> 11:11:20,400 resulting Collision is a list for map 15040 11:11:20,400 --> 11:11:22,256 it's a map as well 15041 11:11:22,256 --> 11:11:25,080 now let's Define a variable here call 15042 11:11:25,080 --> 11:11:28,436 number so let's type in val numbers I'm 15043 11:11:28,436 --> 11:11:32,720 going to put equals to list of 15044 11:11:33,916 --> 11:11:37,256 and I'm gonna type here 15045 11:11:37,256 --> 11:11:39,980 one 15046 11:11:40,140 --> 11:11:42,620 two 15047 11:11:45,000 --> 11:11:47,000 three 15048 11:11:47,276 --> 11:11:50,060 and four 15049 11:11:53,040 --> 11:11:56,160 now if you want to filter this list what 15050 11:11:56,160 --> 11:11:57,720 you need to do is let's create another 15051 11:11:57,720 --> 11:12:00,120 variable here called 15052 11:12:00,120 --> 11:12:02,276 longer 15053 11:12:02,276 --> 11:12:05,119 then 15054 11:12:06,596 --> 11:12:08,096 three 15055 11:12:08,096 --> 11:12:10,080 I'm going to put equals I'm going to 15056 11:12:10,080 --> 11:12:11,640 type numbers 15057 11:12:11,640 --> 11:12:15,720 Dot and we type filter 15058 11:12:15,720 --> 11:12:18,120 so filter 15059 11:12:18,120 --> 11:12:20,340 and we choose this one 15060 11:12:20,340 --> 11:12:23,460 and here we defined how our list so we 15061 11:12:23,460 --> 11:12:25,500 will Define the expression which is 15062 11:12:25,500 --> 11:12:28,256 gonna determine how our list is going to 15063 11:12:28,256 --> 11:12:31,200 be is gonna be filtered so I'm gonna 15064 11:12:31,200 --> 11:12:36,776 type here it so the element dot length 15065 11:12:36,776 --> 11:12:39,240 let's say greater than 15066 11:12:39,240 --> 11:12:40,620 3. 15067 11:12:40,620 --> 11:12:43,740 now if we I add the print line down here 15068 11:12:43,740 --> 11:12:46,256 print line 15069 11:12:46,256 --> 11:12:49,860 now this list is going to contain all 15070 11:12:49,860 --> 11:12:54,360 the elements from our numbers list which 15071 11:12:54,360 --> 11:12:57,120 are longer than three so if I put here 15072 11:12:57,120 --> 11:13:01,800 longer than three and if I run this 15073 11:13:01,800 --> 11:13:04,436 we get 3 and 4 because those are the 15074 11:13:04,436 --> 11:13:07,980 only string numbers which have a length 15075 11:13:07,980 --> 11:13:09,776 longer than three so I'm going to close 15076 11:13:09,776 --> 11:13:11,880 the console next let's see how we can 15077 11:13:11,880 --> 11:13:13,380 filter them up and for that I'm going to 15078 11:13:13,380 --> 11:13:15,240 create another variable called numbers 15079 11:13:15,240 --> 11:13:17,720 map 15080 11:13:17,880 --> 11:13:20,119 foreign 15081 11:13:22,096 --> 11:13:24,960 here the key is going to be a string key 15082 11:13:24,960 --> 11:13:26,160 one 15083 11:13:26,160 --> 11:13:28,620 the value is going to be one 15084 11:13:28,620 --> 11:13:31,740 comma then 15085 11:13:31,740 --> 11:13:33,596 key to 15086 11:13:33,596 --> 11:13:35,899 to 15087 11:13:37,140 --> 11:13:39,000 then 15088 11:13:39,000 --> 11:13:41,276 key three 15089 11:13:41,276 --> 11:13:43,256 two three 15090 11:13:43,256 --> 11:13:48,300 and now I'm gonna put key 101 15091 11:13:48,360 --> 11:13:49,756 two 15092 11:13:49,756 --> 11:13:52,080 101 the value 15093 11:13:52,080 --> 11:13:54,180 now if you want to filter now this map 15094 11:13:54,180 --> 11:13:55,740 I'm going to create another variable 15095 11:13:55,740 --> 11:13:59,159 called filtered 15096 11:13:59,640 --> 11:14:01,740 mop and it's going to be equal to our 15097 11:14:01,740 --> 11:14:04,020 numbers map but it's going to be filter 15098 11:14:04,020 --> 11:14:06,840 so I'm going to type DOT number snap 15099 11:14:06,840 --> 11:14:08,460 that filter 15100 11:14:08,460 --> 11:14:10,860 and now I'm going to filter filter the 15101 11:14:10,860 --> 11:14:14,160 map both by the key and by the value so 15102 11:14:14,160 --> 11:14:17,540 we type here it dot key 15103 11:14:17,540 --> 11:14:21,360 dot ends with so we we want to filter it 15104 11:14:21,360 --> 11:14:23,756 first by the key and the one that key to 15105 11:14:23,756 --> 11:14:26,340 ends with a specific character and that 15106 11:14:26,340 --> 11:14:28,800 character is going to be one 15107 11:14:28,800 --> 11:14:33,120 and so we put the operator and we want 15108 11:14:33,120 --> 11:14:37,256 the the value to be greater than 100 so 15109 11:14:37,256 --> 11:14:38,820 we put here it 15110 11:14:38,820 --> 11:14:41,960 dot value 15111 11:14:42,120 --> 11:14:46,860 greater than 100 and the only key which 15112 11:14:46,860 --> 11:14:50,220 satisfy this condition that we find here 15113 11:14:50,220 --> 11:14:53,756 it's only is this key so now if I add 15114 11:14:53,756 --> 11:14:55,680 the print line here 15115 11:14:55,680 --> 11:14:58,080 and if I type filter map so the map 15116 11:14:58,080 --> 11:15:00,120 which was filtered using the expression 15117 11:15:00,120 --> 11:15:01,620 that we defined here 15118 11:15:01,620 --> 11:15:04,740 now if you run this you're gonna see 15119 11:15:04,740 --> 11:15:06,416 gonna see 15120 11:15:06,416 --> 11:15:08,276 this in the output because this is the 15121 11:15:08,276 --> 11:15:09,776 only one which satisfy the condition so 15122 11:15:09,776 --> 11:15:13,740 you get key 101 and we get the value 101 15123 11:15:13,740 --> 11:15:15,480 so this is how you can filter them up 15124 11:15:15,480 --> 11:15:19,620 using the both the key and the value now 15125 11:15:19,620 --> 11:15:22,680 the predicates in filter can only check 15126 11:15:22,680 --> 11:15:24,900 the values of the elements if you want 15127 11:15:24,900 --> 11:15:27,560 to use element position in the filter 15128 11:15:27,560 --> 11:15:31,680 use filter indexed it takes a predicate 15129 11:15:31,680 --> 11:15:33,840 with two arguments the index and the 15130 11:15:33,840 --> 11:15:35,276 value of an element 15131 11:15:35,276 --> 11:15:37,320 to filter correction by a negative 15132 11:15:37,320 --> 11:15:40,140 condition use filter not it's written as 15133 11:15:40,140 --> 11:15:41,640 a list of the elements for which the 15134 11:15:41,640 --> 11:15:44,520 predicate yields false so let's see how 15135 11:15:44,520 --> 11:15:46,200 we can do this so I'm gonna declare two 15136 11:15:46,200 --> 11:15:48,180 variables down here 15137 11:15:48,180 --> 11:15:50,580 file 15138 11:15:50,580 --> 11:15:52,500 filtered 15139 11:15:52,500 --> 11:15:54,776 index 15140 11:15:54,776 --> 11:15:57,720 is going to be equal to numbers 15141 11:15:57,720 --> 11:16:01,200 or a variable that is defined above 15142 11:16:01,200 --> 11:16:03,596 that 15143 11:16:03,596 --> 11:16:06,740 filter index 15144 11:16:09,180 --> 11:16:11,936 and this is gonna take 15145 11:16:11,936 --> 11:16:15,000 two parameters index and the value so 15146 11:16:15,000 --> 11:16:17,660 I'm going to type here 15147 11:16:18,060 --> 11:16:21,680 actually you need to put curly braces 15148 11:16:24,180 --> 11:16:26,840 index 15149 11:16:29,220 --> 11:16:32,700 let's change this to value 15150 11:16:32,700 --> 11:16:36,720 actually let's keep it as s 15151 11:16:36,720 --> 11:16:39,240 Ctrl Z 15152 11:16:39,240 --> 11:16:40,800 and 15153 11:16:40,800 --> 11:16:42,840 here we're gonna Define 15154 11:16:42,840 --> 11:16:49,200 our expression by a by which our list is 15155 11:16:49,200 --> 11:16:50,400 going to be filtered so I'm going to 15156 11:16:50,400 --> 11:16:55,080 type here that first I want the 15157 11:16:55,080 --> 11:16:57,800 index 15158 11:16:57,840 --> 11:17:02,416 to not be equal to zero 15159 11:17:04,320 --> 11:17:06,980 and 15160 11:17:08,640 --> 11:17:11,340 I I also want 15161 11:17:11,340 --> 11:17:14,299 that 15162 11:17:14,640 --> 11:17:17,700 the value of the element so actually 15163 11:17:17,700 --> 11:17:19,620 let's change this to valid to make more 15164 11:17:19,620 --> 11:17:21,596 sense so let's type here to value 15165 11:17:21,596 --> 11:17:24,720 because we're talking about those ones 15166 11:17:24,720 --> 11:17:27,840 so I want the value of that string so 15167 11:17:27,840 --> 11:17:31,200 the value dot length I want I want it to 15168 11:17:31,200 --> 11:17:34,640 be less than a 5. 15169 11:17:34,740 --> 11:17:36,960 all right so this is the transformation 15170 11:17:36,960 --> 11:17:40,256 if you want this is not a transformation 15171 11:17:40,256 --> 11:17:43,096 this is how we want to filter our 15172 11:17:43,096 --> 11:17:45,720 numbers list that we defined above I 15173 11:17:45,720 --> 11:17:46,916 want to 15174 11:17:46,916 --> 11:17:49,620 filter both by the index so this is why 15175 11:17:49,620 --> 11:17:51,960 we use the filter index and also by the 15176 11:17:51,960 --> 11:17:52,800 value 15177 11:17:52,800 --> 11:17:55,560 and I want the index to not be equal to 15178 11:17:55,560 --> 11:17:57,960 zero this is the first condition for the 15179 11:17:57,960 --> 11:18:01,200 filtering and I also want so use the end 15180 11:18:01,200 --> 11:18:04,140 operator that the valued of the element 15181 11:18:04,140 --> 11:18:07,140 the length to be less than five now if I 15182 11:18:07,140 --> 11:18:11,400 print this if I added here a print line 15183 11:18:11,400 --> 11:18:15,680 but first let's Also let's also use the 15184 11:18:15,680 --> 11:18:18,416 uh filter not so I'm going to type here 15185 11:18:18,416 --> 11:18:20,340 Val 15186 11:18:20,340 --> 11:18:23,119 foreign 15187 11:18:24,300 --> 11:18:26,700 filtered nut 15188 11:18:26,700 --> 11:18:30,560 and I'm going to type equals numbers 15189 11:18:30,900 --> 11:18:34,756 dot filter not 15190 11:18:35,520 --> 11:18:38,220 curly braces 15191 11:18:38,220 --> 11:18:41,580 and we type here it dot 15192 11:18:41,580 --> 11:18:42,960 length 15193 11:18:42,960 --> 11:18:45,960 so the value of the element 15194 11:18:45,960 --> 11:18:49,916 is to be less than equal to 3. 15195 11:18:49,916 --> 11:18:52,020 now if we output those two in the 15196 11:18:52,020 --> 11:18:54,120 console so if I add here print line and 15197 11:18:54,120 --> 11:18:57,776 I'm going to Output filter 15198 11:19:00,000 --> 11:19:02,160 Index this should be filtered in the 15199 11:19:02,160 --> 11:19:05,240 solicit Factor this 15200 11:19:09,300 --> 11:19:14,276 so let's type here in the index 15201 11:19:15,840 --> 11:19:17,520 I type it already 15202 11:19:17,520 --> 11:19:19,680 so let's type here 15203 11:19:19,680 --> 11:19:22,140 edex actually 15204 11:19:22,140 --> 11:19:25,159 it will make more sense 15205 11:19:26,880 --> 11:19:31,620 but I have Elixir so why anyway so I'm 15206 11:19:31,620 --> 11:19:34,140 gonna output that first then I'm gonna 15207 11:19:34,140 --> 11:19:35,756 hear a print line 15208 11:19:35,756 --> 11:19:39,540 and I'm gonna type filtered not 15209 11:19:39,540 --> 11:19:42,120 and filter not as I said is gonna return 15210 11:19:42,120 --> 11:19:44,096 a list of the elements for which the 15211 11:19:44,096 --> 11:19:46,500 predicate yields false 15212 11:19:46,500 --> 11:19:49,436 now if you run those two look what 15213 11:19:49,436 --> 11:19:53,300 you're gonna have on the output 15214 11:19:54,776 --> 11:19:58,680 so this is the output for our first for 15215 11:19:58,680 --> 11:20:00,596 this print line and for this print line 15216 11:20:00,596 --> 11:20:02,160 so let's think about what we have here 15217 11:20:02,160 --> 11:20:05,040 first we have two and four 15218 11:20:05,040 --> 11:20:08,820 and we Define the filter here first the 15219 11:20:08,820 --> 11:20:10,500 index which should not be equal to zero 15220 11:20:10,500 --> 11:20:15,000 so the one is first excluded and we also 15221 11:20:15,000 --> 11:20:17,756 want to that to the value that length to 15222 11:20:17,756 --> 11:20:20,220 be less than five so the only ones which 15223 11:20:20,220 --> 11:20:22,860 uh for which the value is less than five 15224 11:20:22,860 --> 11:20:27,300 is the 2 and 4 because the length of the 15225 11:20:27,300 --> 11:20:29,700 three is bigger than 5 because it has a 15226 11:20:29,700 --> 11:20:34,756 length of 0 1 2 3 15227 11:20:34,756 --> 11:20:37,916 4 and 15228 11:20:37,916 --> 11:20:40,380 the length is 5 but I want the length to 15229 11:20:40,380 --> 11:20:42,596 be less than five not less than equal 15230 11:20:42,596 --> 11:20:44,220 than five so this is why this is not 15231 11:20:44,220 --> 11:20:47,220 included and you only have two and four 15232 11:20:47,220 --> 11:20:49,020 next 15233 11:20:49,020 --> 11:20:51,776 uh now use the filter nut and it's gonna 15234 11:20:51,776 --> 11:20:54,240 as I said it's gonna return a list of 15235 11:20:54,240 --> 11:20:56,640 elements for which the predicate for 15236 11:20:56,640 --> 11:20:58,680 each this expression is gonna yield 15237 11:20:58,680 --> 11:21:02,700 false so it's gonna go through all of 15238 11:21:02,700 --> 11:21:05,340 our elements and it's gonna return the 15239 11:21:05,340 --> 11:21:08,700 elements for which this is false so we 15240 11:21:08,700 --> 11:21:10,020 get three 15241 11:21:10,020 --> 11:21:13,560 N4 because those are the only ones which 15242 11:21:13,560 --> 11:21:17,340 are not less than equal to three they 15243 11:21:17,340 --> 11:21:19,080 are greater than equal to three so this 15244 11:21:19,080 --> 11:21:20,700 is why we have here three and four 15245 11:21:20,700 --> 11:21:23,580 because this returns if Returns the 15246 11:21:23,580 --> 11:21:27,240 elements if the uh the the length of the 15247 11:21:27,240 --> 11:21:29,276 element is less than equal to 3 so this 15248 11:21:29,276 --> 11:21:32,276 is why you get here 3 and 4. 15249 11:21:32,276 --> 11:21:33,480 and 15250 11:21:33,480 --> 11:21:35,880 there are also functions that narrow the 15251 11:21:35,880 --> 11:21:38,460 element type by filtering elements of a 15252 11:21:38,460 --> 11:21:40,740 given type so there is a function called 15253 11:21:40,740 --> 11:21:44,820 filter is instance which returns a 15254 11:21:44,820 --> 11:21:46,680 collection of filaments of a given type 15255 11:21:46,680 --> 11:21:50,580 being called so let's see how you can 15256 11:21:50,580 --> 11:21:52,680 use that but first let's create a 15257 11:21:52,680 --> 11:21:54,720 Val mixed 15258 11:21:54,720 --> 11:21:57,900 let's call it mixed 15259 11:21:57,900 --> 11:21:59,160 list 15260 11:21:59,160 --> 11:22:01,560 I'm gonna put equals another card type 15261 11:22:01,560 --> 11:22:03,900 list off 15262 11:22:03,900 --> 11:22:05,820 and I'm gonna Define some mixed elements 15263 11:22:05,820 --> 11:22:07,256 here so I'm going to type here let's say 15264 11:22:07,256 --> 11:22:08,936 one 15265 11:22:08,936 --> 11:22:14,160 two three then some charts like 15266 11:22:14,160 --> 11:22:16,820 a 15267 11:22:18,480 --> 11:22:20,840 B 15268 11:22:21,900 --> 11:22:23,060 C 15269 11:22:23,060 --> 11:22:26,580 now so on strings let's type here 15270 11:22:26,580 --> 11:22:28,436 hello 15271 11:22:28,436 --> 11:22:31,620 World another string 15272 11:22:31,620 --> 11:22:37,096 Alex and let's also type 15273 11:22:37,320 --> 11:22:39,660 a Boolean so let's type here let's say 15274 11:22:39,660 --> 11:22:41,936 false 15275 11:22:41,936 --> 11:22:45,776 now there is a function as I said which 15276 11:22:45,776 --> 11:22:48,776 which Narrows the element type by 15277 11:22:48,776 --> 11:22:51,120 filtering the elements of a given type 15278 11:22:51,120 --> 11:22:52,740 so we can use this function called 15279 11:22:52,740 --> 11:22:56,220 filter is instance so what you can do 15280 11:22:56,220 --> 11:23:00,360 here is we can we can type 15281 11:23:00,360 --> 11:23:03,480 mixed list 15282 11:23:03,480 --> 11:23:04,980 Dot 15283 11:23:04,980 --> 11:23:06,596 filter 15284 11:23:06,596 --> 11:23:10,159 is instance 15285 11:23:11,820 --> 11:23:17,276 so we choose I think this one not this 15286 11:23:17,276 --> 11:23:18,416 one 15287 11:23:18,416 --> 11:23:21,916 filter is instance 15288 11:23:22,096 --> 11:23:26,580 of Y so mixture is that 15289 11:23:26,580 --> 11:23:29,120 filter 15290 11:23:29,580 --> 11:23:32,840 is instance 15291 11:23:35,596 --> 11:23:39,180 and here with inside the angle brackets 15292 11:23:39,180 --> 11:23:41,340 we Define the generic type for which 15293 11:23:41,340 --> 11:23:42,960 this list is going to be filtered so 15294 11:23:42,960 --> 11:23:46,140 let's first choose the chart Char then 15295 11:23:46,140 --> 11:23:49,380 I'm going to put that for each so I'm 15296 11:23:49,380 --> 11:23:51,540 going to use the for each Loop 15297 11:23:51,540 --> 11:23:54,240 and I'm gonna add print line 15298 11:23:54,240 --> 11:23:56,756 and I'm going to type it here so I'm 15299 11:23:56,756 --> 11:23:58,620 going to type the parameter 15300 11:23:58,620 --> 11:24:00,900 which is the each chart so now if I run 15301 11:24:00,900 --> 11:24:04,020 this we will get only the charts being 15302 11:24:04,020 --> 11:24:06,240 displayed here because it is filtering 15303 11:24:06,240 --> 11:24:08,520 by using the instance of charge so you 15304 11:24:08,520 --> 11:24:12,000 get the charge if I change this to 15305 11:24:12,000 --> 11:24:15,000 to in now I'll get the integers from our 15306 11:24:15,000 --> 11:24:18,560 mixed list so if I run this 15307 11:24:22,140 --> 11:24:24,360 now get the integers one two three that 15308 11:24:24,360 --> 11:24:25,800 we have defined here 15309 11:24:25,800 --> 11:24:28,860 and if I change this to let's say a 15310 11:24:28,860 --> 11:24:31,620 string I will get the strings inside our 15311 11:24:31,620 --> 11:24:34,020 mixed list because filter is instance 15312 11:24:34,020 --> 11:24:37,080 it's gonna take the specific title that 15313 11:24:37,080 --> 11:24:39,000 you type here and then it's gonna Loop 15314 11:24:39,000 --> 11:24:41,936 through all of those types which are of 15315 11:24:41,936 --> 11:24:44,340 this type in this list so if I run this 15316 11:24:44,340 --> 11:24:48,560 I'm gonna see Hello World Analytics 15317 11:24:50,880 --> 11:24:54,596 so we get hello world and Alex also I 15318 11:24:54,596 --> 11:24:57,840 can filter for Boolean values if I type 15319 11:24:57,840 --> 11:24:59,756 here Boolean is gonna 15320 11:24:59,756 --> 11:25:02,460 it's gonna output in the closet or the 15321 11:25:02,460 --> 11:25:03,900 one Boolean value that we have here 15322 11:25:03,900 --> 11:25:07,460 which is false so if I run this 15323 11:25:10,500 --> 11:25:12,416 I get false so this is how you can use 15324 11:25:12,416 --> 11:25:15,840 the filter is instant function on types 15325 11:25:15,840 --> 11:25:19,020 to filter the mixed list and you can 15326 11:25:19,020 --> 11:25:21,776 also put that because this Returns the 15327 11:25:21,776 --> 11:25:24,480 list this entire thing a filter at least 15328 11:25:24,480 --> 11:25:27,240 you can put that for each to call the 15329 11:25:27,240 --> 11:25:30,540 the this the for this for each Lambda 15330 11:25:30,540 --> 11:25:33,120 function and then we output them in the 15331 11:25:33,120 --> 11:25:34,860 console 15332 11:25:34,860 --> 11:25:38,340 uh the next thing that you can look and 15333 11:25:38,340 --> 11:25:40,256 that we're gonna look at is called 15334 11:25:40,256 --> 11:25:42,300 Partition so I'm going to add a comment 15335 11:25:42,300 --> 11:25:44,276 here but I'm going to hide the console 15336 11:25:44,276 --> 11:25:46,620 first so I'm going to put here a comment 15337 11:25:46,620 --> 11:25:51,380 I'm going to type here partition part 15338 11:25:51,380 --> 11:25:54,596 T1 so partition 15339 11:25:54,596 --> 11:25:57,900 and this is an alter another filtering 15340 11:25:57,900 --> 11:26:00,660 function called Partition and it filters 15341 11:26:00,660 --> 11:26:03,540 a collection by a predicate and it keeps 15342 11:26:03,540 --> 11:26:05,756 the elements that don't match in a 15343 11:26:05,756 --> 11:26:07,140 separate list 15344 11:26:07,140 --> 11:26:10,980 so you have a pair of lists as a return 15345 11:26:10,980 --> 11:26:13,140 value the first list is containing the 15346 11:26:13,140 --> 11:26:15,660 element that match the predicate the 15347 11:26:15,660 --> 11:26:17,640 predicate and the second one is 15348 11:26:17,640 --> 11:26:19,620 containing everything else from the 15349 11:26:19,620 --> 11:26:21,660 original collection so I'm gonna take 15350 11:26:21,660 --> 11:26:25,436 again our numbers that have defined the 15351 11:26:25,436 --> 11:26:27,776 path so I'm going to type here 15352 11:26:27,776 --> 11:26:29,276 uh 15353 11:26:29,276 --> 11:26:33,300 file and to to get this partition we put 15354 11:26:33,300 --> 11:26:36,436 we put parentheses and here to type 15355 11:26:36,436 --> 11:26:38,936 match so this is going to be the values 15356 11:26:38,936 --> 11:26:40,640 which are going to be 15357 11:26:40,640 --> 11:26:44,220 matched by the predicate and 15358 11:26:44,220 --> 11:26:46,080 I'm gonna type here rest and you're 15359 11:26:46,080 --> 11:26:47,700 gonna be stored the rest of the value 15360 11:26:47,700 --> 11:26:50,040 which which don't match the expression 15361 11:26:50,040 --> 11:26:52,560 the predicate that's going to be defined 15362 11:26:52,560 --> 11:26:55,080 and I put here equals 15363 11:26:55,080 --> 11:26:57,596 numbers 15364 11:26:57,596 --> 11:27:00,840 dot partition 15365 11:27:00,840 --> 11:27:03,720 and we type here it 15366 11:27:03,720 --> 11:27:06,380 Dot 15367 11:27:07,320 --> 11:27:10,040 length 15368 11:27:10,200 --> 11:27:11,820 greater 15369 11:27:11,820 --> 11:27:13,320 than 3. 15370 11:27:13,320 --> 11:27:15,840 now if I 15371 11:27:15,840 --> 11:27:18,300 other print line here 15372 11:27:18,300 --> 11:27:21,360 I can print first the match 15373 11:27:21,360 --> 11:27:24,596 let's press Ctrl D and now I can print 15374 11:27:24,596 --> 11:27:26,520 the rest of the elements which don't 15375 11:27:26,520 --> 11:27:29,096 match this this condition this 15376 11:27:29,096 --> 11:27:31,020 expression that is defined here so if I 15377 11:27:31,020 --> 11:27:32,640 run this code 15378 11:27:32,640 --> 11:27:34,860 you will see first all the elements 15379 11:27:34,860 --> 11:27:36,596 which are greater than 3 and then we're 15380 11:27:36,596 --> 11:27:37,916 gonna see the rest of the elements which 15381 11:27:37,916 --> 11:27:41,096 are not greater than 3. but let's add 15382 11:27:41,096 --> 11:27:43,500 the space here to make things more 15383 11:27:43,500 --> 11:27:44,700 clearly so we're going to add a hero 15384 11:27:44,700 --> 11:27:46,500 print line 15385 11:27:46,500 --> 11:27:50,700 quotation marks for recession so let's 15386 11:27:50,700 --> 11:27:53,479 run this again 15387 11:27:57,660 --> 11:28:00,840 so we get three and four so those are 15388 11:28:00,840 --> 11:28:04,040 the elements which are coming from our 15389 11:28:04,040 --> 11:28:09,060 match list which is defined here uh 15390 11:28:09,060 --> 11:28:11,700 which is uh which satisfy this condition 15391 11:28:11,700 --> 11:28:14,276 which is greater than 3 so we have three 15392 11:28:14,276 --> 11:28:16,256 and four so the length is greater than 15393 11:28:16,256 --> 11:28:17,400 three 15394 11:28:17,400 --> 11:28:19,800 and then we have one and two so those 15395 11:28:19,800 --> 11:28:21,960 are the rest of the elements which are 15396 11:28:21,960 --> 11:28:24,720 not satisfying this condition that is 15397 11:28:24,720 --> 11:28:27,416 defined here so we have one and two so 15398 11:28:27,416 --> 11:28:28,980 this is how you can use the partition 15399 11:28:28,980 --> 11:28:30,720 function to 15400 11:28:30,720 --> 11:28:34,320 to get the 15401 11:28:34,320 --> 11:28:37,380 basically to get a pair in which the 15402 11:28:37,380 --> 11:28:39,660 return of value the first is the first 15403 11:28:39,660 --> 11:28:41,276 is the list containing the elements that 15404 11:28:41,276 --> 11:28:43,080 match the predicate and the same code is 15405 11:28:43,080 --> 11:28:44,700 the one containing everything else from 15406 11:28:44,700 --> 11:28:46,200 the original collection 15407 11:28:46,200 --> 11:28:48,360 so this is how you can use the the 15408 11:28:48,360 --> 11:28:50,400 partition function so 15409 11:28:50,400 --> 11:28:52,916 now I'm gonna end the video and see you 15410 11:28:52,916 --> 11:28:54,660 in the next video now it's time to look 15411 11:28:54,660 --> 11:28:57,416 at testing predicates and those are 15412 11:28:57,416 --> 11:28:59,460 functions that simply test the predicate 15413 11:28:59,460 --> 11:29:01,140 against the collection elements and we 15414 11:29:01,140 --> 11:29:03,116 have three functions any 15415 11:29:03,116 --> 11:29:05,220 which returns true if at least one of 15416 11:29:05,220 --> 11:29:07,320 the element matches the given predicate 15417 11:29:07,320 --> 11:29:09,960 then we have none which returns true if 15418 11:29:09,960 --> 11:29:11,880 none of the elements match the given 15419 11:29:11,880 --> 11:29:14,096 predicate and then we have all which 15420 11:29:14,096 --> 11:29:15,776 returns true if all of the elements 15421 11:29:15,776 --> 11:29:18,240 match the given predicate so let's test 15422 11:29:18,240 --> 11:29:20,160 all of them so I'm gonna type here print 15423 11:29:20,160 --> 11:29:22,460 line 15424 11:29:22,800 --> 11:29:25,640 numbers 15425 11:29:25,680 --> 11:29:30,980 that let's start with any so we put any 15426 11:29:31,320 --> 11:29:37,200 so here we type it dot ends 15427 11:29:37,200 --> 11:29:38,520 with 15428 11:29:38,520 --> 11:29:41,580 and let's put let's say 15429 11:29:41,580 --> 11:29:44,116 e 15430 11:29:45,480 --> 11:29:49,500 then out of another print line 15431 11:29:49,500 --> 11:29:51,116 numbers 15432 11:29:51,116 --> 11:29:54,200 dot none 15433 11:29:54,900 --> 11:29:57,060 it should be 15434 11:29:57,060 --> 11:29:58,860 curly braces 15435 11:29:58,860 --> 11:30:00,360 eat 15436 11:30:00,360 --> 11:30:03,060 Dot and sweet 15437 11:30:03,060 --> 11:30:05,820 let's say w 15438 11:30:05,820 --> 11:30:09,256 and print line 15439 11:30:10,380 --> 11:30:12,000 numbers 15440 11:30:12,000 --> 11:30:14,160 dot all 15441 11:30:14,160 --> 11:30:17,700 let's say it dot length 15442 11:30:17,700 --> 11:30:20,880 so if if the length of all the elements 15443 11:30:20,880 --> 11:30:23,400 is greater than one 15444 11:30:23,400 --> 11:30:25,680 so now if you run this 15445 11:30:25,680 --> 11:30:28,680 all of those gonna return either true or 15446 11:30:28,680 --> 11:30:30,916 false 15447 11:30:33,060 --> 11:30:35,820 so get true true and true this is 15448 11:30:35,820 --> 11:30:39,116 because first testing any and then this 15449 11:30:39,116 --> 11:30:40,560 returns true with at least one of the 15450 11:30:40,560 --> 11:30:42,300 elements much in the given predicate so 15451 11:30:42,300 --> 11:30:45,180 it is true we have 15452 11:30:45,180 --> 11:30:48,300 two elements which ends with the E we 15453 11:30:48,300 --> 11:30:51,240 have three and one so that's why you 15454 11:30:51,240 --> 11:30:52,680 return true there 15455 11:30:52,680 --> 11:30:56,580 then we check to see if uh none of the 15456 11:30:56,580 --> 11:30:58,740 elements ends with W so we're checking 15457 11:30:58,740 --> 11:31:01,320 to see if none of the elements uh in our 15458 11:31:01,320 --> 11:31:04,860 numbers ends with w n is true not only 15459 11:31:04,860 --> 11:31:06,660 for elements will end through double so 15460 11:31:06,660 --> 11:31:09,116 we have two in the output 15461 11:31:09,116 --> 11:31:11,936 then we check numbers.org so we check 15462 11:31:11,936 --> 11:31:14,340 into if all the elements the line the 15463 11:31:14,340 --> 11:31:16,320 length of all the elements the element 15464 11:31:16,320 --> 11:31:18,060 strings that we have here one two and 15465 11:31:18,060 --> 11:31:20,340 three are greater than one and this row 15466 11:31:20,340 --> 11:31:22,616 all of them are greater than one so this 15467 11:31:22,616 --> 11:31:24,720 one have true true intro 15468 11:31:24,720 --> 11:31:26,936 so this is how you can use the test 15469 11:31:26,936 --> 11:31:30,960 predicates uh on your collections 15470 11:31:30,960 --> 11:31:32,880 first time to look at the plus and minus 15471 11:31:32,880 --> 11:31:35,820 operators so in Catherine plus and minus 15472 11:31:35,820 --> 11:31:38,580 operators are defined for collections 15473 11:31:38,580 --> 11:31:40,500 they take a collection as the first 15474 11:31:40,500 --> 11:31:42,840 operand and the second operand can be 15475 11:31:42,840 --> 11:31:44,640 either an element or another collection 15476 11:31:44,640 --> 11:31:46,740 the return value is a new read-only 15477 11:31:46,740 --> 11:31:48,416 collection and let's look at an example 15478 11:31:48,416 --> 11:31:51,240 on this I'm going to type here Val 15479 11:31:51,240 --> 11:31:53,160 numbers 15480 11:31:53,160 --> 11:31:55,320 you want to put equals two so now I'm 15481 11:31:55,320 --> 11:31:58,500 going to type your Multiple List off 15482 11:31:58,500 --> 11:32:01,256 Multiple List at least with we to which 15483 11:32:01,256 --> 11:32:03,360 you can add and remove elements and I'm 15484 11:32:03,360 --> 11:32:05,040 gonna type here 15485 11:32:05,040 --> 11:32:07,756 one 15486 11:32:08,340 --> 11:32:10,460 two 15487 11:32:11,096 --> 11:32:13,096 three 15488 11:32:13,916 --> 11:32:16,080 and four now if you want to add an 15489 11:32:16,080 --> 11:32:18,720 element to this list you need to type 15490 11:32:18,720 --> 11:32:21,596 here numbers dot add 15491 11:32:21,596 --> 11:32:23,880 and here you need to specify let's say 15492 11:32:23,880 --> 11:32:26,640 five and now this new element is going 15493 11:32:26,640 --> 11:32:28,800 to be added to the list but with the 15494 11:32:28,800 --> 11:32:30,776 plus and minus operator you can do we 15495 11:32:30,776 --> 11:32:33,180 can do this in an another way so you can 15496 11:32:33,180 --> 11:32:34,616 type here 15497 11:32:34,616 --> 11:32:36,480 uh 15498 11:32:36,480 --> 11:32:38,460 but I'm gonna put this in another list 15499 11:32:38,460 --> 11:32:41,040 so you can type here Val 15500 11:32:41,040 --> 11:32:43,936 plus list 15501 11:32:44,520 --> 11:32:48,060 so we put equals to and type numbers 15502 11:32:48,060 --> 11:32:49,616 Plus 15503 11:32:49,616 --> 11:32:52,140 and to type five and now this element is 15504 11:32:52,140 --> 11:32:55,680 going to be added to our list that we 15505 11:32:55,680 --> 11:32:57,660 have here it's going to be added to the 15506 11:32:57,660 --> 11:33:00,000 the string five and it's going to be 15507 11:33:00,000 --> 11:33:02,460 stored in this plus list now let's look 15508 11:33:02,460 --> 11:33:05,096 at the minus operator here Val I'm going 15509 11:33:05,096 --> 11:33:08,540 to call this minus list 15510 11:33:09,180 --> 11:33:11,700 and put equals 2 and I'm going to type 15511 11:33:11,700 --> 11:33:14,340 numbers so now I'm going to use the 15512 11:33:14,340 --> 11:33:16,740 minus operator so I put minus 15513 11:33:16,740 --> 11:33:19,436 and we type minus 15514 11:33:19,436 --> 11:33:21,596 multiple list of 15515 11:33:21,596 --> 11:33:23,580 and we put here 15516 11:33:23,580 --> 11:33:27,240 let's say three and four so we type here 15517 11:33:27,240 --> 11:33:29,596 three 15518 11:33:29,700 --> 11:33:32,479 and four 15519 11:33:33,416 --> 11:33:35,756 I have an underline let's see what it 15520 11:33:35,756 --> 11:33:37,860 says 15521 11:33:37,860 --> 11:33:40,560 now anyway let's run our code to see 15522 11:33:40,560 --> 11:33:43,520 what to get in the output 15523 11:33:44,096 --> 11:33:45,840 but we need to add some print lines 15524 11:33:45,840 --> 11:33:48,180 together so 15525 11:33:48,180 --> 11:33:50,340 I'm gonna add here a print line 15526 11:33:50,340 --> 11:33:53,115 plus list 15527 11:33:57,900 --> 11:34:01,580 and another print line 15528 11:34:02,276 --> 11:34:06,799 minus list now if I run this again 15529 11:34:11,936 --> 11:34:15,776 we get one two three four and our 15530 11:34:15,776 --> 11:34:18,180 Five Element added to our numbers list 15531 11:34:18,180 --> 11:34:20,276 from our class list which is outputted 15532 11:34:20,276 --> 11:34:21,180 here 15533 11:34:21,180 --> 11:34:25,380 and then we get 1 and 2 because 3 and 4 15534 11:34:25,380 --> 11:34:27,660 were removed using the minus operator so 15535 11:34:27,660 --> 11:34:29,276 this is how you can use the minus and 15536 11:34:29,276 --> 11:34:31,800 plus operators with the collections 15537 11:34:31,800 --> 11:34:34,680 so this is our discussion about the plus 15538 11:34:34,680 --> 11:34:36,840 and minus operators and I'm going to 15539 11:34:36,840 --> 11:34:38,460 look at we're gonna 15540 11:34:38,460 --> 11:34:39,900 uh 15541 11:34:39,900 --> 11:34:42,060 move to the next section so I'm going to 15542 11:34:42,060 --> 11:34:43,860 close the console and I'm going to end 15543 11:34:43,860 --> 11:34:45,720 the video 15544 11:34:45,720 --> 11:34:47,340 now it's time to start a discussion 15545 11:34:47,340 --> 11:34:49,616 about grouping so the kotlin standard 15546 11:34:49,616 --> 11:34:51,596 Library provides extension functions for 15547 11:34:51,596 --> 11:34:54,416 grouping collection elements the basic 15548 11:34:54,416 --> 11:34:56,580 function Group by takes a Lambda 15549 11:34:56,580 --> 11:34:59,096 function and returns them up in this map 15550 11:34:59,096 --> 11:35:01,800 each key is the Lambda result so the 15551 11:35:01,800 --> 11:35:03,540 result of that expression inside the 15552 11:35:03,540 --> 11:35:05,160 Lambda function 15553 11:35:05,160 --> 11:35:07,740 and the corresponding values is the list 15554 11:35:07,740 --> 11:35:09,720 of filaments on which this result is 15555 11:35:09,720 --> 11:35:12,596 returned this function can be used for 15556 11:35:12,596 --> 11:35:14,936 example to group a list of strings by 15557 11:35:14,936 --> 11:35:16,140 the first letter 15558 11:35:16,140 --> 11:35:19,140 you can also call Group by with a second 15559 11:35:19,140 --> 11:35:21,540 Lambda argument a value transformation 15560 11:35:21,540 --> 11:35:24,720 function in the result map of group y 15561 11:35:24,720 --> 11:35:28,320 with two lambdas the key is produced by 15562 11:35:28,320 --> 11:35:30,840 key produced by key selector function 15563 11:35:30,840 --> 11:35:33,000 are mapped to the result of the value 15564 11:35:33,000 --> 11:35:35,160 transformation function instead of the 15565 11:35:35,160 --> 11:35:37,080 original elements 15566 11:35:37,080 --> 11:35:39,596 so let's see what all of this means by 15567 11:35:39,596 --> 11:35:41,580 looking at an example so if I type here 15568 11:35:41,580 --> 11:35:44,000 Val 15569 11:35:46,616 --> 11:35:48,060 numbers 15570 11:35:48,060 --> 11:35:50,220 and I'm going to put 15571 11:35:50,220 --> 11:35:53,000 list of 15572 11:35:55,200 --> 11:35:58,140 again one 15573 11:35:58,140 --> 11:36:00,140 two 15574 11:36:03,596 --> 11:36:05,936 three 15575 11:36:05,936 --> 11:36:08,400 four 15576 11:36:08,400 --> 11:36:11,300 and five 15577 11:36:12,720 --> 11:36:15,960 now we I can add here a print line 15578 11:36:15,960 --> 11:36:19,276 I can type numbers 15579 11:36:19,560 --> 11:36:24,799 and I can type here dot Group by 15580 11:36:25,560 --> 11:36:28,320 and I'm gonna group it by 15581 11:36:28,320 --> 11:36:32,400 it dot first so the first letter in in 15582 11:36:32,400 --> 11:36:36,116 one of our elements that 15583 11:36:36,116 --> 11:36:38,520 uppercase 15584 11:36:38,520 --> 11:36:40,256 and now we're going to look at the next 15585 11:36:40,256 --> 11:36:43,560 the next Group by with the key selector 15586 11:36:43,560 --> 11:36:47,276 and with the transformation function so 15587 11:36:47,276 --> 11:36:50,720 I'm going to type here print line 15588 11:36:51,540 --> 11:36:55,140 numbers Dot 15589 11:36:55,740 --> 11:36:58,579 group y 15590 11:37:00,300 --> 11:37:02,220 so we have this one key selector so you 15591 11:37:02,220 --> 11:37:04,256 type here key selector 15592 11:37:04,256 --> 11:37:05,820 equals 15593 11:37:05,820 --> 11:37:09,256 and here we put 15594 11:37:09,776 --> 11:37:13,080 curly braces and we type eat that first 15595 11:37:13,080 --> 11:37:16,560 so the first letter in our element so 15596 11:37:16,560 --> 11:37:19,320 first it should be here 15597 11:37:19,320 --> 11:37:23,360 we put a comma 15598 11:37:24,300 --> 11:37:27,596 and we type value so the how the value 15599 11:37:27,596 --> 11:37:30,540 of the the value of this list is going 15600 11:37:30,540 --> 11:37:32,820 to are going to be transformed and here 15601 11:37:32,820 --> 11:37:35,220 we also put curly braces and we put it 15602 11:37:35,220 --> 11:37:38,580 that uppercase so put it curly braces it 15603 11:37:38,580 --> 11:37:39,720 should be 15604 11:37:39,720 --> 11:37:43,740 it dot uppercase 15605 11:37:43,740 --> 11:37:46,860 let's press Ctrl alt L to format code 15606 11:37:46,860 --> 11:37:49,200 and now let's run our code to see what 15607 11:37:49,200 --> 11:37:52,039 we get in the output 15608 11:37:57,776 --> 11:37:59,756 so we get 15609 11:37:59,756 --> 11:38:02,040 for the first one number the numbers are 15610 11:38:02,040 --> 11:38:04,080 grouped by for uh and we have this 15611 11:38:04,080 --> 11:38:06,660 Lambda it that first.apper case it takes 15612 11:38:06,660 --> 11:38:08,756 the first letter in Upper cases so it 15613 11:38:08,756 --> 11:38:11,640 gets whoa whoa so then you get one 15614 11:38:11,640 --> 11:38:15,660 we get T we get to we get F we get four 15615 11:38:15,660 --> 11:38:19,080 we get uh then we use this one which on 15616 11:38:19,080 --> 11:38:22,860 which we we uh you we we group uh 15617 11:38:22,860 --> 11:38:25,380 elements with a second Lambda argument 15618 11:38:25,380 --> 11:38:27,116 with the value transformation function 15619 11:38:27,116 --> 11:38:30,000 and the result uh map of group y with 15620 11:38:30,000 --> 11:38:32,460 two lambdas are the keys produced by the 15621 11:38:32,460 --> 11:38:34,680 key selector and the function are the 15622 11:38:34,680 --> 11:38:36,596 function are mapped to the result of the 15623 11:38:36,596 --> 11:38:39,240 value transformation function so the 15624 11:38:39,240 --> 11:38:41,460 results are mapped to this 15625 11:38:41,460 --> 11:38:43,500 transformation that is defined here so 15626 11:38:43,500 --> 11:38:47,276 this this is what creates the keys this 15627 11:38:47,276 --> 11:38:49,560 is what creates the key this uh Lambda 15628 11:38:49,560 --> 11:38:52,560 function and this is what creates the 15629 11:38:52,560 --> 11:38:56,096 the the the the values so we get all 15630 11:38:56,096 --> 11:38:58,200 because you get in that first so the 15631 11:38:58,200 --> 11:39:00,480 first letter and get one because this is 15632 11:39:00,480 --> 11:39:02,580 the value so the value is uppercased we 15633 11:39:02,580 --> 11:39:05,880 get one so we get one here then we get 15634 11:39:05,880 --> 11:39:08,640 again eat that first for the for all men 15635 11:39:08,640 --> 11:39:10,860 so you get T then you get two and I get 15636 11:39:10,860 --> 11:39:14,756 uppercased and get F lowercase f and get 15637 11:39:14,756 --> 11:39:18,240 four and uh and five 15638 11:39:18,240 --> 11:39:20,880 so this is our discussion about grouping 15639 11:39:20,880 --> 11:39:23,880 and uh my suggestion for you is to play 15640 11:39:23,880 --> 11:39:26,460 with the code try different values for 15641 11:39:26,460 --> 11:39:29,460 the key selector for the value transform 15642 11:39:29,460 --> 11:39:32,700 for uh try to group The Elements by 15643 11:39:32,700 --> 11:39:35,936 different expressions and just play with 15644 11:39:35,936 --> 11:39:39,300 the code until you get uh get uh until 15645 11:39:39,300 --> 11:39:42,000 you get comfortable with the code now 15646 11:39:42,000 --> 11:39:43,380 it's time to start a discussion about 15647 11:39:43,380 --> 11:39:45,660 the driven collection parts and the 15648 11:39:45,660 --> 11:39:47,340 quoting standard Library contest 15649 11:39:47,340 --> 11:39:49,256 accession functions for retrieving parts 15650 11:39:49,256 --> 11:39:50,700 of a collection 15651 11:39:50,700 --> 11:39:52,740 these functions provide a variety of 15652 11:39:52,740 --> 11:39:54,540 ways to select elements for the result 15653 11:39:54,540 --> 11:39:56,220 collection listing their position 15654 11:39:56,220 --> 11:39:59,040 explicitly specifying the result size 15655 11:39:59,040 --> 11:40:00,776 and others I'm going to start with the 15656 11:40:00,776 --> 11:40:02,936 first function called slice 15657 11:40:02,936 --> 11:40:04,740 which Returns the list of collection 15658 11:40:04,740 --> 11:40:07,616 elements with given integers the indices 15659 11:40:07,616 --> 11:40:10,616 may be based either as a range or as a 15660 11:40:10,616 --> 11:40:12,300 collection of integer values so I'm 15661 11:40:12,300 --> 11:40:14,936 gonna create first domain function 15662 11:40:14,936 --> 11:40:18,000 I'm going to type Val numbers I'm going 15663 11:40:18,000 --> 11:40:20,900 to create a list of 15664 11:40:21,240 --> 11:40:24,680 and I'm going to type here one 15665 11:40:25,200 --> 11:40:28,039 to 15666 11:40:28,616 --> 11:40:31,200 three 15667 11:40:31,200 --> 11:40:33,860 four 15668 11:40:34,080 --> 11:40:36,080 five 15669 11:40:37,200 --> 11:40:39,300 and six 15670 11:40:39,300 --> 11:40:41,700 now to use the slice function we simply 15671 11:40:41,700 --> 11:40:44,160 add here a print line 15672 11:40:44,160 --> 11:40:46,220 and we'll type your numbers 15673 11:40:46,220 --> 11:40:49,680 dot slice 15674 11:40:49,680 --> 11:40:52,980 and we create here arrange so we type 1 15675 11:40:52,980 --> 11:40:54,776 dot dot 15676 11:40:54,776 --> 11:40:56,416 3 15677 11:40:56,416 --> 11:40:59,640 and next we're going to add another 15678 11:40:59,640 --> 11:41:03,480 print line and I'm going to type numbers 15679 11:41:03,480 --> 11:41:06,740 dot slice 15680 11:41:07,616 --> 11:41:10,320 and I'm going to put here now zero dot 15681 11:41:10,320 --> 11:41:12,060 dot 15682 11:41:12,060 --> 11:41:13,980 four 15683 11:41:13,980 --> 11:41:16,580 and 15684 11:41:16,680 --> 11:41:20,160 I'm gonna type step 15685 11:41:20,160 --> 11:41:23,160 two 15686 11:41:31,140 --> 11:41:33,320 foreign 15687 11:41:38,240 --> 11:41:41,756 list so slice here 15688 11:41:41,756 --> 11:41:44,720 slice 15689 11:41:45,000 --> 11:41:47,220 and I'm gonna add another print line 15690 11:41:47,220 --> 11:41:49,580 here 15691 11:41:49,860 --> 11:41:52,520 numbers 15692 11:41:52,680 --> 11:41:56,040 dot slice 15693 11:41:56,040 --> 11:41:59,276 and I'm gonna add now a set here so I'm 15694 11:41:59,276 --> 11:42:02,299 going to type set off 15695 11:42:02,820 --> 11:42:04,800 and I'm gonna type 15696 11:42:04,800 --> 11:42:07,320 three 15697 11:42:07,320 --> 11:42:09,916 five 15698 11:42:10,140 --> 11:42:13,500 and zero so let's press Ctrl R till the 15699 11:42:13,500 --> 11:42:14,756 format the code 15700 11:42:14,756 --> 11:42:17,040 now if you run this code look at the 15701 11:42:17,040 --> 11:42:19,400 output 15702 11:42:36,416 --> 11:42:39,660 so you first get the first three 15703 11:42:39,660 --> 11:42:41,756 elements we get to 15704 11:42:41,756 --> 11:42:45,720 two three and four because uh we're 15705 11:42:45,720 --> 11:42:47,460 starting from the index one so we get 15706 11:42:47,460 --> 11:42:49,080 two three and four 15707 11:42:49,080 --> 11:42:53,276 then we get from 0 to 4 so you get one 15708 11:42:53,276 --> 11:42:55,380 but you don't get the two because you 15709 11:42:55,380 --> 11:42:58,080 have this step two which steps the 15710 11:42:58,080 --> 11:43:01,200 second element and then we have three 15711 11:43:01,200 --> 11:43:03,660 and five because it's stepping for the 15712 11:43:03,660 --> 11:43:05,640 four so this is what step two is doing 15713 11:43:05,640 --> 11:43:06,660 here 15714 11:43:06,660 --> 11:43:10,200 and next we have numbers slice and get 15715 11:43:10,200 --> 11:43:12,000 four 15716 11:43:12,000 --> 11:43:16,200 six and uh one but we get them as the 15717 11:43:16,200 --> 11:43:18,240 string representation we don't get them 15718 11:43:18,240 --> 11:43:20,460 as 15719 11:43:20,460 --> 11:43:24,900 as a integers so 15720 11:43:24,900 --> 11:43:27,240 all right 15721 11:43:27,240 --> 11:43:28,560 now 15722 11:43:28,560 --> 11:43:30,776 let's look at the next functions and 15723 11:43:30,776 --> 11:43:33,060 those are the take and drop 15724 11:43:33,060 --> 11:43:35,640 and to get the specified numbers of 15725 11:43:35,640 --> 11:43:38,340 elements starting from the first you can 15726 11:43:38,340 --> 11:43:41,400 use the take function for getting the 15727 11:43:41,400 --> 11:43:44,220 last elements use the take last when 15728 11:43:44,220 --> 11:43:46,140 call when when called with a number 15729 11:43:46,140 --> 11:43:48,300 larger than the collection size both 15730 11:43:48,300 --> 11:43:50,756 functions return the whole collection to 15731 11:43:50,756 --> 11:43:52,740 take all the elements except a given 15732 11:43:52,740 --> 11:43:55,680 number of first or last elements called 15733 11:43:55,680 --> 11:43:58,320 the drop or the and the drop loss 15734 11:43:58,320 --> 11:43:59,936 function respectively 15735 11:43:59,936 --> 11:44:03,256 so I'm gonna 15736 11:44:04,256 --> 11:44:07,436 add some print lines here I'm gonna add 15737 11:44:07,436 --> 11:44:08,936 print line 15738 11:44:08,936 --> 11:44:12,680 and I'm going to type numbers 15739 11:44:12,680 --> 11:44:15,980 dot take 15740 11:44:18,960 --> 11:44:20,460 three 15741 11:44:20,460 --> 11:44:23,840 Another Printer line 15742 11:44:24,060 --> 11:44:26,659 numbers 15743 11:44:27,300 --> 11:44:30,720 dot take last 15744 11:44:30,720 --> 11:44:33,436 hmm 15745 11:44:34,020 --> 11:44:39,020 also three then another print line 15746 11:44:39,180 --> 11:44:40,800 numbers 15747 11:44:40,800 --> 11:44:42,900 Dot 15748 11:44:42,900 --> 11:44:45,540 drop 15749 11:44:45,540 --> 11:44:47,340 one 15750 11:44:47,340 --> 11:44:50,719 and another print line 15751 11:44:54,540 --> 11:44:56,220 numbers 15752 11:44:56,220 --> 11:45:00,720 dot drop last 15753 11:45:01,560 --> 11:45:05,840 five now if you run this code 15754 11:45:06,740 --> 11:45:10,820 look what you get in the output 15755 11:45:12,300 --> 11:45:15,000 but let's add a space here so on another 15756 11:45:15,000 --> 11:45:17,096 print line 15757 11:45:17,096 --> 11:45:20,096 quotation marks photosyn now let's run 15758 11:45:20,096 --> 11:45:22,756 this code again 15759 11:45:27,660 --> 11:45:29,640 so we get uh 15760 11:45:29,640 --> 11:45:32,580 for you get numbers take three and this 15761 11:45:32,580 --> 11:45:36,116 function basically takes only three 15762 11:45:36,116 --> 11:45:36,960 um 15763 11:45:36,960 --> 11:45:39,300 three elements from our list 15764 11:45:39,300 --> 11:45:42,840 then numbers take last three is gonna 15765 11:45:42,840 --> 11:45:44,936 take the last three elements from hourly 15766 11:45:44,936 --> 11:45:47,040 so I have four five and six 15767 11:45:47,040 --> 11:45:50,040 then we have a print print line numbers 15768 11:45:50,040 --> 11:45:53,756 drop one so you have two three four five 15769 11:45:53,756 --> 11:45:56,040 and six and the first element is dropped 15770 11:45:56,040 --> 11:45:58,380 so this this is what this function is 15771 11:45:58,380 --> 11:45:59,460 doing 15772 11:45:59,460 --> 11:46:02,640 next we have numbers dot drop plus five 15773 11:46:02,640 --> 11:46:05,040 and this function is basically dropping 15774 11:46:05,040 --> 11:46:08,880 all the uh file the last five elements 15775 11:46:08,880 --> 11:46:11,220 and you have only the one so this is 15776 11:46:11,220 --> 11:46:14,096 what those functions are doing 15777 11:46:14,096 --> 11:46:16,616 you can also use predicates to define 15778 11:46:16,616 --> 11:46:18,596 the number of elements for taking or 15779 11:46:18,596 --> 11:46:21,000 dropping and there are four functions 15780 11:46:21,000 --> 11:46:23,820 similar to the ones described above 15781 11:46:23,820 --> 11:46:27,180 and those are take while take last while 15782 11:46:27,180 --> 11:46:30,660 drop while and drop drop last while and 15783 11:46:30,660 --> 11:46:33,240 let's look at all of them but I'm gonna 15784 11:46:33,240 --> 11:46:35,880 put a space between uh 15785 11:46:35,880 --> 11:46:37,436 our code that we have here so I'm gonna 15786 11:46:37,436 --> 11:46:40,520 have the print line here 15787 11:46:41,640 --> 11:46:45,616 quotation marks photo session 15788 11:46:46,400 --> 11:46:49,756 print line 15789 11:46:51,416 --> 11:46:53,820 so I'm going to type here numbers 15790 11:46:53,820 --> 11:46:55,800 dot take 15791 11:46:55,800 --> 11:46:58,800 while 15792 11:47:00,540 --> 11:47:03,116 and here we Define the predicate I'm 15793 11:47:03,116 --> 11:47:04,320 going to type 15794 11:47:04,320 --> 11:47:05,880 not 15795 11:47:05,880 --> 11:47:08,700 eat dot starts 15796 11:47:08,700 --> 11:47:11,180 with 15797 11:47:11,756 --> 11:47:13,380 f 15798 11:47:13,380 --> 11:47:16,256 in quotation marks 15799 11:47:16,256 --> 11:47:20,000 now another print line 15800 11:47:21,000 --> 11:47:23,220 numbers 15801 11:47:23,220 --> 11:47:26,360 dot take 15802 11:47:26,520 --> 11:47:29,359 last 15803 11:47:30,776 --> 11:47:33,380 while 15804 11:47:33,776 --> 11:47:37,860 and we Define here the predicate it 15805 11:47:37,860 --> 11:47:42,256 not equal to 3. not equal 15806 11:47:42,416 --> 11:47:45,360 two three 15807 11:47:45,360 --> 11:47:48,000 another print line 15808 11:47:48,000 --> 11:47:51,080 we type numbers 15809 11:47:54,960 --> 11:47:58,460 dot drop while 15810 11:48:01,256 --> 11:48:03,416 and we Define the expression the 15811 11:48:03,416 --> 11:48:05,756 predicate which is going to be applied 15812 11:48:05,756 --> 11:48:08,520 for our drop last while 15813 11:48:08,520 --> 11:48:10,680 so it should be 15814 11:48:10,680 --> 11:48:14,000 drop while here 15815 11:48:16,560 --> 11:48:19,460 and type it 15816 11:48:19,460 --> 11:48:23,096 dot length 15817 11:48:23,096 --> 11:48:25,800 equals equals to 3. 15818 11:48:25,800 --> 11:48:29,540 and what another print line 15819 11:48:29,880 --> 11:48:32,596 numbers 15820 11:48:33,180 --> 11:48:35,220 Dot 15821 11:48:35,220 --> 11:48:38,540 drop last while 15822 11:48:38,756 --> 11:48:42,180 and we type here it contains 15823 11:48:42,180 --> 11:48:44,700 it dot 15824 11:48:44,700 --> 11:48:47,700 contains 15825 11:48:50,460 --> 11:48:53,580 quotation marks I 15826 11:48:53,580 --> 11:48:55,080 no 15827 11:48:55,080 --> 11:48:58,276 if you run this code 15828 11:49:04,436 --> 11:49:07,680 we first get one 15829 11:49:07,680 --> 11:49:10,680 two and three so because here we check 15830 11:49:10,680 --> 11:49:13,256 in we're uh because using the logical 15831 11:49:13,256 --> 11:49:15,720 not operated we want all the elements 15832 11:49:15,720 --> 11:49:18,540 which don't start with f so we don't 15833 11:49:18,540 --> 11:49:20,820 have four and a five we have only one 15834 11:49:20,820 --> 11:49:22,200 two and three 15835 11:49:22,200 --> 11:49:23,756 next we have 15836 11:49:23,756 --> 11:49:26,640 numbers that take last while and we 15837 11:49:26,640 --> 11:49:29,040 Define the expression for which is going 15838 11:49:29,040 --> 11:49:30,680 to be 15839 11:49:30,680 --> 11:49:33,660 returned the elements by it not equal to 15840 11:49:33,660 --> 11:49:36,660 three so you only have four 15841 11:49:36,660 --> 11:49:42,180 five and six and uh the the the three 15842 11:49:42,180 --> 11:49:44,160 the three is is missing and we don't 15843 11:49:44,160 --> 11:49:46,500 have one because take last value only 15844 11:49:46,500 --> 11:49:49,980 takes the uh last elements so this is 15845 11:49:49,980 --> 11:49:52,580 why we have only 15846 11:49:52,880 --> 11:49:56,096 and six when you don't have three so it 15847 11:49:56,096 --> 11:49:58,740 doesn't care about the first element or 15848 11:49:58,740 --> 11:50:02,580 uh about uh the second element 15849 11:50:02,580 --> 11:50:06,360 next we have numbers drop while eat that 15850 11:50:06,360 --> 11:50:10,860 length equals to 3 and here we have 15851 11:50:10,860 --> 11:50:12,840 we have 15852 11:50:12,840 --> 11:50:14,820 so it at the lines equals to three and 15853 11:50:14,820 --> 11:50:17,180 we have 15854 11:50:17,220 --> 11:50:21,300 three four five and six so 15855 11:50:21,300 --> 11:50:24,720 this condition is satisfied so it 15856 11:50:24,720 --> 11:50:27,776 basically it drops all the elements 15857 11:50:27,776 --> 11:50:30,416 which are equal to three so we have 15858 11:50:30,416 --> 11:50:32,880 three four five and six and all of those 15859 11:50:32,880 --> 11:50:36,360 are bigger than three and the ones which 15860 11:50:36,360 --> 11:50:39,680 are equal to three respectively one 15861 11:50:39,680 --> 11:50:47,400 uh uh two and uh let's see and six 15862 11:50:47,400 --> 11:50:51,320 actually six is appearing here 15863 11:50:51,416 --> 11:50:54,020 so 15864 11:50:54,596 --> 11:50:56,520 one 15865 11:50:56,520 --> 11:50:59,000 two 15866 11:51:02,096 --> 11:51:04,500 I don't know why six supreme but this 15867 11:51:04,500 --> 11:51:06,776 basically saying that it should drop 15868 11:51:06,776 --> 11:51:10,020 while if that length equals uh to three 15869 11:51:10,020 --> 11:51:12,480 so it basically drops all the elements 15870 11:51:12,480 --> 11:51:15,900 which are equal to with the length three 15871 11:51:15,900 --> 11:51:20,756 so we have this is what we have here um 15872 11:51:20,756 --> 11:51:22,436 three four 15873 11:51:22,436 --> 11:51:24,960 five and six six should not be but I 15874 11:51:24,960 --> 11:51:26,640 don't know why it's here now 15875 11:51:26,640 --> 11:51:29,060 the next that we have here is numbers 15876 11:51:29,060 --> 11:51:32,276 dropped last while so again this is uh 15877 11:51:32,276 --> 11:51:34,380 Focus specifically about the last 15878 11:51:34,380 --> 11:51:38,640 element so it contains I so get uh 15879 11:51:38,640 --> 11:51:41,276 uh one two three and four so because 15880 11:51:41,276 --> 11:51:43,500 this drop last while is basically 15881 11:51:43,500 --> 11:51:46,616 basically dropping all the elements all 15882 11:51:46,616 --> 11:51:49,880 the last Elements which contain uh which 15883 11:51:49,880 --> 11:51:53,060 contains I so we have 15884 11:51:53,060 --> 11:51:56,659 six five 15885 11:51:56,756 --> 11:52:00,320 so we have a 15886 11:52:01,020 --> 11:52:03,480 drop last while so we have it that 15887 11:52:03,480 --> 11:52:06,320 contains I 15888 11:52:08,936 --> 11:52:11,040 so it drops all the elements which 15889 11:52:11,040 --> 11:52:13,140 contains I so sorry for what I said 15890 11:52:13,140 --> 11:52:15,180 previously so this way I have here one 15891 11:52:15,180 --> 11:52:18,060 two three and four and uh the five and 15892 11:52:18,060 --> 11:52:20,340 the six are missing because it basically 15893 11:52:20,340 --> 11:52:22,616 drops the old Elements which contains 15894 11:52:22,616 --> 11:52:25,320 the letter i so this is how you can use 15895 11:52:25,320 --> 11:52:27,840 the take and drop the next function that 15896 11:52:27,840 --> 11:52:31,200 you can use is called chunked 15897 11:52:31,200 --> 11:52:33,720 and 15898 11:52:33,720 --> 11:52:35,160 if you want to break a collection 15899 11:52:35,160 --> 11:52:37,916 through parts of a given size then you 15900 11:52:37,916 --> 11:52:40,436 can use the chunked function chunks take 15901 11:52:40,436 --> 11:52:42,720 a single argument the size of the chunk 15902 11:52:42,720 --> 11:52:45,000 and Returns the list of 15903 11:52:45,000 --> 11:52:46,436 [Music] 15904 11:52:46,436 --> 11:52:49,380 list of the given size the first chunk 15905 11:52:49,380 --> 11:52:51,660 starts from the first element and 15906 11:52:51,660 --> 11:52:53,640 contains the size of the elements and 15907 11:52:53,640 --> 11:52:55,916 the second chunk holds the next size 15908 11:52:55,916 --> 11:52:58,080 elements and so on the last chunk maybe 15909 11:52:58,080 --> 11:53:01,680 may have a similar size so if I have 15910 11:53:01,680 --> 11:53:05,840 here let's change this to numbers 15911 11:53:06,000 --> 11:53:11,300 strings and let's type here 15912 11:53:11,936 --> 11:53:13,200 Val 15913 11:53:13,200 --> 11:53:16,039 numbers 15914 11:53:16,680 --> 11:53:18,596 while I was changing that I should use 15915 11:53:18,596 --> 11:53:21,560 refactor so 15916 11:53:23,400 --> 11:53:27,140 let's use a refactor 15917 11:53:27,360 --> 11:53:29,460 rename 15918 11:53:29,460 --> 11:53:32,700 numbers strings so it will be changed in 15919 11:53:32,700 --> 11:53:35,700 all the places we're using that 15920 11:53:35,700 --> 11:53:39,416 and here have numbers and I'm gonna put 15921 11:53:39,416 --> 11:53:42,840 equals and I'll put parentheses 15922 11:53:42,840 --> 11:53:47,276 zero dot dot 13. and I'm gonna put that 15923 11:53:47,276 --> 11:53:50,820 to list to convert this to a list 15924 11:53:50,820 --> 11:53:53,820 and we add a print line to see how this 15925 11:53:53,820 --> 11:53:56,040 chunk function is working so we got the 15926 11:53:56,040 --> 11:53:58,980 print right let's close the console 15927 11:53:58,980 --> 11:54:02,960 and we type here numbers Dot 15928 11:54:03,720 --> 11:54:07,276 Dot chunked 15929 11:54:09,660 --> 11:54:12,116 and would put three so it's gonna chunk 15930 11:54:12,116 --> 11:54:16,520 our elements into three into 15931 11:54:17,700 --> 11:54:19,700 um 15932 11:54:19,800 --> 11:54:23,340 basically elements of three so we have 15933 11:54:23,340 --> 11:54:27,300 zero one two three four five six seven 15934 11:54:27,300 --> 11:54:31,860 eight and they're both on on chunks of 15935 11:54:31,860 --> 11:54:33,360 three elements so this is what this 15936 11:54:33,360 --> 11:54:34,860 chunks is doing 15937 11:54:34,860 --> 11:54:37,680 so this is why I have here this result 15938 11:54:37,680 --> 11:54:40,520 and 15939 11:54:41,400 --> 11:54:43,680 the next function that you're going to 15940 11:54:43,680 --> 11:54:47,276 look at is called windowed and 15941 11:54:47,276 --> 11:54:50,160 and we we can also use a transformation 15942 11:54:50,160 --> 11:54:53,040 on our chunked element so I cannot hear 15943 11:54:53,040 --> 11:54:54,776 a print line 15944 11:54:54,776 --> 11:54:57,860 we type numbers 15945 11:54:59,040 --> 11:55:02,540 Dot chunked 15946 11:55:08,096 --> 11:55:10,916 chunked which we also choose the size 15947 11:55:10,916 --> 11:55:12,416 three 15948 11:55:12,416 --> 11:55:16,020 then you put curly braces and here I'm 15949 11:55:16,020 --> 11:55:19,140 gonna Define how our chunked elements 15950 11:55:19,140 --> 11:55:22,380 are going to be transformed so we put it 15951 11:55:22,380 --> 11:55:24,840 dot 15952 11:55:24,840 --> 11:55:27,480 sum so it's going to sum the chunked 15953 11:55:27,480 --> 11:55:30,180 elements so if I press Ctrl I'll tell 15954 11:55:30,180 --> 11:55:31,860 the format the code and now if I run 15955 11:55:31,860 --> 11:55:33,240 this look what we're going to see in the 15956 11:55:33,240 --> 11:55:35,540 output 15957 11:55:39,740 --> 11:55:44,220 so we get uh you get first the chunked 15958 11:55:44,220 --> 11:55:45,776 elements here 15959 11:55:45,776 --> 11:55:50,520 in entry in three elements and then we 15960 11:55:50,520 --> 11:55:53,276 have our chunked elements but they are 15961 11:55:53,276 --> 11:55:55,520 summed up so we have first three because 15962 11:55:55,520 --> 11:55:58,200 zero plus one plus two is three then we 15963 11:55:58,200 --> 11:56:00,660 have three four five because that is 15964 11:56:00,660 --> 11:56:03,180 twelve don't have seven eight uh seven 15965 11:56:03,180 --> 11:56:04,380 uh 15966 11:56:04,380 --> 11:56:06,416 six seven eight and they have twenty one 15967 11:56:06,416 --> 11:56:08,040 then we have 15968 11:56:08,040 --> 11:56:12,360 uh 10 11 and 9 and we have 30 and so on 15969 11:56:12,360 --> 11:56:17,220 so this is uh how uh you can use the 15970 11:56:17,220 --> 11:56:19,680 chunked function to chunk the elements 15971 11:56:19,680 --> 11:56:21,840 together by a given size and you can 15972 11:56:21,840 --> 11:56:23,936 also use the transformation function to 15973 11:56:23,936 --> 11:56:26,040 do a transformation on them like adding 15974 11:56:26,040 --> 11:56:29,276 them so as you can see we have adding 15975 11:56:29,276 --> 11:56:30,596 them here 15976 11:56:30,596 --> 11:56:32,640 next we're going to look at the next 15977 11:56:32,640 --> 11:56:34,436 function function that you can use and 15978 11:56:34,436 --> 11:56:37,080 that is called the windowed function and 15979 11:56:37,080 --> 11:56:39,720 we can retrieve all possible ranges of 15980 11:56:39,720 --> 11:56:41,460 the collection elements of a given size 15981 11:56:41,460 --> 11:56:43,860 with the window function the function of 15982 11:56:43,860 --> 11:56:46,740 getting them is called windowed it 15983 11:56:46,740 --> 11:56:49,080 returns a list of element ranges that 15984 11:56:49,080 --> 11:56:50,880 you would see if you are looking at the 15985 11:56:50,880 --> 11:56:53,340 collation through a slightly wind off of 15986 11:56:53,340 --> 11:56:56,276 the given size unlike chunked window 15987 11:56:56,276 --> 11:56:59,640 returns element ranges Windows starting 15988 11:56:59,640 --> 11:57:01,980 from each collection element all the all 15989 11:57:01,980 --> 11:57:04,980 the windows are returned as an element 15990 11:57:04,980 --> 11:57:10,460 of a single list so if I have here 15991 11:57:11,096 --> 11:57:13,616 the 15992 11:57:13,616 --> 11:57:16,880 let's create another 15993 11:57:16,980 --> 11:57:20,340 variable here let's call it 15994 11:57:20,340 --> 11:57:24,179 Val numbers 15995 11:57:25,800 --> 11:57:27,300 string 15996 11:57:27,300 --> 11:57:28,860 two 15997 11:57:28,860 --> 11:57:33,200 and we're gonna type here let's say 15998 11:57:33,960 --> 11:57:36,720 or I can assign 15999 11:57:36,720 --> 11:57:39,240 I can assign the number of strings here 16000 11:57:39,240 --> 11:57:42,480 so I'm going to type here number strings 16001 11:57:42,480 --> 11:57:45,000 now what I can do here I I can add a 16002 11:57:45,000 --> 11:57:46,256 print line 16003 11:57:46,256 --> 11:57:52,160 and like I can type numbers string store 16004 11:57:52,380 --> 11:57:54,240 Dot 16005 11:57:54,240 --> 11:57:57,480 windowed and we choose let's say three 16006 11:57:57,480 --> 11:57:59,276 and let's see what we have on the output 16007 11:57:59,276 --> 11:58:02,479 so now if you run this code 16008 11:58:02,640 --> 11:58:05,096 I should have added here a space so 16009 11:58:05,096 --> 11:58:08,096 let's add the print line here 16010 11:58:08,096 --> 11:58:11,000 backslash 16011 11:58:11,936 --> 11:58:14,840 photo selection 16012 11:58:16,860 --> 11:58:19,040 foreign 16013 11:58:24,240 --> 11:58:26,460 so we have one two three then we have 16014 11:58:26,460 --> 11:58:29,220 two three four you have three four five 16015 11:58:29,220 --> 11:58:32,640 and four five six so this is similar to 16016 11:58:32,640 --> 11:58:34,800 junk but window provides more 16017 11:58:34,800 --> 11:58:38,776 flexibility because uh you can 16018 11:58:38,776 --> 11:58:41,400 specify a step which defines a distance 16019 11:58:41,400 --> 11:58:44,040 between the first element and and to a 16020 11:58:44,040 --> 11:58:47,040 DH and windows by default the value is 16021 11:58:47,040 --> 11:58:48,720 one so the result contains Windows 16022 11:58:48,720 --> 11:58:50,276 starting from all elements if you 16023 11:58:50,276 --> 11:58:52,380 increase the step to two you'll receive 16024 11:58:52,380 --> 11:58:55,560 only Windows 30 from the other element 16025 11:58:55,560 --> 11:58:59,340 first third and so on and finally you 16026 11:58:59,340 --> 11:59:00,776 can apply transformation to the return 16027 11:59:00,776 --> 11:59:03,300 ranges right away to do this you provide 16028 11:59:03,300 --> 11:59:05,096 the transformation as a Lambda function 16029 11:59:05,096 --> 11:59:08,160 when calling windows so it is similar to 16030 11:59:08,160 --> 11:59:09,300 chunked 16031 11:59:09,300 --> 11:59:12,180 and um 16032 11:59:12,180 --> 11:59:15,240 so with this I think we end our 16033 11:59:15,240 --> 11:59:16,916 discussion about the thieving collection 16034 11:59:16,916 --> 11:59:20,160 parts and see you in the next video and 16035 11:59:20,160 --> 11:59:22,860 if you find this confusing don't worry 16036 11:59:22,860 --> 11:59:26,116 because it's so confusing for uh for me 16037 11:59:26,116 --> 11:59:29,240 for me and for probably other people 16038 11:59:29,240 --> 11:59:33,540 because it takes time to to get used to 16039 11:59:33,540 --> 11:59:36,300 this and my suggestion for you just play 16040 11:59:36,300 --> 11:59:39,596 with the code change the the parameters 16041 11:59:39,596 --> 11:59:42,720 play with the functions try different uh 16042 11:59:42,720 --> 11:59:45,776 values and so on so see you in the next 16043 11:59:45,776 --> 11:59:47,160 video 16044 11:59:47,160 --> 11:59:48,480 now we're going to look at some 16045 11:59:48,480 --> 11:59:50,276 functions with which you can retrieve 16046 11:59:50,276 --> 11:59:52,916 single elements from our Collections and 16047 11:59:52,916 --> 11:59:55,916 the first one is one which retrieves an 16048 11:59:55,916 --> 11:59:58,800 element by its position so I'm gonna 16049 11:59:58,800 --> 12:00:02,640 create the Val numbers again 16050 12:00:02,640 --> 12:00:06,240 numbers equals to 16051 12:00:06,240 --> 12:00:09,320 list of 16052 12:00:10,916 --> 12:00:12,540 and 16053 12:00:12,540 --> 12:00:15,116 we type here 16054 12:00:15,116 --> 12:00:17,700 one 16055 12:00:17,700 --> 12:00:20,000 two 16056 12:00:20,276 --> 12:00:22,276 three 16057 12:00:23,460 --> 12:00:27,860 and four and also five 16058 12:00:30,840 --> 12:00:32,520 and let's say that I want to get the 16059 12:00:32,520 --> 12:00:34,436 element at position three to do that 16060 12:00:34,436 --> 12:00:37,680 just add here a print line and we type 16061 12:00:37,680 --> 12:00:40,040 numbers 16062 12:00:43,220 --> 12:00:46,880 dot element 16063 12:00:46,880 --> 12:00:49,436 and here you specify the position three 16064 12:00:49,436 --> 12:00:52,560 so zero one two three so we're going to 16065 12:00:52,560 --> 12:00:56,596 get four outputted in the console 16066 12:00:57,240 --> 12:00:59,340 so we get in the output as expected for 16067 12:00:59,340 --> 12:01:01,680 now there are there are also two 16068 12:01:01,680 --> 12:01:03,480 functions for retrieving the first and 16069 12:01:03,480 --> 12:01:05,936 last element and to do that just add 16070 12:01:05,936 --> 12:01:08,340 another print line here 16071 12:01:08,340 --> 12:01:11,640 and type numbers that first 16072 12:01:11,640 --> 12:01:13,256 so this is gonna retrieve the first 16073 12:01:13,256 --> 12:01:15,960 element and Ctrl d 16074 12:01:15,960 --> 12:01:18,480 numbers Dot 16075 12:01:18,480 --> 12:01:20,340 last so 16076 12:01:20,340 --> 12:01:22,380 this is gonna receive the first element 16077 12:01:22,380 --> 12:01:25,020 one and last is gonna retrieve the last 16078 12:01:25,020 --> 12:01:28,399 element five so if you run this 16079 12:01:29,840 --> 12:01:33,116 we get first four from our 16080 12:01:33,116 --> 12:01:35,580 disappearance line then we get first one 16081 12:01:35,580 --> 12:01:37,436 and again the last five 16082 12:01:37,436 --> 12:01:39,680 and we can also edit three 16083 12:01:39,680 --> 12:01:43,256 elements by condition so I can type here 16084 12:01:43,256 --> 12:01:46,560 uh I can type down here or 16085 12:01:46,560 --> 12:01:49,256 or I will type it here 16086 12:01:49,256 --> 12:01:50,820 actually I'm going to type it down here 16087 12:01:50,820 --> 12:01:53,460 it's going to add here a print line 16088 12:01:53,460 --> 12:01:56,416 numbers let's say 16089 12:01:56,416 --> 12:02:00,000 DOT first now I'm going to retrieve by a 16090 12:02:00,000 --> 12:02:01,680 condition so once I first I'm going to 16091 12:02:01,680 --> 12:02:03,840 choose this one with curly braces and 16092 12:02:03,840 --> 12:02:05,756 the condition is that I want to leave 16093 12:02:05,756 --> 12:02:08,040 the first element for which the length 16094 12:02:08,040 --> 12:02:09,480 is greater than 3 so I'm going to type 16095 12:02:09,480 --> 12:02:12,180 here it dot length 16096 12:02:12,180 --> 12:02:15,180 greater than 3. and I'm gonna add I'm 16097 12:02:15,180 --> 12:02:17,936 gonna press Ctrl D because I'm gonna do 16098 12:02:17,936 --> 12:02:20,776 the same thing for the 16099 12:02:20,776 --> 12:02:23,040 by condition so I'm going to type here 16100 12:02:23,040 --> 12:02:25,500 numbers that last but I'm gonna change 16101 12:02:25,500 --> 12:02:28,256 the condition I'm gonna change it to 16102 12:02:28,256 --> 12:02:33,540 eat that starts with the it that starts 16103 12:02:33,540 --> 12:02:35,520 with 16104 12:02:35,520 --> 12:02:37,560 the letter f 16105 12:02:37,560 --> 12:02:39,416 so now we're retrieving the first 16106 12:02:39,416 --> 12:02:42,300 element which is greater than three and 16107 12:02:42,300 --> 12:02:44,640 next retrieving the last element which 16108 12:02:44,640 --> 12:02:46,860 starts with the letter f 16109 12:02:46,860 --> 12:02:50,300 so now if you run this 16110 12:02:51,180 --> 12:02:53,820 we get 3 and 5 because 3 is the first 16111 12:02:53,820 --> 12:02:56,276 element which is has a length greater 16112 12:02:56,276 --> 12:02:58,500 than three and can get five because this 16113 12:02:58,500 --> 12:03:01,320 is the one the last element which starts 16114 12:03:01,320 --> 12:03:05,220 with the letter F and if you can other 16115 12:03:05,220 --> 12:03:08,096 space here if you want to make things 16116 12:03:08,096 --> 12:03:09,720 more clear so we're gonna add the print 16117 12:03:09,720 --> 12:03:12,080 line quotation marks 16118 12:03:12,080 --> 12:03:13,800 backslashing 16119 12:03:13,800 --> 12:03:15,360 actually 16120 12:03:15,360 --> 12:03:17,580 forward selection 16121 12:03:17,580 --> 12:03:20,660 so if you run this 16122 12:03:26,040 --> 12:03:28,500 we get three and five output today in 16123 12:03:28,500 --> 12:03:30,416 the console 16124 12:03:30,416 --> 12:03:32,220 and we can also retrieve a random 16125 12:03:32,220 --> 12:03:34,500 element from our numbers so I can type 16126 12:03:34,500 --> 12:03:36,416 here print line 16127 12:03:36,416 --> 12:03:37,640 numbers 16128 12:03:37,640 --> 12:03:40,860 dot random and this is going to return a 16129 12:03:40,860 --> 12:03:42,480 random number from our list of numbers 16130 12:03:42,480 --> 12:03:45,500 so if I run this 16131 12:03:49,160 --> 12:03:53,960 we get one and if I run this again 16132 12:03:55,200 --> 12:03:57,360 get four so every time you run this 16133 12:03:57,360 --> 12:03:59,520 you'll get a random number 16134 12:03:59,520 --> 12:04:01,380 and you can also check if the list is 16135 12:04:01,380 --> 12:04:04,680 empty so you can type here print line 16136 12:04:04,680 --> 12:04:07,220 numbers 16137 12:04:08,220 --> 12:04:10,500 that is empty then that is going to 16138 12:04:10,500 --> 12:04:14,040 return true if the list is empty and if 16139 12:04:14,040 --> 12:04:15,480 the list is not empty it's going to 16140 12:04:15,480 --> 12:04:19,220 return false so if you run this 16141 12:04:22,256 --> 12:04:25,200 we get false so this is our discussion 16142 12:04:25,200 --> 12:04:27,540 about how to retrieve single elements 16143 12:04:27,540 --> 12:04:29,700 see in the next video now it's time to 16144 12:04:29,700 --> 12:04:31,380 start a discussion about aggregate 16145 12:04:31,380 --> 12:04:33,720 operations so kotlin collections 16146 12:04:33,720 --> 12:04:35,640 contains functions for commonly used 16147 12:04:35,640 --> 12:04:37,680 aggregate operations 16148 12:04:37,680 --> 12:04:39,720 and those are operations that return a 16149 12:04:39,720 --> 12:04:41,400 single value based on the collection 16150 12:04:41,400 --> 12:04:44,040 content most of them are well known and 16151 12:04:44,040 --> 12:04:45,960 work in the same way as they do in other 16152 12:04:45,960 --> 12:04:47,936 programming languages 16153 12:04:47,936 --> 12:04:51,060 and we have minor null and Max ornal 16154 12:04:51,060 --> 12:04:52,680 which return the smallest and the 16155 12:04:52,680 --> 12:04:54,776 largest element respectively and on 16156 12:04:54,776 --> 12:04:57,060 empty collections the return null then 16157 12:04:57,060 --> 12:04:58,800 we have average which Returns the 16158 12:04:58,800 --> 12:05:00,000 average value of elements in the 16159 12:05:00,000 --> 12:05:01,740 collection then we have sum which 16160 12:05:01,740 --> 12:05:03,180 Returns the sum of elements in the 16161 12:05:03,180 --> 12:05:04,740 collection of numbers 16162 12:05:04,740 --> 12:05:06,776 and have count which Returns the number 16163 12:05:06,776 --> 12:05:09,840 of elements in our collections so please 16164 12:05:09,840 --> 12:05:13,340 declare a list numbers 16165 12:05:13,436 --> 12:05:16,220 list of 16166 12:05:19,500 --> 12:05:22,200 let's put so let's put some numbers here 16167 12:05:22,200 --> 12:05:25,460 like let's say six 16168 12:05:26,460 --> 12:05:28,616 then 16169 12:05:28,616 --> 12:05:30,416 14 16170 12:05:30,416 --> 12:05:33,255 4 16171 12:05:33,916 --> 12:05:35,880 let's say 16172 12:05:35,880 --> 12:05:37,680 100 16173 12:05:37,680 --> 12:05:40,800 now let's press Ctrl format the code and 16174 12:05:40,800 --> 12:05:42,900 let's add the print line here let's put 16175 12:05:42,900 --> 12:05:45,660 quotation marks and let's type the sum 16176 12:05:45,660 --> 12:05:47,936 is 16177 12:05:47,936 --> 12:05:50,776 dollar sign 16178 12:05:55,680 --> 12:05:59,520 and the type here numbers 16179 12:05:59,520 --> 12:06:02,539 dot sum 16180 12:06:02,880 --> 12:06:04,916 numbers 16181 12:06:04,916 --> 12:06:07,800 that's sum 16182 12:06:07,800 --> 12:06:10,436 all right and uh 16183 12:06:10,436 --> 12:06:13,616 I'll add another print line and now this 16184 12:06:13,616 --> 12:06:16,256 is gonna say 16185 12:06:16,256 --> 12:06:17,820 the 16186 12:06:17,820 --> 12:06:20,880 count is 16187 12:06:20,880 --> 12:06:23,700 again dollar sign 16188 12:06:23,700 --> 12:06:26,276 numbers 16189 12:06:26,276 --> 12:06:28,740 dot count 16190 12:06:28,740 --> 12:06:31,820 another print line 16191 12:06:34,700 --> 12:06:38,040 the average 16192 12:06:38,040 --> 12:06:40,680 is 16193 12:06:40,680 --> 12:06:43,560 again dollar sign 16194 12:06:43,560 --> 12:06:46,916 numbers that average 16195 12:06:46,916 --> 12:06:50,180 another print line 16196 12:06:51,116 --> 12:06:54,840 and now I'm going to say the 16197 12:06:54,840 --> 12:06:58,916 max value is 16198 12:06:58,916 --> 12:07:02,400 again dollar sign numbers 16199 12:07:02,400 --> 12:07:05,480 dot marks 16200 12:07:05,880 --> 12:07:09,360 numbers Dot 16201 12:07:10,680 --> 12:07:13,500 marks 16202 12:07:13,500 --> 12:07:16,020 or null 16203 12:07:16,020 --> 12:07:20,040 and I'll print line 16204 12:07:20,040 --> 12:07:24,960 the mean value is 16205 12:07:25,140 --> 12:07:30,480 dollar sign numbers that mean 16206 12:07:30,480 --> 12:07:33,860 or null 16207 12:07:33,960 --> 12:07:36,480 now if you run this code you're gonna 16208 12:07:36,480 --> 12:07:38,300 see in the output the corresponding 16209 12:07:38,300 --> 12:07:40,616 values for those expressions that we 16210 12:07:40,616 --> 12:07:43,560 have here so again the sum is 134 the 16211 12:07:43,560 --> 12:07:45,960 count is 5 because we have five elements 16212 12:07:45,960 --> 12:07:50,220 here the average is 26.28 the maximum 16213 12:07:50,220 --> 12:07:53,220 value is 100 and the minimum value is 4. 16214 12:07:53,220 --> 12:07:54,540 so this is how you can use those 16215 12:07:54,540 --> 12:07:58,140 aggregate operations and agree and those 16216 12:07:58,140 --> 12:08:00,436 aggregate um 16217 12:08:00,436 --> 12:08:04,200 functions and beside the regular sum 16218 12:08:04,200 --> 12:08:06,300 there is an advantage so much summation 16219 12:08:06,300 --> 12:08:09,116 function called sum of that takes a 16220 12:08:09,116 --> 12:08:11,160 selected function and Returns the sum 16221 12:08:11,160 --> 12:08:13,320 sum of its application to all collection 16222 12:08:13,320 --> 12:08:15,300 elements and let me show you how this 16223 12:08:15,300 --> 12:08:18,776 works so you just type here 16224 12:08:18,776 --> 12:08:22,520 so we type here print line 16225 12:08:23,220 --> 12:08:26,300 the sum is 16226 12:08:28,200 --> 12:08:30,960 dollar sign 16227 12:08:30,960 --> 12:08:32,700 numbers 16228 12:08:32,700 --> 12:08:35,640 dot sum of 16229 12:08:35,640 --> 12:08:37,200 we select this one 16230 12:08:37,200 --> 12:08:39,240 and here we type it 16231 12:08:39,240 --> 12:08:42,180 times two 16232 12:08:42,180 --> 12:08:43,980 now if you run this what it's going to 16233 12:08:43,980 --> 12:08:46,380 do is it's gonna sum all the numbers 16234 12:08:46,380 --> 12:08:48,060 like the first function that we have 16235 12:08:48,060 --> 12:08:51,416 here so it's going to sum 134 and it's 16236 12:08:51,416 --> 12:08:53,820 multi is going to multiply that that 16237 12:08:53,820 --> 12:08:57,680 number by two so if you run this 16238 12:09:00,240 --> 12:09:05,116 we get 260 which is 132 times 16239 12:09:05,116 --> 12:09:07,860 134 times 2. so this is how you can use 16240 12:09:07,860 --> 12:09:10,436 the sum off with this uh 16241 12:09:10,436 --> 12:09:12,560 transformation which is gonna basically 16242 12:09:12,560 --> 12:09:16,380 multiply the list with a number with the 16243 12:09:16,380 --> 12:09:18,480 with the sum of the list with the number 16244 12:09:18,480 --> 12:09:20,040 so this is our discussion about 16245 12:09:20,040 --> 12:09:22,320 aggregate operations and see you in the 16246 12:09:22,320 --> 12:09:24,300 next video now it's time to start a 16247 12:09:24,300 --> 12:09:26,160 discussion about ordering and in this 16248 12:09:26,160 --> 12:09:27,540 video we're going to look at comparable 16249 12:09:27,540 --> 12:09:30,416 and add comparator interfaces 16250 12:09:30,416 --> 12:09:33,480 so first I'm going to create a list of 16251 12:09:33,480 --> 12:09:35,776 numbers 16252 12:09:36,000 --> 12:09:37,560 and 16253 12:09:37,560 --> 12:09:39,596 I'm not gonna 16254 12:09:39,596 --> 12:09:41,820 I'm gonna get a multiple list so 16255 12:09:41,820 --> 12:09:44,640 Multiple List off and I'm gonna 16256 12:09:44,640 --> 12:09:46,740 Define some numbers here not in a 16257 12:09:46,740 --> 12:09:48,480 specific order 16258 12:09:48,480 --> 12:09:52,980 so we type two let's say five 16259 12:09:52,980 --> 12:09:54,360 one 16260 12:09:54,360 --> 12:09:56,096 let's say 16261 12:09:56,096 --> 12:09:58,436 40. 16262 12:09:58,436 --> 12:10:00,720 20. 16263 12:10:00,720 --> 12:10:04,220 100 let's say 16264 12:10:04,700 --> 12:10:08,480 60. now what if I want to 16265 12:10:08,480 --> 12:10:12,480 order the numbers that I have inside our 16266 12:10:12,480 --> 12:10:14,756 list numbers to do that you just need 16267 12:10:14,756 --> 12:10:17,520 you type numbers 16268 12:10:17,520 --> 12:10:19,140 Dot 16269 12:10:19,140 --> 12:10:21,960 and there is a function called sorted so 16270 12:10:21,960 --> 12:10:25,080 if I type number that's sorted now our 16271 12:10:25,080 --> 12:10:27,416 number on our numbers are going to be 16272 12:10:27,416 --> 12:10:30,900 sorted so if you type here for each 16273 12:10:30,900 --> 12:10:33,060 put in the line 16274 12:10:33,060 --> 12:10:35,659 it 16275 12:10:36,060 --> 12:10:38,820 so now if you run this 16276 12:10:38,820 --> 12:10:42,116 even though we didn't type our numbers 16277 12:10:42,116 --> 12:10:45,416 in ascending order there will be sorted 16278 12:10:45,416 --> 12:10:47,096 and as you can see they are sorted we 16279 12:10:47,096 --> 12:10:48,596 have one two 16280 12:10:48,596 --> 12:10:51,416 five twenty forty six three and one 16281 12:10:51,416 --> 12:10:54,776 hundred so they are sorted by this 16282 12:10:54,776 --> 12:10:56,580 function called sorted and why this is 16283 12:10:56,580 --> 12:10:57,840 working 16284 12:10:57,840 --> 12:10:59,400 it's working because if you click on 16285 12:10:59,400 --> 12:11:02,520 this sorted function 16286 12:11:02,520 --> 12:11:03,900 what a 16287 12:11:03,900 --> 12:11:07,436 actually if you type here let's say 16288 12:11:07,436 --> 12:11:11,240 let's say that I put the type here 16289 12:11:11,276 --> 12:11:12,960 and 16290 12:11:12,960 --> 12:11:16,340 if I click on the type 16291 12:11:17,520 --> 12:11:21,416 this tie this type integer this class is 16292 12:11:21,416 --> 12:11:22,860 implementing this thing called 16293 12:11:22,860 --> 12:11:25,256 comparable and this comparable has a 16294 12:11:25,256 --> 12:11:28,380 function called compare to so when we 16295 12:11:28,380 --> 12:11:30,560 actually type numbers that are sorted 16296 12:11:30,560 --> 12:11:33,596 what what internally was happening is 16297 12:11:33,596 --> 12:11:36,300 that the integer class was using the 16298 12:11:36,300 --> 12:11:39,916 compared to function which is which is a 16299 12:11:39,916 --> 12:11:43,080 function which you want to you need to 16300 12:11:43,080 --> 12:11:44,460 implement if you implement the 16301 12:11:44,460 --> 12:11:48,300 comparable interface to compare if 16302 12:11:48,300 --> 12:11:51,776 to compare our numbers and to based on 16303 12:11:51,776 --> 12:11:54,960 that to to sub them to to sort them 16304 12:11:54,960 --> 12:11:59,580 and now let's say that I have or let's 16305 12:11:59,580 --> 12:12:03,116 say that I have a data class 16306 12:12:03,116 --> 12:12:05,936 called laptop 16307 12:12:05,936 --> 12:12:07,860 and it's gonna have a 16308 12:12:07,860 --> 12:12:10,680 variable called brand it's going to be 16309 12:12:10,680 --> 12:12:13,320 of type string 16310 12:12:13,320 --> 12:12:16,080 another variable called here this could 16311 12:12:16,080 --> 12:12:19,640 be the the year one one this specific 16312 12:12:19,640 --> 12:12:24,140 version was released on int 16313 12:12:24,560 --> 12:12:26,936 also need 16314 12:12:26,936 --> 12:12:28,860 and the wall 16315 12:12:28,860 --> 12:12:31,436 let's say um 16316 12:12:31,436 --> 12:12:33,960 price also an integer 16317 12:12:33,960 --> 12:12:38,936 right now if I create here a list of 16318 12:12:38,936 --> 12:12:42,480 laptops laptops 16319 12:12:43,200 --> 12:12:46,740 equals to Multiple List of 16320 12:12:46,740 --> 12:12:50,160 and I type here laptop 16321 12:12:50,160 --> 12:12:54,060 let's say that I type here Dell 16322 12:12:54,060 --> 12:12:58,860 and I put the year let's say 2021 16323 12:12:58,860 --> 12:13:01,616 the ram let's say that is four gigabytes 16324 12:13:01,616 --> 12:13:04,220 RAM and the price will say that is 16325 12:13:04,220 --> 12:13:06,540 600 okay 16326 12:13:06,540 --> 12:13:08,520 comma I'm going to create another laptop 16327 12:13:08,520 --> 12:13:09,416 here 16328 12:13:09,416 --> 12:13:11,820 so another object I'm gonna type here 16329 12:13:11,820 --> 12:13:13,436 let's call it 16330 12:13:13,436 --> 12:13:15,660 Acer 16331 12:13:15,660 --> 12:13:19,080 is going to be let's say the year um 16332 12:13:19,080 --> 12:13:22,160 let's put 20 16333 12:13:22,200 --> 12:13:24,540 20. 16334 12:13:24,540 --> 12:13:27,360 the para is let's say the ram let's say 16335 12:13:27,360 --> 12:13:30,240 that is eight gigabytes Ram 16336 12:13:30,240 --> 12:13:34,256 and the prices say that it's 100 dollars 16337 12:13:34,256 --> 12:13:37,020 comma and let's create another laptop 16338 12:13:37,020 --> 12:13:39,300 object so we type laptop 16339 12:13:39,300 --> 12:13:42,596 and create an Apple laptop which is 16340 12:13:42,596 --> 12:13:44,096 which you're gonna for which you're 16341 12:13:44,096 --> 12:13:45,660 gonna choose the 16342 12:13:45,660 --> 12:13:47,756 the year let's say 16343 12:13:47,756 --> 12:13:49,756 um 16344 12:13:51,300 --> 12:13:54,020 Tony 16345 12:13:54,540 --> 12:13:56,276 2020 16346 12:13:56,276 --> 12:13:57,900 2022 16347 12:13:57,900 --> 12:14:00,060 and 16348 12:14:00,060 --> 12:14:02,360 for the ram let's say they're choose 16 16349 12:14:02,360 --> 12:14:06,540 gigabytes RAM and 1 000 for the price 16350 12:14:06,540 --> 12:14:10,680 now if I type here laptops 16351 12:14:10,680 --> 12:14:12,480 that sorted let's say that I want to 16352 12:14:12,480 --> 12:14:15,540 sort the laptops as if you can see while 16353 12:14:15,540 --> 12:14:18,300 on time pink the IntelliJ is not giving 16354 12:14:18,300 --> 12:14:21,416 me any hints to have this function 16355 12:14:21,416 --> 12:14:23,240 sorted 16356 12:14:23,240 --> 12:14:26,460 as a true fill in that function because 16357 12:14:26,460 --> 12:14:30,180 that function is not working on our 16358 12:14:30,180 --> 12:14:32,880 laptops list and why why is happening 16359 12:14:32,880 --> 12:14:34,020 that 16360 12:14:34,020 --> 12:14:36,980 that is happening because the IntelliJ 16361 12:14:36,980 --> 12:14:38,936 doesn't know 16362 12:14:38,936 --> 12:14:41,880 how we want to sort this laptop list 16363 12:14:41,880 --> 12:14:44,160 that we have here because you want you 16364 12:14:44,160 --> 12:14:45,720 can sort it in many ways you can sort it 16365 12:14:45,720 --> 12:14:48,116 by the name you can sort it by the year 16366 12:14:48,116 --> 12:14:49,860 you can sort it by Dynamic and sort it 16367 12:14:49,860 --> 12:14:52,916 by the price so when you say laptops are 16368 12:14:52,916 --> 12:14:56,520 sorted the IntelliJ is not it's not 16369 12:14:56,520 --> 12:14:58,200 annoying 16370 12:14:58,200 --> 12:15:00,480 what do you mean because previously it 16371 12:15:00,480 --> 12:15:03,180 was working with our integers or and 16372 12:15:03,180 --> 12:15:05,580 without integer class because the 16373 12:15:05,580 --> 12:15:09,060 integer class if you type if you put the 16374 12:15:09,060 --> 12:15:10,380 type here 16375 12:15:10,380 --> 12:15:12,720 back integer 16376 12:15:12,720 --> 12:15:15,540 the integer class or the int class what 16377 12:15:15,540 --> 12:15:17,700 I was doing actually it was inheriting 16378 12:15:17,700 --> 12:15:20,756 from this comparable interface and it 16379 12:15:20,756 --> 12:15:22,616 was implementing the compared to 16380 12:15:22,616 --> 12:15:26,096 function which is used when you call the 16381 12:15:26,096 --> 12:15:29,096 sorted function and inside this 16382 12:15:29,096 --> 12:15:32,820 comparative function it has defined its 16383 12:15:32,820 --> 12:15:35,820 own Logic for comparing integers for 16384 12:15:35,820 --> 12:15:38,460 comparing whole numbers and what we need 16385 12:15:38,460 --> 12:15:40,740 to do in order to be able to 16386 12:15:40,740 --> 12:15:43,436 sort our list of laptops is we need to 16387 12:15:43,436 --> 12:15:45,900 implement the comparable interface and 16388 12:15:45,900 --> 12:15:48,720 you need to Define our own logic which 16389 12:15:48,720 --> 12:15:50,360 is going to determine 16390 12:15:50,360 --> 12:15:53,276 how our list is going to be awarded by 16391 12:15:53,276 --> 12:15:56,700 by which uh property by the Year by the 16392 12:15:56,700 --> 12:15:58,380 ram by the price so this is what you 16393 12:15:58,380 --> 12:16:00,960 need to do and to do that just put here 16394 12:16:00,960 --> 12:16:03,480 a colon and we'll implement the 16395 12:16:03,480 --> 12:16:05,460 interface comparable so we type it 16396 12:16:05,460 --> 12:16:07,436 comparable and we have a suggestion you 16397 12:16:07,436 --> 12:16:09,180 should choose this one 16398 12:16:09,180 --> 12:16:12,596 we put angle brackets and here we type 16399 12:16:12,596 --> 12:16:14,700 laptop because you want to implement 16400 12:16:14,700 --> 12:16:17,640 this we want to compare laptops so 16401 12:16:17,640 --> 12:16:19,740 laptops so this swipe water level there 16402 12:16:19,740 --> 12:16:21,480 then you put curly braces and now we 16403 12:16:21,480 --> 12:16:24,960 need to implement the function so click 16404 12:16:24,960 --> 12:16:27,180 on that it says Implement members you 16405 12:16:27,180 --> 12:16:30,060 have the compare to function now 16406 12:16:30,060 --> 12:16:32,700 you don't need to Define our logic which 16407 12:16:32,700 --> 12:16:38,400 is gonna determine what makes uh uh zero 16408 12:16:38,400 --> 12:16:40,500 the final logic which is going to 16409 12:16:40,500 --> 12:16:42,480 determine how our elements is going to 16410 12:16:42,480 --> 12:16:44,880 be watered by what criteria and I'm 16411 12:16:44,880 --> 12:16:47,880 gonna order the the laptops first by the 16412 12:16:47,880 --> 12:16:50,936 price and to do that to do that just 16413 12:16:50,936 --> 12:16:55,080 type if and to type this and we type in 16414 12:16:55,080 --> 12:16:56,640 this because this is going to be called 16415 12:16:56,640 --> 12:17:00,480 on an inst an instance of our laptop 16416 12:17:00,480 --> 12:17:03,596 left of class is going to call the 16417 12:17:03,596 --> 12:17:05,820 compared to function and it's going to 16418 12:17:05,820 --> 12:17:08,640 compare with another argument so if this 16419 12:17:08,640 --> 12:17:10,740 dot price we say here 16420 12:17:10,740 --> 12:17:13,560 is greater than other 16421 12:17:13,560 --> 12:17:15,000 that price 16422 12:17:15,000 --> 12:17:18,116 we we return as positive number so we 16423 12:17:18,116 --> 12:17:22,140 type return one and a positive Value 16424 12:17:22,140 --> 12:17:25,200 Plus simply shows that the receiver 16425 12:17:25,200 --> 12:17:28,020 object so the objects 16426 12:17:28,020 --> 12:17:32,820 this object is greater than the the uh 16427 12:17:32,820 --> 12:17:35,460 the object which was passed so this is 16428 12:17:35,460 --> 12:17:39,360 what this means this one else lcf is the 16429 12:17:39,360 --> 12:17:42,660 receiver object is not so if this dot 16430 12:17:42,660 --> 12:17:45,776 price is less than other that price 16431 12:17:45,776 --> 12:17:48,596 let's say then gonna return 16432 12:17:48,596 --> 12:17:51,180 -1 a negative number so I'm going to put 16433 12:17:51,180 --> 12:17:53,880 here minus one and what this basically 16434 12:17:53,880 --> 12:17:55,560 logic is I'm just going to swap them so 16435 12:17:55,560 --> 12:17:56,936 it's going to swap the elements based on 16436 12:17:56,936 --> 12:18:00,060 this logic else I'm going to return 0 16437 12:18:00,060 --> 12:18:01,980 and that is going to mean that they are 16438 12:18:01,980 --> 12:18:05,340 equal so if you type here return 0. and 16439 12:18:05,340 --> 12:18:07,200 if you click here it says that you can 16440 12:18:07,200 --> 12:18:09,540 write this in a different way so if you 16441 12:18:09,540 --> 12:18:11,340 click on this 16442 12:18:11,340 --> 12:18:13,500 and let's add the print line to see that 16443 12:18:13,500 --> 12:18:17,160 our if if uh are called so if I type 16444 12:18:17,160 --> 12:18:19,020 here print line 16445 12:18:19,020 --> 12:18:22,096 I'm gonna say 16446 12:18:24,660 --> 12:18:28,936 in if statement 16447 12:18:29,756 --> 12:18:32,540 dollar sign 16448 12:18:33,116 --> 12:18:36,776 actually let's type swapping 16449 12:18:36,776 --> 12:18:39,000 dollar sign 16450 12:18:39,000 --> 12:18:43,276 this dot brand name 16451 12:18:45,540 --> 12:18:46,740 with 16452 12:18:46,740 --> 12:18:48,416 dollar sign 16453 12:18:48,416 --> 12:18:50,276 other 16454 12:18:50,276 --> 12:18:53,096 dot brand 16455 12:18:53,096 --> 12:18:55,140 so let's add this print line also in our 16456 12:18:55,140 --> 12:18:58,020 lcf 16457 12:18:58,020 --> 12:19:00,300 and I'm not gonna add in inside the ears 16458 12:19:00,300 --> 12:19:02,400 because our we don't have Elements which 16459 12:19:02,400 --> 12:19:03,776 are equal but 16460 12:19:03,776 --> 12:19:06,360 you can add that if you want now if I 16461 12:19:06,360 --> 12:19:09,840 type laptops that's sorted now I have a 16462 12:19:09,840 --> 12:19:14,580 suggestion and if I type for each 16463 12:19:14,580 --> 12:19:18,900 print line it so print the element 16464 12:19:18,900 --> 12:19:21,116 now if you run this look you're gonna 16465 12:19:21,116 --> 12:19:25,020 see the elements sorted and uh ascending 16466 12:19:25,020 --> 12:19:28,200 order by their uh their price so you see 16467 12:19:28,200 --> 12:19:31,500 that we have first the brand the brand 16468 12:19:31,500 --> 12:19:35,460 name Dell and we have price 600 then we 16469 12:19:35,460 --> 12:19:36,116 have 16470 12:19:36,116 --> 12:19:39,900 the price uh you have laptop Acer and we 16471 12:19:39,900 --> 12:19:41,880 have eight hundred dollars and have left 16472 12:19:41,880 --> 12:19:44,276 to apoll the brand and you have the 16473 12:19:44,276 --> 12:19:45,840 price one thousand dollars so the 16474 12:19:45,840 --> 12:19:49,020 nearest so now they are sorted using our 16475 12:19:49,020 --> 12:19:51,660 own logic which was defined inside this 16476 12:19:51,660 --> 12:19:55,140 comparable interface and which was 16477 12:19:55,140 --> 12:19:58,500 defined inside the which was defined by 16478 12:19:58,500 --> 12:20:00,776 this comparable interface and which was 16479 12:20:00,776 --> 12:20:02,640 defined inside this function compared to 16480 12:20:02,640 --> 12:20:05,640 that we've overrided inside our client 16481 12:20:05,640 --> 12:20:07,916 and as you can see 16482 12:20:07,916 --> 12:20:11,400 if uh you look you see here that he says 16483 12:20:11,400 --> 12:20:13,980 if an if statement swapping Acer window 16484 12:20:13,980 --> 12:20:16,560 so the elements are swapped based on our 16485 12:20:16,560 --> 12:20:19,256 road in if statements it says swapping 16486 12:20:19,256 --> 12:20:21,960 apple with Acer so they are they are 16487 12:20:21,960 --> 12:20:24,300 swapped based on our logic 16488 12:20:24,300 --> 12:20:25,916 so this is how you can use the 16489 12:20:25,916 --> 12:20:28,436 comparable uh interface but what if I 16490 12:20:28,436 --> 12:20:30,960 want now to compare by the 16491 12:20:30,960 --> 12:20:34,500 let's say uh RAM what can I do I can't 16492 12:20:34,500 --> 12:20:37,140 implement the comparable interface again 16493 12:20:37,140 --> 12:20:40,820 here and I can't uh 16494 12:20:40,860 --> 12:20:43,980 I I can I can add the that property here 16495 12:20:43,980 --> 12:20:45,900 but that is gonna conflict with my logic 16496 12:20:45,900 --> 12:20:49,080 with for the price so what can we do and 16497 12:20:49,080 --> 12:20:51,480 here comes into action comparator 16498 12:20:51,480 --> 12:20:54,720 with comparator you can compare your uh 16499 12:20:54,720 --> 12:20:58,616 your list you can you can uh com you can 16500 12:20:58,616 --> 12:21:00,840 order actually I should say you can sort 16501 12:21:00,840 --> 12:21:04,740 your list using multiple properties so 16502 12:21:04,740 --> 12:21:07,860 you can order it by the uh brand name 16503 12:21:07,860 --> 12:21:09,776 you can order by the year we can already 16504 12:21:09,776 --> 12:21:11,220 buy the ram you can already buy the 16505 12:21:11,220 --> 12:21:14,276 price and with compare because with the 16506 12:21:14,276 --> 12:21:15,776 comparable interface 16507 12:21:15,776 --> 12:21:19,500 we can do that just only by one property 16508 12:21:19,500 --> 12:21:23,400 and that is pretty uh inflexible and 16509 12:21:23,400 --> 12:21:26,880 it's not what you want and how how to 16510 12:21:26,880 --> 12:21:30,116 use this comparator to use the 16511 12:21:30,116 --> 12:21:32,400 comparator we just type here 16512 12:21:32,400 --> 12:21:34,800 class 16513 12:21:34,800 --> 12:21:36,860 I'm gonna call it 16514 12:21:36,860 --> 12:21:41,460 comparator let's say Ram 16515 12:21:41,460 --> 12:21:45,180 and it's gonna extend 16516 12:21:45,180 --> 12:21:48,360 from comparator so type comparator and 16517 12:21:48,360 --> 12:21:50,520 you have the type and for the type here 16518 12:21:50,520 --> 12:21:52,680 we want to compare laptop so you type 16519 12:21:52,680 --> 12:21:54,240 laptop here 16520 12:21:54,240 --> 12:21:56,880 angle brackets curly braces and press 16521 12:21:56,880 --> 12:21:59,115 enter 16522 12:22:00,060 --> 12:22:02,640 and now we need to override 16523 12:22:02,640 --> 12:22:05,040 the 16524 12:22:05,040 --> 12:22:07,680 compare function inside our class so you 16525 12:22:07,680 --> 12:22:09,720 override that function and here again 16526 12:22:09,720 --> 12:22:13,936 let's change those to laptop one 16527 12:22:15,960 --> 12:22:18,116 so no now we have laptop one and laptop 16528 12:22:18,116 --> 12:22:20,416 two 16529 12:22:20,936 --> 12:22:23,900 and we Define 16530 12:22:24,060 --> 12:22:28,256 the same logic so we type here uh 16531 12:22:28,256 --> 12:22:30,240 return 16532 12:22:30,240 --> 12:22:32,580 if because you need to return an integer 16533 12:22:32,580 --> 12:22:34,080 if 16534 12:22:34,080 --> 12:22:36,900 this dot Ram 16535 12:22:36,900 --> 12:22:40,256 actually not this that ram laptop 16536 12:22:40,256 --> 12:22:43,080 laptop1.com 16537 12:22:43,080 --> 12:22:46,560 is greater than laptop 2. 16538 12:22:46,560 --> 12:22:48,776 Ram 16539 12:22:48,776 --> 12:22:51,000 and we need to delete this nullables 16540 12:22:51,000 --> 12:22:52,740 because you need to 16541 12:22:52,740 --> 12:22:55,080 have some errors there 16542 12:22:55,080 --> 12:23:01,640 and put curly braces return one else if 16543 12:23:03,480 --> 12:23:06,660 else if laptop 16544 12:23:06,660 --> 12:23:09,480 one dot Ram 16545 12:23:09,480 --> 12:23:13,880 is less than laptop 2. 16546 12:23:13,880 --> 12:23:19,740 laptop two dot Ram then gonna return I'm 16547 12:23:19,740 --> 12:23:21,900 gonna put curly braces I'm gonna type 16548 12:23:21,900 --> 12:23:22,820 return 16549 12:23:22,820 --> 12:23:25,860 minus one else 16550 12:23:25,860 --> 12:23:28,800 we're gonna return 16551 12:23:28,800 --> 12:23:33,300 zero now if I want to now to compare our 16552 12:23:33,300 --> 12:23:35,820 let's add the space here now I want to 16553 12:23:35,820 --> 12:23:38,580 sort our elements based on the RAM and 16554 12:23:38,580 --> 12:23:41,756 another print line here to add a space 16555 12:23:41,756 --> 12:23:44,936 so I'm gonna have the biggest photos in 16556 12:23:44,936 --> 12:23:47,220 also here one 16557 12:23:47,220 --> 12:23:50,580 so go quickly space it here 16558 12:23:50,580 --> 12:23:53,416 should be print 16559 12:23:55,616 --> 12:23:58,520 and now what we can do is we type 16560 12:23:58,520 --> 12:24:01,320 laptops Dot 16561 12:24:01,320 --> 12:24:02,840 and we type here 16562 12:24:02,840 --> 12:24:06,060 sorted with and which is we choose this 16563 12:24:06,060 --> 12:24:08,640 one which has the parameter Define a 16564 12:24:08,640 --> 12:24:10,500 comparator and we're going to pass our 16565 12:24:10,500 --> 12:24:12,776 comparator that we defined here with the 16566 12:24:12,776 --> 12:24:14,820 comparative function so I'm gonna pause 16567 12:24:14,820 --> 12:24:17,276 I'm going to type sorted with and we 16568 12:24:17,276 --> 12:24:20,276 type comparator 16569 12:24:20,276 --> 12:24:22,220 foreign 16570 12:24:22,220 --> 12:24:24,540 our class 16571 12:24:24,540 --> 12:24:27,256 and 16572 12:24:28,020 --> 12:24:30,616 create an instance and within the type 16573 12:24:30,616 --> 12:24:34,800 dot for each now this one 16574 12:24:34,800 --> 12:24:37,560 for each print line 16575 12:24:37,560 --> 12:24:38,640 it 16576 12:24:38,640 --> 12:24:40,500 so now if you run this you're gonna see 16577 12:24:40,500 --> 12:24:44,640 them sorted by their border Ram 16578 12:24:44,640 --> 12:24:47,276 so if you run this 16579 12:24:47,276 --> 12:24:52,579 as you can see uh if you look at the 16580 12:24:52,800 --> 12:24:55,200 if you look at the first there are they 16581 12:24:55,200 --> 12:24:57,300 are First Source did by their price we 16582 12:24:57,300 --> 12:25:03,116 have 68 800 and 1000 dollars and then we 16583 12:25:03,116 --> 12:25:05,340 have them sorted by the ram so we have 16584 12:25:05,340 --> 12:25:08,040 four for the four which is the dell and 16585 12:25:08,040 --> 12:25:10,800 then we have the Acer which is uh eight 16586 12:25:10,800 --> 12:25:12,416 gigabytes around and you have the Apple 16587 12:25:12,416 --> 12:25:15,840 which is 16 and uh if you look uh 16588 12:25:15,840 --> 12:25:18,116 actually they are in this order in the 16589 12:25:18,116 --> 12:25:20,340 list so let's change this to to let's 16590 12:25:20,340 --> 12:25:24,060 say to 16 and these two to wait 16591 12:25:24,060 --> 12:25:25,140 to 16592 12:25:25,140 --> 12:25:27,300 to make it more clear so let's run this 16593 12:25:27,300 --> 12:25:29,416 again 16594 12:25:31,616 --> 12:25:33,720 so as you can see they are still started 16595 12:25:33,720 --> 12:25:36,180 so we have first the Dell which has four 16596 12:25:36,180 --> 12:25:39,000 gigabytes of RAM then we have the Apple 16597 12:25:39,000 --> 12:25:40,980 which is eight then we have the Acer 16598 12:25:40,980 --> 12:25:43,740 which is 16. so this is why so now it's 16599 12:25:43,740 --> 12:25:47,040 sorting our list this sorted width using 16600 12:25:47,040 --> 12:25:49,860 comparator Ram that we defined we 16601 12:25:49,860 --> 12:25:52,020 created and then we Loop through all our 16602 12:25:52,020 --> 12:25:53,880 all elements because that Returns the 16603 12:25:53,880 --> 12:25:55,380 list so this is how you can use 16604 12:25:55,380 --> 12:25:59,340 comparator and if you want to uh to to 16605 12:25:59,340 --> 12:26:00,900 sort of let's say now the list by the 16606 12:26:00,900 --> 12:26:02,700 year you just create another comparator 16607 12:26:02,700 --> 12:26:04,520 so you type class 16608 12:26:04,520 --> 12:26:07,320 comparator let's say here 16609 12:26:07,320 --> 12:26:10,916 and it's also going to extend comparator 16610 12:26:10,916 --> 12:26:12,660 comparator 16611 12:26:12,660 --> 12:26:15,540 is going to have for the type laptops 16612 12:26:15,540 --> 12:26:18,300 because it's gonna Source laptops curly 16613 12:26:18,300 --> 12:26:21,596 braces we implement the function compare 16614 12:26:21,596 --> 12:26:23,520 and to define the logic for comparing 16615 12:26:23,520 --> 12:26:27,116 elements using uh using the ear so let's 16616 12:26:27,116 --> 12:26:29,400 delete this nullables 16617 12:26:29,400 --> 12:26:32,936 let's type here laptop one 16618 12:26:32,936 --> 12:26:36,380 and here laptop too 16619 12:26:36,776 --> 12:26:41,240 and I'm gonna copy this chord 16620 12:26:43,080 --> 12:26:46,340 I'm gonna paste it here 16621 12:26:47,700 --> 12:26:50,520 but I'm going to change this from Ram to 16622 12:26:50,520 --> 12:26:51,720 here 16623 12:26:51,720 --> 12:26:54,380 so here 16624 12:26:54,416 --> 12:26:56,899 here 16625 12:26:59,580 --> 12:27:02,480 also here 16626 12:27:05,400 --> 12:27:07,800 again 16627 12:27:07,800 --> 12:27:11,400 and we have an error here 16628 12:27:11,400 --> 12:27:13,800 missing so let's add the curly brace 16629 12:27:13,800 --> 12:27:16,040 here 16630 12:27:16,256 --> 12:27:19,020 all right so now if I want to to have 16631 12:27:19,020 --> 12:27:21,540 the element sorted by the year we just 16632 12:27:21,540 --> 12:27:22,680 type here 16633 12:27:22,680 --> 12:27:26,460 let's put a space Also print line 16634 12:27:26,460 --> 12:27:29,096 quotation marks for us this run and 16635 12:27:29,096 --> 12:27:31,740 found the laptops to be sorted by the by 16636 12:27:31,740 --> 12:27:33,900 the by the year I just like laptops that 16637 12:27:33,900 --> 12:27:35,276 sorted with 16638 12:27:35,276 --> 12:27:37,500 and this expects a comparator so I'm 16639 12:27:37,500 --> 12:27:39,660 gonna call our comparator that we 16640 12:27:39,660 --> 12:27:43,020 created comparator here comparator 16641 12:27:43,020 --> 12:27:44,276 here 16642 12:27:44,276 --> 12:27:47,880 an instance of that then for each 16643 12:27:47,880 --> 12:27:51,240 now this one again for each 16644 12:27:51,240 --> 12:27:53,220 print line 16645 12:27:53,220 --> 12:27:54,840 it 16646 12:27:54,840 --> 12:27:57,540 so if you run this 16647 12:27:57,540 --> 12:27:59,640 now we're gonna see the last element 16648 12:27:59,640 --> 12:28:03,240 sorted by their ear I have as you can 16649 12:28:03,240 --> 12:28:04,020 see 16650 12:28:04,020 --> 12:28:06,480 you have uh we have them sorted by the 16651 12:28:06,480 --> 12:28:10,140 ram so this is from this one 16652 12:28:10,140 --> 12:28:12,180 and now it's using comparator here so 16653 12:28:12,180 --> 12:28:13,680 you're sorting the embodies we have 20 16654 12:28:13,680 --> 12:28:17,640 20 21 and 2022 so this is just not like 16655 12:28:17,640 --> 12:28:20,160 the order that they are defined in uh in 16656 12:28:20,160 --> 12:28:22,080 the list so this is how you can use 16657 12:28:22,080 --> 12:28:24,116 comparable and comparable but if you 16658 12:28:24,116 --> 12:28:26,276 think about there is a lot of code here 16659 12:28:26,276 --> 12:28:29,756 to just have to have our 16660 12:28:29,756 --> 12:28:33,660 our list so that you have to we found 16661 12:28:33,660 --> 12:28:35,640 to 16662 12:28:35,640 --> 12:28:38,160 you have to you can use the compare the 16663 12:28:38,160 --> 12:28:40,140 comparable and with the comparable you 16664 12:28:40,140 --> 12:28:43,256 can use it with just one property and if 16665 12:28:43,256 --> 12:28:44,936 you want to use it with another property 16666 12:28:44,936 --> 12:28:46,680 you have to create those comparators 16667 12:28:46,680 --> 12:28:48,596 which gives you some flexibility but 16668 12:28:48,596 --> 12:28:51,360 still you have a lot of code and we can 16669 12:28:51,360 --> 12:28:54,540 remove all of this code 16670 12:28:54,540 --> 12:28:56,580 and use Lambda 16671 12:28:56,580 --> 12:28:58,256 functions 16672 12:28:58,256 --> 12:29:01,080 so I can delete all of this 16673 12:29:01,080 --> 12:29:04,380 all of this comparable that we have here 16674 12:29:04,380 --> 12:29:07,200 and instead of using those comparators 16675 12:29:07,200 --> 12:29:09,360 and all of this stuff what I can do is I 16676 12:29:09,360 --> 12:29:12,256 can delete all of this 16677 12:29:12,900 --> 12:29:16,080 I can little or this also 16678 12:29:16,080 --> 12:29:18,180 now what I can do is I can add here 16679 12:29:18,180 --> 12:29:22,200 laptops dot sorted 16680 12:29:22,200 --> 12:29:25,320 with and here 16681 12:29:25,320 --> 12:29:27,596 so we choose this one sorted with which 16682 12:29:27,596 --> 12:29:30,180 has this parameter comparator press 16683 12:29:30,180 --> 12:29:34,460 enter and here we type 16684 12:29:34,460 --> 12:29:36,180 compare 16685 12:29:36,180 --> 12:29:37,616 by 16686 12:29:37,616 --> 12:29:40,616 and here we type and this line of code 16687 12:29:40,616 --> 12:29:43,860 that we have here we it what it actually 16688 12:29:43,860 --> 12:29:46,560 is doing is creating a comparator but is 16689 12:29:46,560 --> 12:29:48,596 creating a comparator using a Lambda 16690 12:29:48,596 --> 12:29:52,680 expression so here you can type it dot 16691 12:29:52,680 --> 12:29:55,740 it's dot let's say uh price 16692 12:29:55,740 --> 12:30:00,240 right and then for each print line 16693 12:30:00,240 --> 12:30:03,540 it so this is equivalent to creating 16694 12:30:03,540 --> 12:30:07,560 that comparator here and inheriting from 16695 12:30:07,560 --> 12:30:09,776 the comparator interface and defining 16696 12:30:09,776 --> 12:30:13,680 the logic for uh uh or ordering for 16697 12:30:13,680 --> 12:30:16,020 sorting them so now if you if you run 16698 12:30:16,020 --> 12:30:18,180 this this will have the same effect like 16699 12:30:18,180 --> 12:30:21,480 the comparator price that we have 16700 12:30:21,480 --> 12:30:23,160 implemented previous as you can see they 16701 12:30:23,160 --> 12:30:25,020 are ordered now by the price and I can 16702 12:30:25,020 --> 12:30:25,980 do 16703 12:30:25,980 --> 12:30:29,460 now on another line of code Source dot 16704 12:30:29,460 --> 12:30:32,640 laptop dot sorted weight 16705 12:30:32,640 --> 12:30:35,936 and we type here compare by we type it 16706 12:30:35,936 --> 12:30:40,276 dot let's say uh 16707 12:30:40,916 --> 12:30:43,460 Ram 16708 12:30:43,616 --> 12:30:46,980 dot for each 16709 12:30:46,980 --> 12:30:48,900 print line 16710 12:30:48,900 --> 12:30:50,220 and it 16711 12:30:50,220 --> 12:30:52,436 now if you run this I'm gonna have the 16712 12:30:52,436 --> 12:30:53,936 same output let's add the space between 16713 12:30:53,936 --> 12:30:57,020 them print line 16714 12:30:59,400 --> 12:31:01,800 so let's add the space to see them to 16715 12:31:01,800 --> 12:31:04,560 see the code more clearly 16716 12:31:04,560 --> 12:31:06,776 and now as the as you can see the first 16717 12:31:06,776 --> 12:31:08,580 they are sorted by their price and then 16718 12:31:08,580 --> 12:31:11,340 they are sorted by their uh their RAM 16719 12:31:11,340 --> 12:31:14,580 memory so this code is equivalent to 16720 12:31:14,580 --> 12:31:16,320 creating what we had previously with the 16721 12:31:16,320 --> 12:31:19,860 comparator and implementing the dotting 16722 12:31:19,860 --> 12:31:21,960 interface and the function and actually 16723 12:31:21,960 --> 12:31:24,720 there is a shorter way even to do this 16724 12:31:24,720 --> 12:31:28,320 we can delete this let's say so the 16725 12:31:28,320 --> 12:31:31,020 sorted with and compare bear compare by 16726 12:31:31,020 --> 12:31:36,360 and we can just type here sort by 16727 12:31:36,360 --> 12:31:40,020 so this this will create internally the 16728 12:31:40,020 --> 12:31:43,380 same thing that we had previously so if 16729 12:31:43,380 --> 12:31:46,200 I type it dot ROM 16730 12:31:46,200 --> 12:31:48,300 and I can do the same thing here I can 16731 12:31:48,300 --> 12:31:50,880 delete all of this and I just 16732 12:31:50,880 --> 12:31:54,360 could type here selector the sort by 16733 12:31:54,360 --> 12:31:57,720 eat that price now this will have the 16734 12:31:57,720 --> 12:31:59,880 same effect like all the code that we 16735 12:31:59,880 --> 12:32:03,116 had previously but now it's extremely 16736 12:32:03,116 --> 12:32:05,880 concise and easier to read you just say 16737 12:32:05,880 --> 12:32:07,980 laptop sorted by sorted them by their 16738 12:32:07,980 --> 12:32:10,320 price so the elliptos by the ram so if 16739 12:32:10,320 --> 12:32:12,180 you run this code 16740 12:32:12,180 --> 12:32:14,580 you'll see the exact output in the 16741 12:32:14,580 --> 12:32:17,936 console because what this function is 16742 12:32:17,936 --> 12:32:20,700 doing uh actually we should Loop through 16743 12:32:20,700 --> 12:32:23,520 them so let's add the print line here 16744 12:32:23,520 --> 12:32:25,616 print line it 16745 12:32:25,616 --> 12:32:28,256 so let's put here for each 16746 12:32:28,256 --> 12:32:29,880 put into line 16747 12:32:29,880 --> 12:32:31,256 it 16748 12:32:31,256 --> 12:32:33,300 so now if you run this you will see the 16749 12:32:33,300 --> 12:32:36,480 exact same output we just this simple 16750 12:32:36,480 --> 12:32:37,860 line of code 16751 12:32:37,860 --> 12:32:39,360 so as you can see you have the same 16752 12:32:39,360 --> 12:32:41,220 output we have them sorted divide their 16753 12:32:41,220 --> 12:32:44,096 price first and then they are sorted by 16754 12:32:44,096 --> 12:32:47,880 their RAM memory and if you hold Ctrl on 16755 12:32:47,880 --> 12:32:51,435 this sorted by function 16756 12:32:54,540 --> 12:32:56,820 as you can see this actually internally 16757 12:32:56,820 --> 12:32:58,800 calls the sorted width which receives 16758 12:32:58,800 --> 12:33:00,360 the comparator and is calling the 16759 12:33:00,360 --> 12:33:02,400 compound the compare buy so this is like 16760 12:33:02,400 --> 12:33:05,700 a syntactical sugar for us which makes 16761 12:33:05,700 --> 12:33:07,680 the code more concise but internally 16762 12:33:07,680 --> 12:33:11,040 it's using using that sorted weight and 16763 12:33:11,040 --> 12:33:13,080 uh if you want to use the salt to do it 16764 12:33:13,080 --> 12:33:15,480 one advance for using a sorted with 16765 12:33:15,480 --> 12:33:17,096 let's say that I type here another print 16766 12:33:17,096 --> 12:33:18,720 line to add a space 16767 12:33:18,720 --> 12:33:21,720 let's put photos lesson 16768 12:33:21,720 --> 12:33:24,540 should be in quotation marks 16769 12:33:24,540 --> 12:33:28,680 forward slash n one 16770 12:33:28,680 --> 12:33:30,960 good thing about the sorted width so 16771 12:33:30,960 --> 12:33:32,936 let's type laptops 16772 12:33:32,936 --> 12:33:34,700 Dot 16773 12:33:34,700 --> 12:33:37,500 sorted weight 16774 12:33:37,500 --> 12:33:39,500 and here we type 16775 12:33:39,500 --> 12:33:42,800 compare by 16776 12:33:42,960 --> 12:33:47,580 let's say it dot year one thing that you 16777 12:33:47,580 --> 12:33:49,860 can do with the sorted with if you call 16778 12:33:49,860 --> 12:33:51,540 specific restorative function you can 16779 12:33:51,540 --> 12:33:55,200 put that and then buy so it will sort by 16780 12:33:55,200 --> 12:33:59,096 year and then by I know let's say uh it 16781 12:33:59,096 --> 12:34:02,276 dot price so this is what you would get 16782 12:34:02,276 --> 12:34:04,436 what you can do with the sorted with you 16783 12:34:04,436 --> 12:34:07,436 can sorted by year first and then by uh 16784 12:34:07,436 --> 12:34:11,060 by the price so if you run this 16785 12:34:13,580 --> 12:34:16,020 as you can see they are sorted first by 16786 12:34:16,020 --> 12:34:18,660 the year so 16787 12:34:18,660 --> 12:34:21,116 and then buy their uh their price so 16788 12:34:21,116 --> 12:34:23,880 this way we get this in the output 16789 12:34:23,880 --> 12:34:28,436 now I'm gonna end this video and uh you 16790 12:34:28,436 --> 12:34:31,140 should you should use this uh this one 16791 12:34:31,140 --> 12:34:32,880 always because it's 16792 12:34:32,880 --> 12:34:36,960 concise monitable yeah very easy to to 16793 12:34:36,960 --> 12:34:40,936 to read and to and to type is 16794 12:34:40,936 --> 12:34:43,436 in contrast with what we had previously 16795 12:34:43,436 --> 12:34:47,276 with the or creating that comparators 16796 12:34:47,276 --> 12:34:48,480 and implementing the comparator 16797 12:34:48,480 --> 12:34:50,400 interface or the comparable 16798 12:34:50,400 --> 12:34:52,980 this is a lot a lot more concise and 16799 12:34:52,980 --> 12:34:55,320 easier to read so this is our discussion 16800 12:34:55,320 --> 12:34:57,000 about the comparable and comparator 16801 12:34:57,000 --> 12:34:58,980 interface so this line of code I repeat 16802 12:34:58,980 --> 12:35:02,096 this line of code that we have here 16803 12:35:02,096 --> 12:35:04,860 this sorted by it does RAM is creating 16804 12:35:04,860 --> 12:35:06,596 internally that comparator that we 16805 12:35:06,596 --> 12:35:07,500 talked about 16806 12:35:07,500 --> 12:35:11,880 the node we're just using this Lambda 16807 12:35:11,880 --> 12:35:13,616 function 16808 12:35:13,616 --> 12:35:16,140 so I'm going to end the video now and 16809 12:35:16,140 --> 12:35:18,180 see you in the next video so there is 16810 12:35:18,180 --> 12:35:20,220 one mistake that I did previously so if 16811 12:35:20,220 --> 12:35:24,300 I press Ctrl Z to have this code uh when 16812 12:35:24,300 --> 12:35:26,756 I run this code you you didn't sound 16813 12:35:26,756 --> 12:35:29,400 output then the list sorted by the year 16814 12:35:29,400 --> 12:35:30,720 and then by the price and that is 16815 12:35:30,720 --> 12:35:33,776 because I forgot to add the four each 16816 12:35:33,776 --> 12:35:35,936 to Loop through our elements so if I add 16817 12:35:35,936 --> 12:35:38,220 the four each and now if I run this now 16818 12:35:38,220 --> 12:35:39,960 we're gonna see them 16819 12:35:39,960 --> 12:35:41,936 sorted first by the year and then buy 16820 12:35:41,936 --> 12:35:43,800 the price because previously it wasn't 16821 12:35:43,800 --> 12:35:46,616 so this is why it wasn't now if you look 16822 12:35:46,616 --> 12:35:48,660 they are sorted by the first by the year 16823 12:35:48,660 --> 12:35:52,380 so we have 2020 2021 and 22 and then buy 16824 12:35:52,380 --> 12:35:54,720 uh by their price so this is why 16825 12:35:54,720 --> 12:35:58,980 previously you saw this one this output 16826 12:35:58,980 --> 12:36:01,916 and also I saw this output that but uh 16827 12:36:01,916 --> 12:36:05,640 some way I uh I didn't uh didn't saw 16828 12:36:05,640 --> 12:36:09,560 that I forgot to add the foliage here so 16829 12:36:09,560 --> 12:36:13,080 this is how you can also sort the sorted 16830 12:36:13,080 --> 12:36:15,596 width you can um sort it by more 16831 12:36:15,596 --> 12:36:17,096 elements you can put here I think 16832 12:36:17,096 --> 12:36:18,180 another 16833 12:36:18,180 --> 12:36:20,580 then buy 16834 12:36:20,580 --> 12:36:22,560 not here 16835 12:36:22,560 --> 12:36:26,596 we can put another so 16836 12:36:26,596 --> 12:36:29,580 dot then by you can put another then by 16837 12:36:29,580 --> 12:36:33,480 here if you want so eat that so let's 16838 12:36:33,480 --> 12:36:35,936 say your RAM 16839 12:36:35,936 --> 12:36:39,180 so you can add more more than buy if you 16840 12:36:39,180 --> 12:36:40,200 want so 16841 12:36:40,200 --> 12:36:42,116 now I'm gonna end with the video see you 16842 12:36:42,116 --> 12:36:43,800 and see you in the next video now it's 16843 12:36:43,800 --> 12:36:45,540 time to start a discussion about binding 16844 12:36:45,540 --> 12:36:47,340 the search but before you look at binary 16845 12:36:47,340 --> 12:36:48,720 search you're going to first look at 16846 12:36:48,720 --> 12:36:51,660 linear search to see why a binary search 16847 12:36:51,660 --> 12:36:53,936 works better so I'm gonna type a 16848 12:36:53,936 --> 12:36:56,040 function here I'm going to type private 16849 12:36:56,040 --> 12:36:59,820 fun it's going to be called search 16850 12:36:59,820 --> 12:37:01,980 element 16851 12:37:01,980 --> 12:37:03,900 and you're going to type search elements 16852 12:37:03,900 --> 12:37:05,700 so this parameter is going to be the 16853 12:37:05,700 --> 12:37:08,899 element that we are searching for 16854 12:37:12,416 --> 12:37:15,720 and it's gonna then another parameter is 16855 12:37:15,720 --> 12:37:18,240 going to be a list 16856 12:37:18,240 --> 12:37:19,980 of numbers so it's going to be a 16857 12:37:19,980 --> 12:37:21,240 multiple list 16858 12:37:21,240 --> 12:37:24,200 of ins 16859 12:37:24,960 --> 12:37:27,000 and it's going to return a number the 16860 12:37:27,000 --> 12:37:29,220 search element or minus one if you don't 16861 12:37:29,220 --> 12:37:31,140 find the search element 16862 12:37:31,140 --> 12:37:32,820 and here we're going to Loop through our 16863 12:37:32,820 --> 12:37:35,400 release so I'm going to type for number 16864 12:37:35,400 --> 12:37:38,776 in our list of numbers 16865 12:37:38,936 --> 12:37:41,580 if 16866 12:37:41,580 --> 12:37:46,080 at a particular iteration or number 16867 12:37:46,080 --> 12:37:48,360 it's equal to the search 16868 12:37:48,360 --> 12:37:50,276 element that means that we found the 16869 12:37:50,276 --> 12:37:51,660 element that we found the number so 16870 12:37:51,660 --> 12:37:54,360 we're going to return number 16871 12:37:54,360 --> 12:37:57,720 else if you come down here and after we 16872 12:37:57,720 --> 12:37:59,936 Loop and we did our if checks we didn't 16873 12:37:59,936 --> 12:38:01,980 return the number that means we didn't 16874 12:38:01,980 --> 12:38:02,936 find a number we're going to return 16875 12:38:02,936 --> 12:38:05,276 minus on here 16876 12:38:05,276 --> 12:38:08,936 now I'm gonna add the print line here 16877 12:38:08,936 --> 12:38:11,700 and I'm gonna type search 16878 12:38:11,700 --> 12:38:14,040 element because this is going to return 16879 12:38:14,040 --> 12:38:15,360 an element and then you're gonna output 16880 12:38:15,360 --> 12:38:17,400 with the print line dot in the console 16881 12:38:17,400 --> 12:38:21,000 and I'm going to type here 27 16882 12:38:21,000 --> 12:38:22,740 and for the next parameter I'm going to 16883 12:38:22,740 --> 12:38:24,596 define a list of numbers one time 16884 12:38:24,596 --> 12:38:26,400 Multiple List off 16885 12:38:26,400 --> 12:38:28,080 and here I'm going to define a number 16886 12:38:28,080 --> 12:38:31,140 from 1 to 30 so I'm going to type here 16887 12:38:31,140 --> 12:38:34,616 the numbers one 16888 12:38:38,340 --> 12:38:42,480 and our last element 30. 16889 12:38:42,480 --> 12:38:44,756 now if you run this code you're gonna 16890 12:38:44,756 --> 12:38:48,720 see in the output 27 16891 12:38:50,520 --> 12:38:54,000 so we get as expected 27 but let's 16892 12:38:54,000 --> 12:38:56,400 declare a variable here I 16893 12:38:56,400 --> 12:38:59,460 to number the number of iteration it 16894 12:38:59,460 --> 12:39:01,860 took it took to find the elements I'm 16895 12:39:01,860 --> 12:39:05,000 going to assign 0 to it 16896 12:39:05,040 --> 12:39:06,776 I'm going to increment it every time you 16897 12:39:06,776 --> 12:39:09,180 Loop so I'm going to type Val I'm going 16898 12:39:09,180 --> 12:39:11,820 to type I sorry plus plus 16899 12:39:11,820 --> 12:39:14,756 and I'm gonna print line here 16900 12:39:14,756 --> 12:39:19,740 and I'm gonna it's gonna say search 16901 12:39:20,160 --> 12:39:21,660 number 16902 12:39:21,660 --> 12:39:22,916 colon 16903 12:39:22,916 --> 12:39:25,860 dollar sign I 16904 12:39:25,860 --> 12:39:28,680 so now if you run this we're gonna see 16905 12:39:28,680 --> 12:39:31,320 how many iteration it took to found our 16906 12:39:31,320 --> 12:39:33,560 element 16907 12:39:34,256 --> 12:39:36,596 and if you look at the console as you 16908 12:39:36,596 --> 12:39:38,720 can see it took 27 16909 12:39:38,720 --> 12:39:42,240 27 iteration before we found our element 16910 12:39:42,240 --> 12:39:46,256 because the way the linear search is 16911 12:39:46,256 --> 12:39:49,500 working is it's going through all of the 16912 12:39:49,500 --> 12:39:51,840 elements is checking out each iteration 16913 12:39:51,840 --> 12:39:54,900 if the number of that particular 16914 12:39:54,900 --> 12:39:56,756 iteration is equal to the search element 16915 12:39:56,756 --> 12:39:58,320 if not it's going to loop again and 16916 12:39:58,320 --> 12:40:01,436 again again until it reaches 27 then it 16917 12:40:01,436 --> 12:40:03,360 found that element and it Returns the 16918 12:40:03,360 --> 12:40:04,580 element and you get that element 16919 12:40:04,580 --> 12:40:06,900 outputted here in the console 16920 12:40:06,900 --> 12:40:09,200 so this is what we call a linear search 16921 12:40:09,200 --> 12:40:12,360 and this is not particularly good 16922 12:40:12,360 --> 12:40:15,116 because imagine if you have 1 billion 16923 12:40:15,116 --> 12:40:17,756 elements or 10 billion elements and you 16924 12:40:17,756 --> 12:40:19,740 need to find I don't know 16925 12:40:19,740 --> 12:40:24,360 maybe the 7th billion element in the in 16926 12:40:24,360 --> 12:40:26,756 that array that is going to be very hard 16927 12:40:26,756 --> 12:40:28,740 and it's going to take a 16928 12:40:28,740 --> 12:40:31,256 time and memory and because of that we 16929 12:40:31,256 --> 12:40:33,596 have another 16930 12:40:33,596 --> 12:40:37,020 data structure for a searching element 16931 12:40:37,020 --> 12:40:39,116 and that is called binary search and I'm 16932 12:40:39,116 --> 12:40:40,560 going to type the code for binary search 16933 12:40:40,560 --> 12:40:42,360 here and also you're going to see the 16934 12:40:42,360 --> 12:40:44,460 number of iteration it tooks to 16935 12:40:44,460 --> 12:40:46,320 find element and also I'm going to 16936 12:40:46,320 --> 12:40:48,360 explain how binary search works so I'm 16937 12:40:48,360 --> 12:40:51,020 going to delete this 16938 12:40:53,276 --> 12:40:56,756 I'm going to type here VAR low I'm going 16939 12:40:56,756 --> 12:40:58,500 to put equals to zero 16940 12:40:58,500 --> 12:41:00,320 VAR 16941 12:41:00,320 --> 12:41:02,820 High equals to 16942 12:41:02,820 --> 12:41:06,776 numbers that size minus one I'm going to 16943 12:41:06,776 --> 12:41:09,116 explain the code immediately 16944 12:41:09,116 --> 12:41:11,820 here I'm going to type while 16945 12:41:11,820 --> 12:41:14,040 low is less than equal 16946 12:41:14,040 --> 12:41:16,640 to high 16947 12:41:17,220 --> 12:41:20,520 here I'm gonna type Val mid so now we're 16948 12:41:20,520 --> 12:41:22,680 going to get the middle element so I'm 16949 12:41:22,680 --> 12:41:25,616 going to put parenthesis slow 16950 12:41:25,616 --> 12:41:29,700 plus High divided by 2 and that's going 16951 12:41:29,700 --> 12:41:31,980 to give us the middle element 15. 16952 12:41:31,980 --> 12:41:33,180 then 16953 12:41:33,180 --> 12:41:35,400 I'm going to type Val comparison so I'm 16954 12:41:35,400 --> 12:41:37,916 going to type CMP 16955 12:41:37,916 --> 12:41:41,400 equals to numbers 16956 12:41:41,400 --> 12:41:44,640 dot get the middle element compared to 16957 12:41:44,640 --> 12:41:45,480 we're going to compare the middle 16958 12:41:45,480 --> 12:41:49,160 element with the search element 16959 12:41:50,160 --> 12:41:54,140 then I'm going to type here if 16960 12:42:02,880 --> 12:42:05,300 now I'm going to type if 16961 12:42:05,300 --> 12:42:08,520 comparison is less than zero 16962 12:42:08,520 --> 12:42:11,756 then I'm going to type here low 16963 12:42:11,756 --> 12:42:13,616 equals to 16964 12:42:13,616 --> 12:42:15,480 mid 16965 12:42:15,480 --> 12:42:17,220 plus one 16966 12:42:17,220 --> 12:42:19,560 lcf 16967 12:42:19,560 --> 12:42:22,980 if comparison is not less than zero 16968 12:42:22,980 --> 12:42:25,080 so it's greater than zero 16969 12:42:25,080 --> 12:42:27,720 I'm going to type here that 16970 12:42:27,720 --> 12:42:29,096 low 16971 12:42:29,096 --> 12:42:31,800 is equal to 16972 12:42:31,800 --> 12:42:33,900 High 16973 12:42:33,900 --> 12:42:35,096 sorry 16974 12:42:35,096 --> 12:42:37,436 is equal to my mid 16975 12:42:37,436 --> 12:42:39,560 -1 16976 12:42:40,680 --> 12:42:42,240 else 16977 12:42:42,240 --> 12:42:44,220 when I type here return 16978 12:42:44,220 --> 12:42:46,080 numbers 16979 12:42:46,080 --> 12:42:48,360 dot get meet and that is going to give 16980 12:42:48,360 --> 12:42:49,980 us the middle element 16981 12:42:49,980 --> 12:42:52,616 now if I declare here a variable 16982 12:42:52,616 --> 12:42:53,880 VAR 16983 12:42:53,880 --> 12:42:56,820 I and if I assign 0 to 8 16984 12:42:56,820 --> 12:43:00,060 and if I increment that I at each 16985 12:43:00,060 --> 12:43:01,800 iteration 16986 12:43:01,800 --> 12:43:05,596 and if I add a print line here 16987 12:43:05,640 --> 12:43:08,340 search 16988 12:43:08,340 --> 12:43:11,040 number 16989 12:43:11,040 --> 12:43:13,380 dollar sign I 16990 12:43:13,380 --> 12:43:16,616 now if you run this you're gonna see uh 16991 12:43:16,616 --> 12:43:18,000 you're gonna actually I should return 16992 12:43:18,000 --> 12:43:19,916 minus on here 16993 12:43:19,916 --> 12:43:22,980 because uh if at the end of our looping 16994 12:43:22,980 --> 12:43:27,720 and after we checked our all of our list 16995 12:43:27,720 --> 12:43:29,160 and we didn't find the number then we 16996 12:43:29,160 --> 12:43:30,540 determine this one meaning that we 16997 12:43:30,540 --> 12:43:32,276 didn't find the element 16998 12:43:32,276 --> 12:43:35,479 so now if you run this again 16999 12:43:41,096 --> 12:43:43,640 we get 17000 12:43:43,800 --> 12:43:47,096 search number one two three and then we 17001 12:43:47,096 --> 12:43:49,740 get 27 and as you can see this is 17002 12:43:49,740 --> 12:43:52,500 significantly different from our linear 17003 12:43:52,500 --> 12:43:54,900 search because now we only took three 17004 12:43:54,900 --> 12:43:57,840 searched and then I found the he found 17005 12:43:57,840 --> 12:44:00,116 our element previously we had to search 17006 12:44:00,116 --> 12:44:02,520 through all of our elements 17007 12:44:02,520 --> 12:44:06,240 through our elements until uh the number 17008 12:44:06,240 --> 12:44:09,116 27 to to get the number seven but now we 17009 12:44:09,116 --> 12:44:10,560 only get the 17010 12:44:10,560 --> 12:44:13,560 three iteration and we get the number 27 17011 12:44:13,560 --> 12:44:16,020 and how is that possible this is because 17012 12:44:16,020 --> 12:44:17,580 now we're using what is called the 17013 12:44:17,580 --> 12:44:20,160 binary search and how this binary search 17014 12:44:20,160 --> 12:44:23,580 works I'm going to explain it now so now 17015 12:44:23,580 --> 12:44:25,380 let's see how the code works 17016 12:44:25,380 --> 12:44:27,900 so first we have a variable declare code 17017 12:44:27,900 --> 12:44:30,596 load which assign a value of 0. then we 17018 12:44:30,596 --> 12:44:31,980 have another variable called height 17019 12:44:31,980 --> 12:44:34,200 which assign number the PSI minus one so 17020 12:44:34,200 --> 12:44:36,240 we assign the size of the array to our 17021 12:44:36,240 --> 12:44:37,020 high 17022 12:44:37,020 --> 12:44:39,116 then we have a variable called I to 17023 12:44:39,116 --> 12:44:42,060 which you assign 0. then we have a while 17024 12:44:42,060 --> 12:44:44,160 loop and we start looping by this 17025 12:44:44,160 --> 12:44:46,200 condition while low is less than equal 17026 12:44:46,200 --> 12:44:47,340 to high 17027 12:44:47,340 --> 12:44:50,400 then we increment our I to keep track of 17028 12:44:50,400 --> 12:44:52,500 how many iteration it takes before we 17029 12:44:52,500 --> 12:44:55,140 find the element and without any console 17030 12:44:55,140 --> 12:44:57,416 search element and the number at that 17031 12:44:57,416 --> 12:44:58,680 iteration 17032 12:44:58,680 --> 12:45:01,256 then we have a variable called mid and 17033 12:45:01,256 --> 12:45:02,756 this is going to give us the middle 17034 12:45:02,756 --> 12:45:05,160 element and to get the middle element we 17035 12:45:05,160 --> 12:45:08,220 add the lower lowest element plus the 17036 12:45:08,220 --> 12:45:10,980 highest element so yeah we'll add here 0 17037 12:45:10,980 --> 12:45:13,380 plus 30 divided by 2 which is 15. so 17038 12:45:13,380 --> 12:45:14,756 this is the 17039 12:45:14,756 --> 12:45:16,560 middle element 17040 12:45:16,560 --> 12:45:19,020 then we declare another variable called 17041 12:45:19,020 --> 12:45:22,800 comparison and we type here numbers that 17042 12:45:22,800 --> 12:45:24,416 get the middle element which is 15 17043 12:45:24,416 --> 12:45:28,200 compared to search element and if the 17044 12:45:28,200 --> 12:45:32,276 middle element is less than the search 17045 12:45:32,276 --> 12:45:35,520 element that means that the search 17046 12:45:35,520 --> 12:45:38,340 element is on the right of the numbers 17047 12:45:38,340 --> 12:45:41,220 which come after the 15 and because of 17048 12:45:41,220 --> 12:45:43,860 that this is gonna return because our 17049 12:45:43,860 --> 12:45:46,256 middle element is less than the search 17050 12:45:46,256 --> 12:45:47,936 element is going to return a negative 17051 12:45:47,936 --> 12:45:50,520 number so this is going to be true 17052 12:45:50,520 --> 12:45:52,800 comparison is going to be less than zero 17053 12:45:52,800 --> 12:45:55,980 and then I'm gonna assign low equals to 17054 12:45:55,980 --> 12:45:58,820 Mid Plus on so now 17055 12:45:58,820 --> 12:46:04,200 the low variable has the value 16 all 17056 12:46:04,200 --> 12:46:07,860 right now it will loop again it will uh 17057 12:46:07,860 --> 12:46:10,860 it will uh do it will have the I 17058 12:46:10,860 --> 12:46:13,020 incrementally it will 17059 12:46:13,020 --> 12:46:15,000 do the while loop it will increment the 17060 12:46:15,000 --> 12:46:17,460 I again and it will output into I in the 17061 12:46:17,460 --> 12:46:21,840 console now here now it will calculate 17062 12:46:21,840 --> 12:46:25,080 again the middle element and it will it 17063 12:46:25,080 --> 12:46:27,660 will add now 16. 17064 12:46:27,660 --> 12:46:28,916 Plus 17065 12:46:28,916 --> 12:46:32,160 30 divided by 2 and that is going to 17066 12:46:32,160 --> 12:46:34,616 give give us 23. 17067 12:46:34,616 --> 12:46:35,756 all right 17068 12:46:35,756 --> 12:46:38,400 now that is the middle element now and 17069 12:46:38,400 --> 12:46:42,000 what we do now here is we again uh I 17070 12:46:42,000 --> 12:46:44,416 have our variable comparison and we get 17071 12:46:44,416 --> 12:46:48,960 numbers that get 23 so we get the 23 17072 12:46:48,960 --> 12:46:50,756 element and we compile with our search 17073 12:46:50,756 --> 12:46:53,400 element which is 27. 17074 12:46:53,400 --> 12:46:57,720 and again because our middle element 17075 12:46:57,720 --> 12:47:00,900 is less than the search element this is 17076 12:47:00,900 --> 12:47:02,756 going to return on any negative number 17077 12:47:02,756 --> 12:47:04,616 so this shift check is going to be true 17078 12:47:04,616 --> 12:47:08,400 again now what is gonna 17079 12:47:08,400 --> 12:47:11,580 happen is that now low 17080 12:47:11,580 --> 12:47:15,000 is going to be equal to Mid 17081 12:47:15,000 --> 12:47:18,300 plus 1 and the low previously was equal 17082 12:47:18,300 --> 12:47:21,776 to 23 so now I'm going to have 23 plus 1 17083 12:47:21,776 --> 12:47:24,416 which is 24 all right 17084 12:47:24,416 --> 12:47:27,116 now this will loop again 17085 12:47:27,116 --> 12:47:29,700 or loop again so 17086 12:47:29,700 --> 12:47:32,640 as you can see this basically breaks the 17087 12:47:32,640 --> 12:47:36,660 the list in half at each iteration and 17088 12:47:36,660 --> 12:47:38,040 now this will loop again it will 17089 12:47:38,040 --> 12:47:40,200 increment the eye to Output I and now it 17090 12:47:40,200 --> 12:47:42,000 will calculate again the middle element 17091 12:47:42,000 --> 12:47:45,240 now the middle element is 24. 17092 12:47:45,240 --> 12:47:47,880 and the high element is 30 and we have 17093 12:47:47,880 --> 12:47:49,980 24 plus 30 17094 12:47:49,980 --> 12:47:54,256 divided by 2. and that is 27 the exact 17095 12:47:54,256 --> 12:47:57,596 number that we are searching for so this 17096 12:47:57,596 --> 12:47:59,936 is how binary search works it will 17097 12:47:59,936 --> 12:48:01,680 compare the middle element with the 17098 12:48:01,680 --> 12:48:04,200 search element if the middle element is 17099 12:48:04,200 --> 12:48:05,820 less than the search element that means 17100 12:48:05,820 --> 12:48:08,400 that your element is on the right side 17101 12:48:08,400 --> 12:48:10,616 of the element which come after the 17102 12:48:10,616 --> 12:48:12,900 middle element so all the elements from 17103 12:48:12,900 --> 12:48:14,340 the left are going to be excluded and 17104 12:48:14,340 --> 12:48:16,616 it's going to search only on the on the 17105 12:48:16,616 --> 12:48:17,520 right side 17106 12:48:17,520 --> 12:48:20,220 then it will split the list again in 17107 12:48:20,220 --> 12:48:22,680 half if the middle element again is less 17108 12:48:22,680 --> 12:48:24,240 than the search element it will go to 17109 12:48:24,240 --> 12:48:26,276 the right again it will speed the least 17110 12:48:26,276 --> 12:48:29,096 in half I need to sooner or later it 17111 12:48:29,096 --> 12:48:32,040 will find the element to to to the right 17112 12:48:32,040 --> 12:48:33,480 or to the left 17113 12:48:33,480 --> 12:48:35,700 so this is how one research version is 17114 12:48:35,700 --> 12:48:42,560 very powerful because for a very big uh 17115 12:48:42,660 --> 12:48:45,480 list you can use binary search and it's 17116 12:48:45,480 --> 12:48:48,000 more effective that uh only the linear 17117 12:48:48,000 --> 12:48:49,560 search 17118 12:48:49,560 --> 12:48:53,040 and uh this is the code that I type but 17119 12:48:53,040 --> 12:48:54,596 it doesn't you don't need to use this 17120 12:48:54,596 --> 12:48:57,060 code because uh I only type this code to 17121 12:48:57,060 --> 12:48:58,980 show you how the binary search works but 17122 12:48:58,980 --> 12:49:01,200 usually you'll use the binary search 17123 12:49:01,200 --> 12:49:03,416 function provided by The Collection so 17124 12:49:03,416 --> 12:49:06,740 you will just type here return 17125 12:49:07,020 --> 12:49:08,580 numbers 17126 12:49:08,580 --> 12:49:11,096 we're gonna put square brackets and I'm 17127 12:49:11,096 --> 12:49:13,200 gonna type binary I'm going to type here 17128 12:49:13,200 --> 12:49:17,756 numbers again binary dot binary search 17129 12:49:17,756 --> 12:49:20,400 because the binary search function 17130 12:49:20,400 --> 12:49:22,740 provided by The Collection only return 17131 12:49:22,740 --> 12:49:26,240 the returns the index so if I 17132 12:49:26,240 --> 12:49:30,500 search for the 27 element 17133 12:49:31,800 --> 12:49:34,800 that is going to return the index 26th 17134 12:49:34,800 --> 12:49:38,096 and the index 26 then it's gonna be the 17135 12:49:38,096 --> 12:49:41,220 number 27 from our numbers list so now 17136 12:49:41,220 --> 12:49:43,680 if you run this you'll see 27 output in 17137 12:49:43,680 --> 12:49:46,160 the console 17138 12:49:47,640 --> 12:49:51,540 so get 27 and we did this one with one 17139 12:49:51,540 --> 12:49:53,640 line of code you can even remove this 17140 12:49:53,640 --> 12:49:56,276 and just have a 17141 12:49:56,276 --> 12:49:58,500 single expression function so we can put 17142 12:49:58,500 --> 12:49:59,700 here 17143 12:49:59,700 --> 12:50:02,460 equals 17144 12:50:02,460 --> 12:50:04,560 so just by that it will do the same 17145 12:50:04,560 --> 12:50:06,300 thing that we did previously with all 17146 12:50:06,300 --> 12:50:08,520 the code that I typed 17147 12:50:08,520 --> 12:50:11,160 so this is our discussion about binary 17148 12:50:11,160 --> 12:50:14,520 search and see you in the next video but 17149 12:50:14,520 --> 12:50:16,320 before I end the video I should say that 17150 12:50:16,320 --> 12:50:19,436 binary search only works with the order 17151 12:50:19,436 --> 12:50:21,900 collection of elements so your elements 17152 12:50:21,900 --> 12:50:24,240 need to be sorted in order to for the 17153 12:50:24,240 --> 12:50:27,596 binary search to work and in our case we 17154 12:50:27,596 --> 12:50:32,096 look we used integer numbers and it was 17155 12:50:32,096 --> 12:50:35,276 easy because we typed elements in a in a 17156 12:50:35,276 --> 12:50:36,900 sorted order 17157 12:50:36,900 --> 12:50:39,660 but uh if the elements are not sorted by 17158 12:50:39,660 --> 12:50:42,480 by research is not going to work also if 17159 12:50:42,480 --> 12:50:44,400 you have your own class so you have your 17160 12:50:44,400 --> 12:50:46,740 own objects and you search with using 17161 12:50:46,740 --> 12:50:51,000 one research uh through a list for a 17162 12:50:51,000 --> 12:50:54,480 specific element in that case your class 17163 12:50:54,480 --> 12:50:56,276 has to implement the comparable 17164 12:50:56,276 --> 12:51:01,320 interface or it has to to to to use the 17165 12:51:01,320 --> 12:51:03,000 comparator interface and I'm going to 17166 12:51:03,000 --> 12:51:04,500 show you how to do that in the next 17167 12:51:04,500 --> 12:51:06,900 video so see you in the next video 17168 12:51:06,900 --> 12:51:08,580 so now it's time to start a discussion 17169 12:51:08,580 --> 12:51:10,400 about genetics so what are genetics 17170 12:51:10,400 --> 12:51:13,200 genetics allows us to give to the 17171 12:51:13,200 --> 12:51:15,776 compiler some hints about what kind of 17172 12:51:15,776 --> 12:51:18,300 objects you are dealing with and this 17173 12:51:18,300 --> 12:51:20,400 helps the compiler to figure out some 17174 12:51:20,400 --> 12:51:23,820 errors at compile time and it also helps 17175 12:51:23,820 --> 12:51:26,400 us to write more consistent and better 17176 12:51:26,400 --> 12:51:29,820 code and we already use genetics when we 17177 12:51:29,820 --> 12:51:32,096 look at collections so every time you 17178 12:51:32,096 --> 12:51:34,500 use an array and you you specified in 17179 12:51:34,500 --> 12:51:38,096 the angle brackets an INT or a double or 17180 12:51:38,096 --> 12:51:42,180 a class that you created an object what 17181 12:51:42,180 --> 12:51:44,400 you actually did there is that you use 17182 12:51:44,400 --> 12:51:47,640 generic you set the compiler that that 17183 12:51:47,640 --> 12:51:50,340 is the kind of data that specific 17184 12:51:50,340 --> 12:51:52,916 collection is gonna store so if you have 17185 12:51:52,916 --> 12:51:56,040 something like this while let's say 17186 12:51:56,040 --> 12:51:58,320 numbers 17187 12:51:58,320 --> 12:52:01,020 and 17188 12:52:01,020 --> 12:52:03,720 we put equals is equals here and type 17189 12:52:03,720 --> 12:52:06,256 array of 17190 12:52:10,740 --> 12:52:15,079 here you can put angle brackets 17191 12:52:17,640 --> 12:52:19,860 and you can specify a type here let's 17192 12:52:19,860 --> 12:52:21,360 say in because 17193 12:52:21,360 --> 12:52:23,820 I'm going to put numbers here and now 17194 12:52:23,820 --> 12:52:27,360 this helps the compiler to figure out if 17195 12:52:27,360 --> 12:52:30,116 let's say we want to add here let's say 17196 12:52:30,116 --> 12:52:33,980 a string so if I type here numbers 17197 12:52:34,520 --> 12:52:37,256 equals let's say your 17198 12:52:37,256 --> 12:52:41,160 some text let's put in quotation marks 17199 12:52:41,160 --> 12:52:43,800 some text as you can see you have those 17200 12:52:43,800 --> 12:52:46,560 underlines red which says if you hover 17201 12:52:46,560 --> 12:52:47,880 over here 17202 12:52:47,880 --> 12:52:49,980 first it says that Val cannot be resent 17203 12:52:49,980 --> 12:52:52,560 so let's change this to VAR to get the 17204 12:52:52,560 --> 12:52:55,276 error that I want so let's put VAR here 17205 12:52:55,276 --> 12:52:58,916 so we get here type mismatch required 17206 12:52:58,916 --> 12:53:02,340 and found string so here we are using a 17207 12:53:02,340 --> 12:53:04,980 generic type and the generic type is an 17208 12:53:04,980 --> 12:53:08,520 integer and because we said explicitly 17209 12:53:08,520 --> 12:53:10,436 here that this array is only going to 17210 12:53:10,436 --> 12:53:13,680 store integers we cannot go down here 17211 12:53:13,680 --> 12:53:18,000 and assign some string but 17212 12:53:18,000 --> 12:53:20,040 as I said at the beginning 17213 12:53:20,040 --> 12:53:23,040 genetics are only a compile timing 17214 12:53:23,040 --> 12:53:24,660 feature they don't make it through the 17215 12:53:24,660 --> 12:53:26,640 runtime and again we're going to come 17216 12:53:26,640 --> 12:53:28,616 back to this later because this has some 17217 12:53:28,616 --> 12:53:30,180 implications 17218 12:53:30,180 --> 12:53:33,180 now let's uh delete this and let's 17219 12:53:33,180 --> 12:53:36,540 create our own generic class to see how 17220 12:53:36,540 --> 12:53:40,200 it works because here we're passing in 17221 12:53:40,200 --> 12:53:44,776 but if you click on this array of 17222 12:53:44,936 --> 12:53:48,540 you see that you don't have here any an 17223 12:53:48,540 --> 12:53:50,700 intest the type that we SP we 17224 12:53:50,700 --> 12:53:53,096 specifically decided to pass to this 17225 12:53:53,096 --> 12:53:56,180 array but if you look here inside this 17226 12:53:56,180 --> 12:53:59,400 library.kt and if you go to array here 17227 12:53:59,400 --> 12:54:01,320 you see you see that we have VAR Arc 17228 12:54:01,320 --> 12:54:03,416 elements and we have this t 17229 12:54:03,416 --> 12:54:07,436 and T is a genetic type and the specific 17230 12:54:07,436 --> 12:54:10,800 type that is going to be passed here is 17231 12:54:10,800 --> 12:54:13,140 the type that we pass when we create an 17232 12:54:13,140 --> 12:54:14,936 arrays when we want to store some 17233 12:54:14,936 --> 12:54:18,480 numbers or some strings and now now 17234 12:54:18,480 --> 12:54:21,000 let's see how we can create a similar 17235 12:54:21,000 --> 12:54:23,520 thing that we have here with our with 17236 12:54:23,520 --> 12:54:26,756 our own code so I'm gonna delete this so 17237 12:54:26,756 --> 12:54:29,096 again generics are there to 17238 12:54:29,096 --> 12:54:31,800 to to help the compiler to figure out 17239 12:54:31,800 --> 12:54:35,540 errors and it also they are there for 17240 12:54:35,540 --> 12:54:37,680 helping us to write better and 17241 12:54:37,680 --> 12:54:39,900 consistent calls so that you you don't 17242 12:54:39,900 --> 12:54:44,160 add let's say uh strings to to an array 17243 12:54:44,160 --> 12:54:47,936 which is meant to store only numbers so 17244 12:54:47,936 --> 12:54:50,220 first I'm going to create a class and 17245 12:54:50,220 --> 12:54:53,220 it's going to be an Open Class 17246 12:54:53,220 --> 12:54:56,580 and it's going to be called player 17247 12:54:56,580 --> 12:54:58,500 and it's going to have a property called 17248 12:54:58,500 --> 12:55:00,720 name so it's going to be a Val name 17249 12:55:00,720 --> 12:55:02,220 we're going to Define this in the 17250 12:55:02,220 --> 12:55:03,776 primary Constructor it's going to be a 17251 12:55:03,776 --> 12:55:05,096 string 17252 12:55:05,096 --> 12:55:08,360 now I'm gonna create 17253 12:55:08,580 --> 12:55:11,840 open class here 17254 12:55:14,640 --> 12:55:16,256 and down here I'm going to create two 17255 12:55:16,256 --> 12:55:18,660 classes first is going to be called 17256 12:55:18,660 --> 12:55:23,060 football player so football 17257 12:55:24,596 --> 12:55:26,096 player 17258 12:55:26,096 --> 12:55:28,500 and this is gonna have a name and it's 17259 12:55:28,500 --> 12:55:30,180 gonna be a property because you're gonna 17260 12:55:30,180 --> 12:55:34,080 gonna extend from our player class we're 17261 12:55:34,080 --> 12:55:35,580 gonna derive from our operator class I'm 17262 12:55:35,580 --> 12:55:37,680 gonna pass this property to the dot 17263 12:55:37,680 --> 12:55:41,040 parameter name to our property name from 17264 12:55:41,040 --> 12:55:43,380 the superclass so if we type here player 17265 12:55:43,380 --> 12:55:46,380 because we were in inheriting from it 17266 12:55:46,380 --> 12:55:49,200 and here first name 17267 12:55:49,200 --> 12:55:52,140 so you pass your name 17268 12:55:52,140 --> 12:55:54,060 next I'm going to create another class 17269 12:55:54,060 --> 12:55:56,400 called baseball player so we type here 17270 12:55:56,400 --> 12:55:58,616 baseball 17271 12:55:58,616 --> 12:56:00,900 player 17272 12:56:00,900 --> 12:56:03,256 and this is also going to have a name 17273 12:56:03,256 --> 12:56:06,660 also only a parameter because the 17274 12:56:06,660 --> 12:56:07,860 property is going to be ultimately 17275 12:56:07,860 --> 12:56:09,480 defined by the 17276 12:56:09,480 --> 12:56:11,400 superclass by the class from which we 17277 12:56:11,400 --> 12:56:13,320 are inheriting from so here we're gonna 17278 12:56:13,320 --> 12:56:15,540 inherit put the colon to inherit from 17279 12:56:15,540 --> 12:56:19,436 our player player class and here we pass 17280 12:56:19,436 --> 12:56:22,436 our name parameter that we defined here 17281 12:56:22,436 --> 12:56:24,180 we pass it here to the primary 17282 12:56:24,180 --> 12:56:26,880 Constructor of the base class from which 17283 12:56:26,880 --> 12:56:28,800 we are inheriting from 17284 12:56:28,800 --> 12:56:32,220 all right so now I'm gonna go up here 17285 12:56:32,220 --> 12:56:36,000 and now I'm gonna create a class called 17286 12:56:36,000 --> 12:56:37,200 team 17287 12:56:37,200 --> 12:56:39,596 and this class is going to represent a 17288 12:56:39,596 --> 12:56:41,160 team and this thing can be a football 17289 12:56:41,160 --> 12:56:43,740 team or a baseball team and here we're 17290 12:56:43,740 --> 12:56:45,300 going to create the class called team so 17291 12:56:45,300 --> 12:56:47,700 we type the keyword class I'm gonna call 17292 12:56:47,700 --> 12:56:48,840 it team 17293 12:56:48,840 --> 12:56:51,596 and now here we put angle brackets and 17294 12:56:51,596 --> 12:56:53,340 inside the angle brackets we're gonna 17295 12:56:53,340 --> 12:56:55,560 Define the generic type t 17296 12:56:55,560 --> 12:56:57,720 and you can use a different letter here 17297 12:56:57,720 --> 12:57:00,180 if you want but usually and generally 17298 12:57:00,180 --> 12:57:03,660 use the T because it stands for the for 17299 12:57:03,660 --> 12:57:05,160 type parameter 17300 12:57:05,160 --> 12:57:08,040 next to put parenthesis to define the 17301 12:57:08,040 --> 12:57:09,960 primary Constructor and for the primary 17302 12:57:09,960 --> 12:57:11,776 Constructor I'm going to define 17303 12:57:11,776 --> 12:57:14,580 a Val name and this is going to 17304 12:57:14,580 --> 12:57:16,380 represent the name of the theme and it's 17305 12:57:16,380 --> 12:57:18,060 going to be a string and next I'm going 17306 12:57:18,060 --> 12:57:20,460 to Define also valuable this is going to 17307 12:57:20,460 --> 12:57:22,980 be called players and it's going to be a 17308 12:57:22,980 --> 12:57:24,776 multiple list 17309 12:57:24,776 --> 12:57:28,256 of also of type T so we type here 17310 12:57:28,256 --> 12:57:30,480 Multiple List 17311 12:57:30,480 --> 12:57:32,700 and we put here t 17312 12:57:32,700 --> 12:57:34,080 right 17313 12:57:34,080 --> 12:57:36,480 now I'm going to put curly braces 17314 12:57:36,480 --> 12:57:38,040 because I'm going to define a function 17315 12:57:38,040 --> 12:57:41,640 inside our class team so here we type 17316 12:57:41,640 --> 12:57:44,460 fun and I'm going to call this function 17317 12:57:44,460 --> 12:57:45,540 add 17318 12:57:45,540 --> 12:57:47,400 players so this function is going to add 17319 12:57:47,400 --> 12:57:49,380 players to the team 17320 12:57:49,380 --> 12:57:51,540 we put parentheses and here we Define a 17321 12:57:51,540 --> 12:57:54,000 parameter called called player and it's 17322 12:57:54,000 --> 12:57:56,640 going to be of type T so it's going to 17323 12:57:56,640 --> 12:57:58,080 be 17324 12:57:58,080 --> 12:58:00,740 should put 17325 12:58:00,776 --> 12:58:02,640 a colon here 17326 12:58:02,640 --> 12:58:06,540 and we put curly braces 17327 12:58:06,540 --> 12:58:08,400 and this is going to receive a parameter 17328 12:58:08,400 --> 12:58:10,560 of type T and this type T that we Define 17329 12:58:10,560 --> 12:58:13,500 here and here and here is going to be 17330 12:58:13,500 --> 12:58:16,680 the type the specific type that we gonna 17331 12:58:16,680 --> 12:58:18,900 pass when we create an instance with our 17332 12:58:18,900 --> 12:58:21,480 class team so 17333 12:58:21,480 --> 12:58:24,000 first here we're gonna check if 17334 12:58:24,000 --> 12:58:26,276 the 17335 12:58:26,276 --> 12:58:27,960 players 17336 12:58:27,960 --> 12:58:31,800 so our list of players contains this 17337 12:58:31,800 --> 12:58:33,960 player that is passed our ad player 17338 12:58:33,960 --> 12:58:36,776 function because if or you already have 17339 12:58:36,776 --> 12:58:39,180 that player in our list We're Not Gonna 17340 12:58:39,180 --> 12:58:41,880 add it to our list so if player and to 17341 12:58:41,880 --> 12:58:44,160 check if that player is in the list we 17342 12:58:44,160 --> 12:58:48,300 call the contains function on our uh on 17343 12:58:48,300 --> 12:58:50,480 on our uh 17344 12:58:50,480 --> 12:58:54,300 Multiple List so here we we type players 17345 12:58:54,300 --> 12:58:58,500 that contains player the argument 17346 12:58:58,500 --> 12:59:01,800 and if that is true I'm gonna put curly 17347 12:59:01,800 --> 12:59:03,000 braces 17348 12:59:03,000 --> 12:59:04,980 and here we're gonna output something 17349 12:59:04,980 --> 12:59:06,840 because to the console so we type here 17350 12:59:06,840 --> 12:59:08,276 print line 17351 12:59:08,276 --> 12:59:12,480 quotation marks and we type player 17352 12:59:12,480 --> 12:59:16,080 colon and then we put dollar sign curly 17353 12:59:16,080 --> 12:59:17,460 braces 17354 12:59:17,460 --> 12:59:20,580 and here we need to do something which 17355 12:59:20,580 --> 12:59:24,596 is called casting and casting casting is 17356 12:59:24,596 --> 12:59:28,580 used when you want a variable to be 17357 12:59:28,580 --> 12:59:32,640 treated as a certain type of variable so 17358 12:59:32,640 --> 12:59:35,160 when you want a variable to be treated 17359 12:59:35,160 --> 12:59:37,800 by the component by the compiler as a 17360 12:59:37,800 --> 12:59:42,416 certain type of object or or a class so 17361 12:59:42,416 --> 12:59:45,596 we type here player 17362 12:59:45,596 --> 12:59:48,360 because let's let's let me show you why 17363 12:59:48,360 --> 12:59:50,700 we need to do that because if I type 17364 12:59:50,700 --> 12:59:53,580 here player now I want to get the name 17365 12:59:53,580 --> 12:59:56,936 and if I put here dot name as you can 17366 12:59:56,936 --> 13:00:00,116 see we don't have any name here because 17367 13:00:00,116 --> 13:00:03,300 because this type because this T and 17368 13:00:03,300 --> 13:00:06,000 this T that we have here is generic the 17369 13:00:06,000 --> 13:00:09,180 compiler has no idea if this T that is 17370 13:00:09,180 --> 13:00:11,220 going to be passed here so this piece so 17371 13:00:11,220 --> 13:00:12,660 if you pass here 17372 13:00:12,660 --> 13:00:15,360 a baseball player or football player it 17373 13:00:15,360 --> 13:00:18,540 has no idea if at this moment now if 17374 13:00:18,540 --> 13:00:21,116 that is gonna have uh is gonna have a 17375 13:00:21,116 --> 13:00:24,180 name property on it this is why when you 17376 13:00:24,180 --> 13:00:25,800 type here that's name we don't get 17377 13:00:25,800 --> 13:00:29,040 nothing and to solve that we can say to 17378 13:00:29,040 --> 13:00:31,740 the compiler for them to the compiler to 17379 13:00:31,740 --> 13:00:34,980 treat this parameter here so this is 17380 13:00:34,980 --> 13:00:36,840 going to be a value pass to this 17381 13:00:36,840 --> 13:00:41,520 parameter as a player and in that sense 17382 13:00:41,520 --> 13:00:43,320 in that in that way you can get access 17383 13:00:43,320 --> 13:00:45,900 to the name property so we type here 17384 13:00:45,900 --> 13:00:47,400 player and 17385 13:00:47,400 --> 13:00:51,540 to cast to cast a variable to a certain 17386 13:00:51,540 --> 13:00:54,300 type we put as so we put 17387 13:00:54,300 --> 13:00:58,560 the keyword as and the type player 17388 13:00:58,560 --> 13:01:01,500 and we put this in parentheses so we 17389 13:01:01,500 --> 13:01:04,140 surround our expression in parentheses 17390 13:01:04,140 --> 13:01:07,580 inside the curly braces 17391 13:01:08,040 --> 13:01:11,820 and now here at the end of of the of the 17392 13:01:11,820 --> 13:01:13,916 end of the enclosing parentheses you put 17393 13:01:13,916 --> 13:01:16,200 that and now we have access to name 17394 13:01:16,200 --> 13:01:19,080 because we settle the compiler hey treat 17395 13:01:19,080 --> 13:01:21,360 this player variable I don't know 17396 13:01:21,360 --> 13:01:23,700 because I don't know if this is going to 17397 13:01:23,700 --> 13:01:27,180 be it's going to be a player 17398 13:01:27,180 --> 13:01:29,460 a player object passed here or a 17399 13:01:29,460 --> 13:01:32,160 baseball player or or a football player 17400 13:01:32,160 --> 13:01:34,800 treat this player variable that is 17401 13:01:34,800 --> 13:01:37,020 passed here as because here we have 17402 13:01:37,020 --> 13:01:39,060 teeth so we don't know what it is three 17403 13:01:39,060 --> 13:01:41,276 display to display a variable as a 17404 13:01:41,276 --> 13:01:43,500 player so you can use the name of a 17405 13:01:43,500 --> 13:01:48,596 variable and we're gonna say is already 17406 13:01:48,596 --> 13:01:52,680 in the TM and we can put here 17407 13:01:52,680 --> 13:01:55,080 the team and we can type here dollar 17408 13:01:55,080 --> 13:02:00,480 sign this Dot name so the name of the TM 17409 13:02:00,480 --> 13:02:04,560 else if if our player is not in the team 17410 13:02:04,560 --> 13:02:06,720 we put else 17411 13:02:06,720 --> 13:02:08,820 we're going to press enter to add the 17412 13:02:08,820 --> 13:02:11,700 right curly brace we're gonna add this 17413 13:02:11,700 --> 13:02:14,040 player to the list because if the if 17414 13:02:14,040 --> 13:02:16,276 fails that means that the player is not 17415 13:02:16,276 --> 13:02:19,140 present in our list so we type here 17416 13:02:19,140 --> 13:02:21,060 players 17417 13:02:21,060 --> 13:02:22,616 our 17418 13:02:22,616 --> 13:02:26,820 Multiple List dot add this player that 17419 13:02:26,820 --> 13:02:30,540 is passed player and we're also going to 17420 13:02:30,540 --> 13:02:32,160 output something to go to the console so 17421 13:02:32,160 --> 13:02:34,320 you add here print line 17422 13:02:34,320 --> 13:02:36,300 and uh 17423 13:02:36,300 --> 13:02:38,880 I'm gonna copy this because I'm just 17424 13:02:38,880 --> 13:02:41,756 gonna change the text a little bit 17425 13:02:41,756 --> 13:02:44,160 so let's delete this 17426 13:02:44,160 --> 13:02:49,820 player and so here is we're gonna say 17427 13:02:50,040 --> 13:02:52,140 instead of it's already 17428 13:02:52,140 --> 13:02:53,880 I'm gonna say it was 17429 13:02:53,880 --> 13:02:55,980 added 17430 13:02:55,980 --> 13:02:58,820 in the team 17431 13:03:01,020 --> 13:03:04,080 now let's create some objects with those 17432 13:03:04,080 --> 13:03:06,240 classes that we have here so first I'm 17433 13:03:06,240 --> 13:03:09,596 going to create some some players so I'm 17434 13:03:09,596 --> 13:03:10,500 going to create first the football 17435 13:03:10,500 --> 13:03:12,660 player so type here 17436 13:03:12,660 --> 13:03:16,560 Val for the ball player equals football 17437 13:03:16,560 --> 13:03:18,840 player 17438 13:03:18,840 --> 13:03:20,700 and 17439 13:03:20,700 --> 13:03:22,800 for the name I'm going to use a generic 17440 13:03:22,800 --> 13:03:26,720 name I'm gonna type just football 17441 13:03:36,240 --> 13:03:39,079 player 17442 13:03:39,180 --> 13:03:41,160 one 17443 13:03:41,160 --> 13:03:43,380 and I'm gonna press Ctrl D to create 17444 13:03:43,380 --> 13:03:46,020 another football player so football 17445 13:03:46,020 --> 13:03:49,820 player two is going to be called 17446 13:03:50,640 --> 13:03:52,320 and I'm gonna create two baseball 17447 13:03:52,320 --> 13:03:54,900 players so we type here Val 17448 13:03:54,900 --> 13:03:57,900 baseball player equals 17449 13:03:57,900 --> 13:04:00,960 baseball player 17450 13:04:00,960 --> 13:04:03,720 and we type here 17451 13:04:03,720 --> 13:04:06,256 baseball 17452 13:04:06,960 --> 13:04:10,616 player one and I'm gonna call Press Ctrl 17453 13:04:10,616 --> 13:04:14,300 D here also I'm gonna 17454 13:04:15,180 --> 13:04:17,880 call this baseball player 2 and I'm 17455 13:04:17,880 --> 13:04:21,079 gonna change this to 2. 17456 13:04:21,840 --> 13:04:24,000 now I'm going to create an instance of 17457 13:04:24,000 --> 13:04:26,220 our team class so we cut we type here 17458 13:04:26,220 --> 13:04:29,040 Vault team and this is going to be a 17459 13:04:29,040 --> 13:04:31,140 team of football players so we type Val 17460 13:04:31,140 --> 13:04:32,400 team 17461 13:04:32,400 --> 13:04:33,776 equals 17462 13:04:33,776 --> 13:04:37,080 and hero type team 17463 13:04:37,080 --> 13:04:39,416 and we put now because as you can see 17464 13:04:39,416 --> 13:04:42,596 it's expecting a type here and the type 17465 13:04:42,596 --> 13:04:44,220 is going to be 17466 13:04:44,220 --> 13:04:47,340 football player so it's gonna be a team 17467 13:04:47,340 --> 13:04:50,340 with what it's gonna be a team with the 17468 13:04:50,340 --> 13:04:52,980 football players and you're gonna Define 17469 13:04:52,980 --> 13:04:54,116 a name 17470 13:04:54,116 --> 13:04:58,040 also generic name football 17471 13:04:58,680 --> 13:05:01,140 team is going to be called 17472 13:05:01,140 --> 13:05:03,060 and they also need to pass an array 17473 13:05:03,060 --> 13:05:04,680 there 17474 13:05:04,680 --> 13:05:07,200 and for this array I'm gonna type just 17475 13:05:07,200 --> 13:05:08,820 uh 17476 13:05:08,820 --> 13:05:12,616 mod multiple array of 17477 13:05:13,560 --> 13:05:15,900 we can't 17478 13:05:15,900 --> 13:05:18,900 foreign 17479 13:05:22,380 --> 13:05:25,020 list of 17480 13:05:25,020 --> 13:05:28,820 and here I'm gonna pass our 17481 13:05:29,880 --> 13:05:32,580 football player I'm gonna pass only our 17482 13:05:32,580 --> 13:05:35,900 first football player here 17483 13:05:37,800 --> 13:05:41,700 now I'm going to create another team Val 17484 13:05:41,700 --> 13:05:45,240 team uh let's actually call this first 17485 13:05:45,240 --> 13:05:50,160 team football team so let's type here 17486 13:05:50,160 --> 13:05:54,080 food bowl 17487 13:05:58,560 --> 13:06:00,000 football team 17488 13:06:00,000 --> 13:06:03,616 now if you run this code 17489 13:06:10,256 --> 13:06:12,660 we don't get it on any output because 17490 13:06:12,660 --> 13:06:15,300 our ad player's function wasn't called 17491 13:06:15,300 --> 13:06:17,640 but if I 17492 13:06:17,640 --> 13:06:21,320 type here a football team 17493 13:06:26,040 --> 13:06:26,756 foreign 17494 13:06:26,756 --> 13:06:30,060 actually here football team 17495 13:06:30,060 --> 13:06:32,220 that 17496 13:06:32,220 --> 13:06:34,560 add players and I'm gonna add football 17497 13:06:34,560 --> 13:06:37,340 player too 17498 13:06:39,240 --> 13:06:42,615 now if I run this code 17499 13:06:47,340 --> 13:06:50,880 we get football player two was actually 17500 13:06:50,880 --> 13:06:54,255 here should be added 17501 13:06:56,040 --> 13:06:58,616 football player 2 was added in the team 17502 13:06:58,616 --> 13:07:01,200 football team so our code works well and 17503 13:07:01,200 --> 13:07:04,560 we use generic types for our classes 17504 13:07:04,560 --> 13:07:07,980 here we specified only the T and this T 17505 13:07:07,980 --> 13:07:10,080 that we have here was replaced with 17506 13:07:10,080 --> 13:07:13,680 football player that we Define here and 17507 13:07:13,680 --> 13:07:14,520 um 17508 13:07:14,520 --> 13:07:16,740 uh we're gonna do the same thing we're 17509 13:07:16,740 --> 13:07:18,416 gonna create another theme for the 17510 13:07:18,416 --> 13:07:21,540 baseball player and we're also going to 17511 13:07:21,540 --> 13:07:22,500 look at 17512 13:07:22,500 --> 13:07:25,616 how we can restrict the types that are 17513 13:07:25,616 --> 13:07:27,776 going to be passed to our generic type 17514 13:07:27,776 --> 13:07:29,040 here 17515 13:07:29,040 --> 13:07:30,540 so now I'm going to create a baseball 17516 13:07:30,540 --> 13:07:32,936 team so I'm going to type here Val 17517 13:07:32,936 --> 13:07:36,020 baseball team 17518 13:07:37,500 --> 13:07:41,540 put equals then you put Here theme 17519 13:07:44,520 --> 13:07:47,160 and here we type for the generic type T 17520 13:07:47,160 --> 13:07:49,740 baseball player because this is gonna 17521 13:07:49,740 --> 13:07:52,560 store baseball players and we Define a 17522 13:07:52,560 --> 13:07:53,756 name here the name is going to be 17523 13:07:53,756 --> 13:07:57,436 baseball team or generic name 17524 13:07:58,340 --> 13:08:02,820 and uh also you need to type to pass a 17525 13:08:02,820 --> 13:08:06,116 multiple list so type Multiple List 17526 13:08:06,116 --> 13:08:11,480 and we pass here a baseball player 17527 13:08:13,200 --> 13:08:15,540 but as you can see 17528 13:08:15,540 --> 13:08:18,116 here for the type 17529 13:08:18,116 --> 13:08:20,820 the type here is great oh that is 17530 13:08:20,820 --> 13:08:21,900 because 17531 13:08:21,900 --> 13:08:25,616 here we pass the football player 17532 13:08:25,616 --> 13:08:28,140 uh object that we create then here we 17533 13:08:28,140 --> 13:08:29,700 pass the baseball player 17534 13:08:29,700 --> 13:08:32,580 because kotlin has Type inference it can 17535 13:08:32,580 --> 13:08:36,116 infer the type as being a team of 17536 13:08:36,116 --> 13:08:37,800 football players so we don't need to put 17537 13:08:37,800 --> 13:08:40,560 here for the player we can delete this 17538 13:08:40,560 --> 13:08:43,880 you can delete all of this 17539 13:08:44,400 --> 13:08:46,256 and now our code still works and you can 17540 13:08:46,256 --> 13:08:48,060 see we have this type here which says 17541 13:08:48,060 --> 13:08:50,756 team football player so it's a team of 17542 13:08:50,756 --> 13:08:53,580 football players and it's uh inferred 17543 13:08:53,580 --> 13:08:55,740 it's inferred automatically by quoting 17544 13:08:55,740 --> 13:08:58,320 for us and you can do the same thing if 17545 13:08:58,320 --> 13:09:00,480 here for a baseball player so I'm gonna 17546 13:09:00,480 --> 13:09:01,860 delete this 17547 13:09:01,860 --> 13:09:03,840 as you can see now we also have the hint 17548 13:09:03,840 --> 13:09:07,380 team baseball player now let's add to 17549 13:09:07,380 --> 13:09:09,416 the baseball player 17550 13:09:09,416 --> 13:09:12,660 let's call the baseball player function 17551 13:09:12,660 --> 13:09:15,300 let's put here a 17552 13:09:15,300 --> 13:09:19,020 so we type here base bullet 17553 13:09:19,020 --> 13:09:22,140 team let's call the function dot add 17554 13:09:22,140 --> 13:09:25,500 players and hero type baseball player 2 17555 13:09:25,500 --> 13:09:28,320 because first one was added there 17556 13:09:28,320 --> 13:09:30,776 so now if you run the squad 17557 13:09:30,776 --> 13:09:35,115 not debug so run this code 17558 13:09:37,500 --> 13:09:40,580 let's increase this 17559 13:09:41,340 --> 13:09:44,040 we get let's actually decrease this so 17560 13:09:44,040 --> 13:09:46,860 let's put here we get player football 17561 13:09:46,860 --> 13:09:49,140 player 2 was added to the team football 17562 13:09:49,140 --> 13:09:52,860 team so we have this code here 17563 13:09:52,860 --> 13:09:56,220 which is called and it says that the 17564 13:09:56,220 --> 13:09:58,560 the football player was added to the 17565 13:09:58,560 --> 13:10:00,000 team and the name of the football team 17566 13:10:00,000 --> 13:10:02,400 and then we have player and we have 17567 13:10:02,400 --> 13:10:05,276 baseball player player two 17568 13:10:05,276 --> 13:10:06,840 so this 17569 13:10:06,840 --> 13:10:08,880 this one 17570 13:10:08,880 --> 13:10:10,500 that we have here 17571 13:10:10,500 --> 13:10:12,416 that we pass here 17572 13:10:12,416 --> 13:10:14,640 baseball team that was this to the 17573 13:10:14,640 --> 13:10:16,380 baseball team as you can see it says 17574 13:10:16,380 --> 13:10:18,540 player baseball player 2 was added to 17575 13:10:18,540 --> 13:10:19,860 the baseball team 17576 13:10:19,860 --> 13:10:22,500 and what generics helps out to do 17577 13:10:22,500 --> 13:10:24,840 because I said they can help the 17578 13:10:24,840 --> 13:10:27,360 compiler to figure out errors if you try 17579 13:10:27,360 --> 13:10:28,916 to 17580 13:10:28,916 --> 13:10:32,700 pass here baseball TM Dot 17581 13:10:32,700 --> 13:10:35,700 add football player so we type here 17582 13:10:35,700 --> 13:10:37,616 football player 17583 13:10:37,616 --> 13:10:40,560 now you get an error because 17584 13:10:40,560 --> 13:10:45,660 the generic type enforces the type on 17585 13:10:45,660 --> 13:10:48,116 the the 17586 13:10:48,116 --> 13:10:50,340 on the class because here we said 17587 13:10:50,340 --> 13:10:53,220 explicitly that this is because this was 17588 13:10:53,220 --> 13:10:55,020 inferred to be as a baseball player if 17589 13:10:55,020 --> 13:10:56,936 you cannot pass here a football player 17590 13:10:56,936 --> 13:11:00,240 in our baseball team because this is in 17591 13:11:00,240 --> 13:11:02,040 first because you said Siri here 17592 13:11:02,040 --> 13:11:05,040 explicitly explicitly because first 17593 13:11:05,040 --> 13:11:07,740 because this was inferred from here so 17594 13:11:07,740 --> 13:11:10,020 we have team baseball player so we 17595 13:11:10,020 --> 13:11:11,936 cannot add here a football brain inside 17596 13:11:11,936 --> 13:11:13,860 our team football player and this is 17597 13:11:13,860 --> 13:11:15,596 very very 17598 13:11:15,596 --> 13:11:18,180 powerful for us because in this way we 17599 13:11:18,180 --> 13:11:21,000 don't do things which doesn't make sense 17600 13:11:21,000 --> 13:11:24,416 and the the same is true about our 17601 13:11:24,416 --> 13:11:28,560 football team so if on our football team 17602 13:11:28,560 --> 13:11:30,240 so if enough of working with a football 17603 13:11:30,240 --> 13:11:32,220 team 17604 13:11:32,220 --> 13:11:34,560 dot add 17605 13:11:34,560 --> 13:11:38,479 and here to put baseball player 17606 13:11:39,180 --> 13:11:42,360 we get an error because we specified we 17607 13:11:42,360 --> 13:11:45,060 specified that because the generic type 17608 13:11:45,060 --> 13:11:46,740 was 17609 13:11:46,740 --> 13:11:49,380 in fair to be a football player so we 17610 13:11:49,380 --> 13:11:51,300 cannot put a baseball player in our team 17611 13:11:51,300 --> 13:11:54,240 of football players so this is what 17612 13:11:54,240 --> 13:11:57,596 generics is doing is figure out those 17613 13:11:57,596 --> 13:12:00,000 errors for us at compile time and this 17614 13:12:00,000 --> 13:12:02,340 is very powerful because we cannot we 17615 13:12:02,340 --> 13:12:04,740 can run our code and we don't get in 17616 13:12:04,740 --> 13:12:06,616 those some 17617 13:12:06,616 --> 13:12:09,840 scenarios where we have a bay of 17618 13:12:09,840 --> 13:12:12,300 baseball player added to a baseball team 17619 13:12:12,300 --> 13:12:15,840 or maybe other examples there so I'm 17620 13:12:15,840 --> 13:12:17,700 gonna delete this 17621 13:12:17,700 --> 13:12:19,916 next we're gonna look at upper bounds 17622 13:12:19,916 --> 13:12:21,480 because 17623 13:12:21,480 --> 13:12:25,500 our upper bounds basically allows us to 17624 13:12:25,500 --> 13:12:29,040 restrict what kind of type is passed to 17625 13:12:29,040 --> 13:12:31,400 our generic type here so 17626 13:12:31,400 --> 13:12:34,256 and particularly gonna see how this is 17627 13:12:34,256 --> 13:12:36,000 gonna affect this code that we use here 17628 13:12:36,000 --> 13:12:41,040 to cast so to to add an upper bound so 17629 13:12:41,040 --> 13:12:43,680 to restrict what kind of type is passed 17630 13:12:43,680 --> 13:12:46,140 to this type we can put 17631 13:12:46,140 --> 13:12:51,060 colon so extends and here you can we can 17632 13:12:51,060 --> 13:12:52,860 type player 17633 13:12:52,860 --> 13:12:56,520 so what we are saying here is that 17634 13:12:56,520 --> 13:12:58,380 every 17635 13:12:58,380 --> 13:13:00,416 type that is going to be passed here 17636 13:13:00,416 --> 13:13:03,060 when you create an instance like 17637 13:13:03,060 --> 13:13:07,020 we did here so but only that this was in 17638 13:13:07,020 --> 13:13:10,020 Fair here this type that is going to be 17639 13:13:10,020 --> 13:13:13,320 passed here needs needs to inherit from 17640 13:13:13,320 --> 13:13:16,256 the player class otherwise this type is 17641 13:13:16,256 --> 13:13:19,200 not going to work so because of that 17642 13:13:19,200 --> 13:13:22,616 because the compiler now knows that and 17643 13:13:22,616 --> 13:13:25,256 enforces this that this type that is 17644 13:13:25,256 --> 13:13:27,360 going to be passed here is is going to 17645 13:13:27,360 --> 13:13:29,900 be is going to inherit from the player 17646 13:13:29,900 --> 13:13:34,380 class we we can't delete this cast 17647 13:13:34,380 --> 13:13:37,616 because now it knows that that type is 17648 13:13:37,616 --> 13:13:40,080 gonna have uh it's gonna be it's gonna 17649 13:13:40,080 --> 13:13:41,820 inherit from the player class so it's 17650 13:13:41,820 --> 13:13:43,916 gonna have the property name so if I 17651 13:13:43,916 --> 13:13:46,399 delete this 17652 13:13:46,680 --> 13:13:49,800 and if I delete this so let's put name 17653 13:13:49,800 --> 13:13:52,040 here 17654 13:13:53,400 --> 13:13:56,000 now 17655 13:13:57,116 --> 13:14:00,500 so let's delete these two 17656 13:14:02,936 --> 13:14:04,680 should be player here 17657 13:14:04,680 --> 13:14:06,660 player 17658 13:14:06,660 --> 13:14:08,700 so now we have player the name because 17659 13:14:08,700 --> 13:14:11,160 it knows that this type so this type 17660 13:14:11,160 --> 13:14:12,660 that is going to be passed series is 17661 13:14:12,660 --> 13:14:14,756 going to inherit from the player cards 17662 13:14:14,756 --> 13:14:16,860 from the player class so it's gonna have 17663 13:14:16,860 --> 13:14:19,080 the property name so you don't need to 17664 13:14:19,080 --> 13:14:24,360 do this the weird this word cast is 17665 13:14:24,360 --> 13:14:27,720 overcasting so I'm gonna delete this 17666 13:14:27,720 --> 13:14:29,820 and now as you can see we don't have uh 17667 13:14:29,820 --> 13:14:32,276 any arrows so now we have the name here 17668 13:14:32,276 --> 13:14:34,800 because now it knows that whatever type 17669 13:14:34,800 --> 13:14:37,500 you pass here it has to inherit from the 17670 13:14:37,500 --> 13:14:40,680 player class so if I go down here and I 17671 13:14:40,680 --> 13:14:43,160 create another class called gamer 17672 13:14:43,160 --> 13:14:46,520 player so 17673 13:14:46,680 --> 13:14:50,000 we type here class 17674 13:14:50,220 --> 13:14:54,000 let's call it games player 17675 13:14:54,000 --> 13:14:57,180 and it's gonna have a Val 17676 13:14:57,180 --> 13:14:59,276 is going to be 17677 13:14:59,276 --> 13:15:01,916 actually let's define a parameter only 17678 13:15:01,916 --> 13:15:03,180 name 17679 13:15:03,180 --> 13:15:05,400 is going to be a string 17680 13:15:05,400 --> 13:15:08,640 and we will not inherit from our player 17681 13:15:08,640 --> 13:15:12,116 class let's see what happens so 17682 13:15:12,116 --> 13:15:14,340 if I um 17683 13:15:14,340 --> 13:15:17,520 if I uh create here another theme let's 17684 13:15:17,520 --> 13:15:19,700 call it Val 17685 13:15:19,700 --> 13:15:25,020 let's call it the game Steam 17686 13:15:25,020 --> 13:15:27,900 so I'm gonna 17687 13:15:27,900 --> 13:15:32,040 type here team and we're gonna specify 17688 13:15:32,040 --> 13:15:33,000 here 17689 13:15:33,000 --> 13:15:35,880 games player 17690 13:15:35,880 --> 13:15:38,160 now if if I put now the primary 17691 13:15:38,160 --> 13:15:40,020 Constructor here here we need to define 17692 13:15:40,020 --> 13:15:42,560 a name 17693 13:15:42,596 --> 13:15:44,936 let's call it 17694 13:15:44,936 --> 13:15:46,500 games 17695 13:15:46,500 --> 13:15:48,000 team 17696 13:15:48,000 --> 13:15:50,160 and 17697 13:15:50,160 --> 13:15:53,960 multiple list of 17698 13:15:56,580 --> 13:15:58,616 now 17699 13:15:58,616 --> 13:16:00,596 here we have an error and if you look it 17700 13:16:00,596 --> 13:16:01,520 says 17701 13:16:01,520 --> 13:16:05,820 expected player found games player and 17702 13:16:05,820 --> 13:16:09,416 this happens because our games player 17703 13:16:09,416 --> 13:16:11,820 that we have here is not inheriting from 17704 13:16:11,820 --> 13:16:14,040 the player class and because we said 17705 13:16:14,040 --> 13:16:16,860 here that the upper bound is this T that 17706 13:16:16,860 --> 13:16:19,740 is passed here so this T is this here 17707 13:16:19,740 --> 13:16:23,340 has to inherit from the player class 17708 13:16:23,340 --> 13:16:26,936 otherwise if you if you don't uh inherit 17709 13:16:26,936 --> 13:16:29,276 from the player class it will not work 17710 13:16:29,276 --> 13:16:32,340 because you said here that it it must 17711 13:16:32,340 --> 13:16:35,160 inherit from the player class now if I 17712 13:16:35,160 --> 13:16:36,660 change this to 17713 13:16:36,660 --> 13:16:38,820 if I change this down here to inherit 17714 13:16:38,820 --> 13:16:40,380 from the player class so it I hear 17715 13:16:40,380 --> 13:16:42,860 player 17716 13:16:43,680 --> 13:16:46,380 we type here name 17717 13:16:46,380 --> 13:16:48,540 now as you can see the error disappears 17718 13:16:48,540 --> 13:16:51,596 because now it's respecting the upper 17719 13:16:51,596 --> 13:16:54,776 bound that we set we set up here which 17720 13:16:54,776 --> 13:16:56,400 is T 17721 13:16:56,400 --> 13:16:59,340 meaning games player has to inherit from 17722 13:16:59,340 --> 13:17:01,860 the player class and down here it's 17723 13:17:01,860 --> 13:17:05,040 inheriting so this is our discussion 17724 13:17:05,040 --> 13:17:08,700 about how we can use generics and how we 17725 13:17:08,700 --> 13:17:10,680 can use how you can Define upper bounds 17726 13:17:10,680 --> 13:17:13,200 and in the next videos you're gonna look 17727 13:17:13,200 --> 13:17:13,980 at 17728 13:17:13,980 --> 13:17:14,820 um 17729 13:17:14,820 --> 13:17:19,140 type erasures and you're going to lose 17730 13:17:19,140 --> 13:17:21,360 what reified 17731 13:17:21,360 --> 13:17:24,360 a keyword and how you can inline our 17732 13:17:24,360 --> 13:17:26,220 function with because you need to inline 17733 13:17:26,220 --> 13:17:29,096 your function when the use refine the 17734 13:17:29,096 --> 13:17:32,400 keyword so see you in the next video 17735 13:17:32,400 --> 13:17:34,680 well actually before I end the video I 17736 13:17:34,680 --> 13:17:36,960 should say why this uh genetics is 17737 13:17:36,960 --> 13:17:38,640 important why 17738 13:17:38,640 --> 13:17:42,240 because if you think about because here 17739 13:17:42,240 --> 13:17:44,460 you can type we can define a generic 17740 13:17:44,460 --> 13:17:46,740 type we don't need to create 17741 13:17:46,740 --> 13:17:51,180 um a separate class for for every team 17742 13:17:51,180 --> 13:17:54,840 that that we want to use so we don't 17743 13:17:54,840 --> 13:17:57,116 need to create a football team we don't 17744 13:17:57,116 --> 13:18:00,060 need to create a separate baseball team 17745 13:18:00,060 --> 13:18:02,300 we don't need to create a 17746 13:18:02,300 --> 13:18:07,500 separate games team we we just create a 17747 13:18:07,500 --> 13:18:10,020 team we Define the generic type T and 17748 13:18:10,020 --> 13:18:14,756 here we pass our we pass our 17749 13:18:14,756 --> 13:18:17,700 our type and in this way we can use only 17750 13:18:17,700 --> 13:18:18,960 one class 17751 13:18:18,960 --> 13:18:22,500 and we can we can have all of those uh 17752 13:18:22,500 --> 13:18:26,640 those uh baseball team we can have the 17753 13:18:26,640 --> 13:18:28,916 football team we can have just by having 17754 13:18:28,916 --> 13:18:31,200 the the type here which is generic 17755 13:18:31,200 --> 13:18:33,596 otherwise we'll have to create separate 17756 13:18:33,596 --> 13:18:35,660 classes for each of those 17757 13:18:35,660 --> 13:18:38,096 teams so 17758 13:18:38,096 --> 13:18:39,776 see you in the next video and next video 17759 13:18:39,776 --> 13:18:41,160 as I said we're gonna look at type 17760 13:18:41,160 --> 13:18:44,700 erasers and refine the verified the 17761 13:18:44,700 --> 13:18:48,300 keyword and the inline functions and in 17762 13:18:48,300 --> 13:18:50,040 the next videos we're going to look also 17763 13:18:50,040 --> 13:18:52,560 at covariance and counter variance so 17764 13:18:52,560 --> 13:18:55,399 see in the next video919204

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.