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

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal) Download
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

line of code we have one single

expression we just return some text or

function we don't have multiple

statements in our function we can write

this as a single expression function

and you can remove the calibration you

can write this as a single expression

body like we did in a previous video so

you can put it just equals and you can

remove the

type here also so if you run this code

now I'm gonna see it to the output for

the parameter message some text which is

the value which is returned from this

function from the single body expression

function right

so see in the next video

all right now it's time to look at the

VAR Arc keyword and how we can use the

water keyword with functions but first

let's declare the function below of our

enclosing

calibrase of our main function so I'm

going to type here the

fun keyword to declare a function

I'm going to call it sum because it's

going to return the sum of all the

parameter that we're going to Define so

I'm going to Define here some parameters

for it's going to be called a it's going

to be of type int

a single second is going to be called B

also in

third is going to be called C

also an end

the final one is going to be called the

alternate

and this this function is going to

return the sum of all of those

parameters so we need to Define here the

return type

which is going to be an hint

a whole number and now we can put the

curly braces and here we type return

a

plus b

plus c

Plus

d

and this is going to return the sum of

all the parameters that we Define here

or the values that is going to be passed

as arguments to this function and you

can write this as a single expression

function if you want so you can write

this uh like this if you want

this will have the same effect

but I will I will write it with the

curly braces because it will make things

more clear so let's undo this

right now let's call this function here

sum

let's press Ctrl alt L to format the

code and let's pass some values here so

I'm going to type here five

six seven

and

10.

all right if you press shift Ctrl p on

this you're gonna see that the type of

this function let's press again

the type of this function is an ins so

this function returns an integer so it's

it's an expression that means that we

can put this instead of putting storing

um

uh the return value in a variable we can

put this direction a printerland

function so you can type here print

area

and we can put the function inside here

because that function is of type in so

it's going to return

the sum of all the arguments that we

type there

so now if you run this code

we got we get 28 because 5 plus 6 Plus

7.10 is 28. now if I want to pass

another parameter here let's say that I

want to pass another number here I can't

because I am limited by the number of

parameters that we Define here of course

that I can Define another parameter here

but what if I want to pass 10 numbers

for 15 numbers or 100 numbers in that

case it will be very tedious to write

all of those parameters and and to solve

that you need to use what is called a

varg varag keyword and Avada keyword

allows us to pass more than one

parameter actually more than one

argument to our

parameters so to declare a Vari keyword

we type here

we type here far ARG so this is the

keyword varag it stands for variable

arguments

now we need to define a name for this

bar Arc I'm going to call it numbers

and we also need to specify a type what

kind of values this varag is going to

store

now this return keyword is this

variables here are red because they

don't exist

now if I want to

but first let's say that I want to type

here another parameter now now you see

that we have no problem so I can type

here whatever

number of numbers I want and there is no

problem as you can see now

to to

sum the values which are stored in these

viral numbers we need to use something

which we didn't cover yet but we have a

separate section which I show you how to

we need to explain this thing that I'm

going to use now and that is Loops

because now we need to Loop through our

numbers and to add our numbers to a

variable so we need to use what is

called a for Loop so to do that first we

declare a variable

I'm gonna call it um

let's say uh result

I'm gonna assign a value of zero

now to use a for Loop we Type 4

and again I'm gonna go into more detail

about this in uh the separate section

but now we need to use this because

there's not another way to to sum up the

values inside the varag numbers

and here we need to define a name for

our variable so it's going to be called

number

and now we use the keyword in

and now we're referring the

parameter the varag parameter which is

defined up called numbers

all right

now we here you can put the curly braces

and inside the cutter blazes we're going

to type

numbers

result

and now we're going to use that

abbreviation which is plus n equals

plus equals

number n

now here gonna return

the result return

result

all right so let's explain what we have

here so first we declare a variable we

assign a value of 0 to it so it's just

type int

next what this for Loop is doing so this

for Loop is doing what is doing is

executing

um a line of code multiple times so it

executing is executing uh uh what's

inside the curly braces until the it

finishes to it finishes the to Loop

through all these numbers which are

stored in the varark number so it first

assigns uh the first the first number or

number here

and it loops and it has that it adds

that number to a result then it does

that again and again and again until it

finishes so then it reaches uh the final

number five and in that case also gonna

sign

from our numbers uh the number five to

our number and then that number is going

to be added to our result variable and

when it finishes uh that looping when it

finishes that thing is gonna return the

result it's gonna return all the values

which are which were been added to our

result variable

so now if you print this

now I get 85 and probably this is the

result of this calculation so this is

how you can use varag numbers what are

the keyword to store more than one

single more than a limited number of

parameters defined

bio and another short shortcut to to

Loop through this is using a for each

Loop so we can do something like this

but this time let's say that I want I

don't want to return something

so let's see that I delete this

and I I can type here actually numbers

four

dot here to put that

four

each

and here we type print Ln

again this is a more complex subject

which is going to be covered in the next

videos so here we type print Ln

and we type it

all right now let's uh

let's call our sum

let's define some numbers here let's say

that I type here

one

four

all right so if you run this code

now you can see that every number in

that

that we defined here is printed one by

one so this number this for each which

is also a loop it goes through all of

these numbers it and it prints them one

by one and again we're gonna look at uh

for the for Loop and the four each in

the next videos but I had no other way

to show you this example without using

them but if you feel confused by for the

for each or the for Loop don't worry we

have a separate

section which in which you talk about in

detail about this for now we need to

only the only thing you need to know is

that you can

pass an indefinite number of parameters

of actually you can pass it an

indefinite number of arguments to our

varag numbers because this allows us to

to power to to pass uh

an indefinite number of values so here

you see that we have one four five six

seven eight nine five seven and six

numbers that we have here so this for

each is going through all of them and it

prints

them one by one so

see you in the next video

all right now it's time to start our

discussion about loops but first let's

create a new project

I'm going to call it loops

select IntelliJ click on next and finish

right a project is created let's delete

the code inside the color braces

let's hide the project pane

and let's start by explaining what Loops

are and why you need to use them

so Loops allows us to execute a piece of

code multiple times without

you writing down every line of code

manually so

let's say that I want to print

10 numbers in the console or 20 or 50.

instead of printing instead of typing

that code manually move line by line

which will be very tedious you can use a

loop and you can put that code in a loop

and in that code in the loop it will be

executed repeatedly until

a certain number is reached so let's see

how we can define a loop to define a

loop we type here

four this is the first Loop that we're

gonna look at because there are other

loops

so this is the for Loop we press enter

inside the parenthesis we type I this is

convention you can put whatever name you

wonder

now we put here in this is the keyword

in and now let's say that I want to loop

from

one

and from 1 to 10 and to do that to type

1 dot dot this is a range

one to ten now

I put a curly braces

and inside the pre the curly braces and

I'm gonna put the code that I want to be

executed

10 times so I'm gonna type here println

and I'm gonna type the value of I at

each iteration and uh

we can put it like this you can type

here uh

you can put in the quotation marks and

you can do the placeholder

you can put I

so now if you run this

so now I get

is one two

until 10. so it what is doing is is

looping through this range until it

reaches 10 then it stops

now uh you can write this I like I did

here using a placeholder where you can

write it like this

I've run this code it will have the same

effect but will not have the taste

before here

so now you get

one

until 10.

now there are other other very

variations to use this range and one of

them is to use until so let's uh command

this code

so let's press Ctrl slash

right now let's type 4 again

and now I'm gonna

I'm gonna call it also I here you can

put whatever name you want

and instead of now I'm going to put also

again the in keyword and instead of

putting the dot dot to Loop to arrange

I'm gonna put

one until

10.

all right

now I'm gonna add the printerliner

statement

and here I'm going to type I

and

this will have the same effect with the

thing with a single difference that 1

until 10 is is gonna exclude ten and one

uh

using the range.10 it will include 10.

so if you run this code

we're gonna see that you will not have

10 here because 10 it is excluded when

you're using the until

so now as you can see we have one until

nine and then it's excluded because now

we're using the until

so if you if you're using the range

which is uh dot the last number is going

to be included but if you use the

until the last number is going to be

excluded so it's important to know that

this two respectively the until and the

range can only count upwards

so we cannot put here 10 until 1 because

the first number the number of on the

left needs to be smaller than the number

on the right so we cannot put 10 until

1. we can only count upwards you can

only put one until 10 or

1.10 so

to Loop backwards we need to use

another thing so let's comment this code

also

now to Loop backwards we Type 4

again in the paradise you put the I here

you can put

whatever if you want

in

so here you can put whatever name but

the convention is I issues now I'm going

to put in and now I'm going to put down

first let's put uh

10

down

down to

one

let's add the curly braces

the printerland

and let's put I here

now this is gonna this is gonna count

downwards now it's gonna it's gonna

count from ten to one so if you run this

code

now get 10 9 so it's counting it's it's

counting backwards right another thing

that you can do is that you can

introduce an arbitrary step so let's uh

let's comment this code also

let's add below another call

another for Loop so here I'm gonna type

four

parenthesis

let's put I

and let's use uh done TL

so in

one

until

10. so now I can put a

an arbitrary step so at uh at this

arbitrary step that number which

is that is that is at dotted arbitrary

step is going to be skip so I can put

here step

to let's say

so now I'm gonna put the quarter Blazers

to have the some code executed I'm going

to put println

and let's put here I

so now if you run the squad

I get one three five seven and nine so

you can see that uh

at the second iteration the number is

Skip so we have one two is skipped now

we have then we have three four three

and four skips and so on so you can put

this arbitrary step if you want so let's

comment this code also

and let's add the first code that we had

here

to

to talk a little bit more so let's put I

in

one

let's put the range Dot 10.

and curly braces

you're gonna type println

hi

now if run this code

we'll get the same output that as at the

beginning of the video so get one

until 10 inclusive now again what this

uh loop is doing is going through this

is going to this to this range from 1 to

10 and it's executing the code inside

the curly braces 10 times and when it

reaches 10 it stops and it executes the

next line of code below here that

because here we don't have any line of

code our

program X is exited and is finished so

this is our discussion about for Loop

there are other Loops which can Loop

which can loop as long as a certain

condition is true not as long as a

certain number is reached so we're going

to look at the next Loops in the next

video

see you next

all right now it's time to look at the

next two Loops that you can use in

quadrant and those are the while and the

Dual Loop

but first let's open our previous

project called Loops because I'm going

to type my code there so open your

previous project and here I already have

deleted the code that we had previously

here but if you have the code delete it

and let's start our discussion about the

while loop

so first I'm going to type

while

this is this is how you declare the

while loop

and here you can press enter to fill in

the the wallykid on the parenthesis for

you or you can type manually the while

and the parenthesis so I'm going to

press enter to fill that for you fill in

that for me

and next you put a space and you put

curly brace here so I'm going to put the

left curly brace and press enter and the

radical base is going to be added

automatically as you can see on the line

five now

a while loop Loops as long as a certain

condition is true in contrast to the for

Loop which Loops for a specific number

of time while a loop Loops as long as a

condition or an expression is true so

this gives us more flexibility in a

circumstances or you don't know for what

specific number of times we want our

code to be repeated so

we put our condition inside the

parenthesis here

and as long as this condition is true

the code inside the current base is

going to be executed repeatedly so let's

see how we can do that so first let's

discard a variable here

I'm going to call it number and I'm

going to sign a value of 0 to it

and here I'm gonna type while number

is less than 10

inside the calibration I'm going to type

the println function

and we're going to Output the value of

our number and then we're going to

increment the value of a number so I'm

going to put number I'm going to use the

increment operator plus plus

let's put a space here to have things

more clear now if you run this code

you're going to see the numbers from 0

to 9.

so what is happening how this works

first

with the color variable with a sine of

value of 0 to it next we Loop using the

while loop and first the while loop is

checking to see if this condition inside

the parenthesis is true so it checks to

see if the value of our variable number

is less than 10 and it's true because we

have zero then it executes the code

inside the curly braces and it outputs

the value of our number in the console

using the printerland function then it

increments the value of our number

and then it Loops again but now the

value of our number is one so now it

will compare again if

the value for number is less than 10

which is true it will loop again it will

output its value it will increment this

value so now we have two and so on until

it reaches nine when it reaches 9 it

will increment the value it will it will

print it will output the value in the

console it will increment this value

then it will check again to see if this

condition is true but now the value is

going to be

10 because not it was incremented when

it was nine

and this condition is going to be ever

it's going to evaluate to false and the

code inside the cutter base is not going

to be executed this way we don't have 10

here and it executes the code that comes

below of our while loop but because you

don't have any code

the program is terminated

and you can write this thing in one

single line but it's recommended to use

this form only if you know that you have

one single statement in our Loop so if I

can delete this

this tool

and I can put here directly number plus

plus and because this Returns the value

it increments the value and it Returns

the values and expression I can

I can run this code and it will have the

same output

but if I delete the increment operator

out after the

variable and if I put it before

now I'm gonna see a different output

here because this increments the value

of

of our number then it decrements the

value of our number and this is called

prefix in increment increment so this is

called prefix incrementing and the the

previous one let's press Ctrl G

this is called postfix increment so this

is postfix because it's the end of our

variable and uh if you put the

incremental operator at the beginning of

our variable is called prefix oh

increment anyway

let's press Ctrl Z to have the code that

I had previously and the curly braces

now look what happens if I delete the

increment

if I don't increment all variable now

now uh let's just talk what is happening

because we don't increment the variable

and the variable always has the value of

zero this will always be true and it

will execute the code inside the current

places forever so we'll have what is

called an infinite Loop so if you run

this code

now you can see that it prints 0 forever

because this condition never ever has to

to to false so let's stop this by

pressing on this uh

Red Square so now the process the

program is terminated so this is why you

need to increment the value of our

number because we want that condition to

be false at some point in the in the in

the in the future

next

let's say that I assign here 10.

so if I run this code now this condition

is going to be false because 10 is not

less than 10. and the code inside the

curly brace is not going to be executive

so now we have no output here because

this condition is evaluates to false and

is not executing the code inside the

calibration but there are certain

situations where you'll want to have

your code executed at least once even

though the initial condition the initial

uh condition or there is expression is

false so if I delete the curly braces

and the code inside the curly braces

here

now to to do what I said if to execute

the the code even though the condition

is initially set to false to execute the

code at least once you need to use a do

while loop and to use a do while loop

you put here at do press enter to add

the calibrases and we put our while at

the end of of our enclosing curly brace

of Urdu and here inside the calibration

or I'm going to put our printerland

function

and I'm gonna output the value of our

variable number

so now if you run this

now get 10 because this executes the

code inside the curly braces until at

least once and after that it comes down

here and it checks to see if this

condition is true or false and if this

condition is true it will repeat the

code

but if the condition is false it uh it

will not repeat the code but the

important aspect here is that it will

execute the code inside the curly braces

at least once irrespective of our

condition if the condition is true of

order false in our case is false

so these are the three Loops that you

can use in a kotlin the for Loop the

while loop and the do while loop now

it's time to see how we can use the

continue and the break keywords with

those Loops so let's press Ctrl Z to

undo the code have the while loop back

because I'm going to start the while

loop and to see how we can use the but

you can continue cured with the while

loop

let's bring this up a little bit

so let's first look at the continue

keyword and the continue keyword can be

used to bypass a section of code so

let's say that I want to skip the number

seven in our Loop so I can type here if

our variable number

equals 2 so 2 equals seven let's put

curly braces press enter toward the

right calibrase continue

so now what this is gonna do is when it

is gonna reach the number seven and this

condition is going to be true it's gonna

call continue and it's going to bypass

the code that comes below of our if

statement so if you run this code

look uh what happens

it happens nothing because uh here our

number has a value of 10 so this

condition is false so the code is not

executed so let's put here zero

now if you run this code

so we get 0 1 2 3 4 5 and 6 but uh

and you see that I save at seven it uh

it skips the code but why I don't have

the next numbers this is because uh

unintentionally we created here an

infinite look so let's stop this and uh

to explain

so what happens here is that because

um we said we say here explicit to

you to check here explicit we check here

to see if our number is equal to seven

then we're gonna continue and in other

words you're gonna skip the code which

comes below of our

of order is the then statement then this

code is never called so this code is

never called the printerland and more

importantly the increment operator is

now never called on our number variable

and because of that

because our variable is not incremented

here either when it's gonna loop again

it will have again the value 7. so this

condition or the in the if is going to

evaluate again to true and it's going to

call continue again and because this

code again is never reached Discord and

the values no not not incremented it

will still have the value seven so this

is gonna evaluate again to true so it

does this forever and to change that we

need to put our

number

and which is incremented before our reef

and that is going to solve the problem

so now if I

Let's uh

now

if I run the code let's increase this

now get one two three four five

and seven is skipped then 8 9 and 10.

because now when this condition level is

to true it will continue it's not going

to print the code it's not going to be

executed below of our if and it will

come back to Loop and now it's going to

increment the value and the value is

going to be

8 instead of 7 this is going to evaluate

to false and the continue is not going

to be called it will print the number

eight and it will do the same for it it

will increment the value again and then

we'll have 9 and so on so this is how

you can use the continue keyword and you

have to be careful where you put your in

uh your whatever incremental variable

because I you saw you can easily get

intro infinite Loop

now you can have a more complex

expression here so you can put here if

number is greater than let's say 2 and

number

is less than eight

now if you run this

look what happens

now get one two and then we get 8 9 and

10 because what this condition now

checks is is if our number is greater

than 10 and less than 8 then continue

then skip that number so this is why the

numbers between 2 and 8 are skipped

because we call continue on on them and

if you hover over here you see that it

says the two comparison should be

converted to a rain check so you can

write the same condition that have here

using a range but this is different from

a range that you you've used in a for

Loop in the sense that this is now

checking to see if our number is in the

range 3

from 3 to 7 so if you run the squad

now I have the exact same output but now

we are using the range to check to see

if our number is in the other engine if

this is the underage we call continue to

skip that

um

the next thing that you can do is you

can

break the loop you can stop the loop

using the break here so I can say here

again if number

equals equals let's say seven

call break

so I'm going to type here break

now when this condition is going to vary

to true it's gonna call break and the

loop is going to stop it's not going to

execute any line of code after that it's

gonna stop it's gonna leave and it's

going to execute the code which comes

below here but in our case we don't

don't have any code

so you see that we have one two three

four five and six and when it reaches

seven it breaks and it nuts is stopping

the loop it's not printing the value and

it executes the loop so it terminates so

foreign

and we can do the same thing with the

for Loop so we can use the four we can

use the continue and

and break with a for Loop so you can

type here four

let's say I in let's create the

let's say

zero

to 10 let's create this range

let's add the curly braces

now I can say here if

I

in

let's say three

to eight

then continue

let's put in the volume

let's type here println

let's print the value of I here

so now if you run this code

so we have our first Loop which Loops a

to six and then it breaks because you

call break there then it's using the for

Loop it Loops it loops

from 0 to 10 but because here we're

checking to see if our number is in the

range three to eight and skip it you get

0 1 2 and then the numbers uh the three

and uh the numbers between the three and

the numbers between 3 and 8 inclusive

are skipped here so we have 0 1 2 and

then the numbers between two and nine

are skipped and we have nine and ten

and we can use also the brake keyword

with the four I can put here if I equals

equals seven

break

[Music]

so now if you run this code

now you get 0 1 2 3 4 5 6 and when it

reaches seven it breaks similar to our

while loop so this is how you can use

the break and continue cured with the

while and for Loop and you can use the

brake and continue cured also with the

Dual Loop

next we're gonna look at how you can

Nest Loops within each others and we're

going to look at an example using the

while loop so I'm going to delete the

code of the for Loop and the printable

statement there

I'm going to delete the if then

statement the break yard and I'm going

to bring the printerland function up

there

down here I'm going to declare a

variable it's going to be a VAR I'm

going to call it I and I'm gonna assign

a value of 0 to it now in inside the

while loop I'm going to type another

while loop here I'm going to put a

conditional while I is less than 5

we're gonna increment I and we're gonna

print and output the value of I in the

console but let's put some text here to

make uh clear what Loops what Loop is

looping so I'm going to put three

asterisk signs here

I'm gonna type the dollar sign and I

so now if you run this code let's also

change this condition too

less than five

so if you run this code

now we get first you get one then we get

our code inside of our inner while loop

which is the three asterisks and get one

two three four five then it Loops again

but let's take this chord line by line

to explain what is happening here

so first decade is variable call number

assign a value of zero to it then we

Loop using the while loop the outer loop

and it checks to see this condition

issue and is true zero is less than five

so it executes the code inside the curly

braces and it increments the value of a

number by one then it outputs the value

of our number in the console so you get

here one

then it declared this variable called I

and then it starts looping inside our

outer loop while using the inner loop

while so it Loops here five times

so when it finishes it goes back

and now the value was incremented by one

it will check again if the condition is

true it will uh execute the code inside

the calibration to increment the value

now so the it will have two it will

output that value so now we have two

here

and then it will loop again five star

five times using the inner loop and it

will do the same thing for the next

round and the next round until you get

to

5. when

we'll get to five five will be not be

less than five and this condition will

evaluate to to false and the code inside

the caliber is not going to be executed

right

and you can use the break keyword inside

the inner loop so I can say here if

you can put it above here so you can say

if

I equals equals zero

then break and now you can put the break

and continue cures in one single line if

you want you can avoid the curly braces

so now if you run this code

now get one two three four five because

now uh when it reaches the inner loop it

breaks immediately what every at every

iteration in the outer loop so when it

comes down here it will check to see if

the conditions through is going to be

true but when it gets to the if

statement it will break it will stop the

loop so it will execute only the Outer

Loop

and with a break keyword you can

not not Target only the loop so with

with this break keyword we stop you stop

only this while loop that uh

that we are currently that is currently

the nearest Loop but there is something

which which is called labels and with

labels you can Target an outer loop so

it can break the outer loop here so you

can break the while loop which is here

and to do that we we give to our while

loop a label so we type here a name I'm

going to call it outer and we put at

now here you can put break and now I'm

gonna type

add

and you put outer and this this should

be a single word

now if you run this look what happens

now get one because it breaks the outer

loop so it first evolves this condition

it increments the value of our number it

prints the value of a number and then it

goes in the Inner Loop it checks to see

if this condition is true it's true then

it's checked to see if I is equal to 0

which is uh true then it breaks but now

it's not breaking this Loop this inner

loop

it's breaking the Outer Loop so it's

breaking this Loop so this is why you

get only one and the code is not

executed because it breaks the outer

loop and the code our program or code

it's terminated because about down here

we don't have any code so this is how

you can use labels with the break

keyboard to Target an outer loop and the

labels can be used also with the

continue keyword and in other Loops that

we've looked at so this is our

discussion about loops what you can do I

should say is that you cannot Target

with the labels and inner loop from an

outer loop so you cannot

we can give here a name to a label to

our inner loop but you cannot Target our

inner from our outer loop so this is our

discussion about loops and the next

video we're going to do a challenge

all right so now it's time to do a

challenge using all the knowledge that

we have accumulated so far and I'm gonna

use the previous project to put my code

there but if you don't have the previous

project you can create a new project and

give it whatever name you want and type

your code there so I'm going to open my

previous project

I'm going to delete the code inside the

main function

now what is the challenge

the challenge is to

create an arbitrary range of numbers

then we need to find a way to go to that

arbitrary range of numbers

and to determine if a number is an even

number or if a number is another number

and based on that if the number is an

odd number skip that number

but if the number is an even number then

output a number in the console

additionally should create a way to

count all the even numbers that you

found in your range and display the

total number so if neither numbers

um at the end of your program

so try to do this solution and after

that you can watch my solution to this

so first I'm going to decide I'm going

to declare a variable call number

I'm going to assign a value of 1 to it

next I'm going to declare another

variable but this is going to be a Val

it's going to be called last number

and it's going to have a value of 20.

now I'm gonna Loop using the while loop

and in the parantes you're going to type

while number

is less than equal to last number

then you're going to put curly braces

now I'm going to increment our number

now here you need to add the logic to

determine if a number is an even number

or not and to do that I'm going to add

the function at the end of our enclosing

curly brace of our many functions so I'm

going to type here the fun keyword

now you need to give a name to our

function it's going to be called is even

number

I'm gonna put parenthesis and it's going

to take a parameter called number of

type int

this function and it's gonna return a

Boolean value true or false this

function so you put colon and we type

Boolean then you put curly braces and

now we'll add the logic to determine if

any number is even or not so you type

here if

and now inside the parenthesis we put

another set of parentheses

and here we refer our parameter number

that we have defined above and here we

put the modulus operator because the

modulus operator gives us the reminder

of a division so we put a modulus

operator 2.

now outside our inner parenthesis we put

equals equals zero then we put curly

braces

and return true

else we're gonna return

false now how this logic works

so

our num our number parameter which is

defined here it's going to take an

argument let's say that it's going to

take an arbitrary number

then he's gonna check to see if that

number divided by 2 has no reminder if

it's and if it has no reminder then that

number is an even number because the

because even numbers when they are

divided by two they have no reminder

and if that is the case if the reminder

equals equals to zero our number is an

even number and it's gonna return true

and their spot is not going to be

executed

now let's add the logic here

list user function here because as you

can see now it's uh gray out it's not

used so let's type here if

and in the parenthesis you're gonna type

is even number our functions we're going

to call our function here and we're

going to pass a parameter to it called

now I'm going to pass the parameter

we're going to pass an argument and the

argument is going to be the variable

that we Define above as you can see you

have the V there so press enter to fill

on that for you let's press Ctrl alt L

to format the code

now

if we press shift Ctrl p on this you're

going to see that this is a Boolean

expression because this returns true or

false so we can put the logical not

operator in front of it to change its

value to switch his value from True to

false and from and from false to true

so let's put curly braces

and here we're gonna call our continue

quick keyword down here I'm gonna add

a printerland

which is going to Output the the number

in our console

next we need to add the logic to to

count the total uh the total even

numbers which are found so I'm gonna

declare a VAR it's going to be

even numbers

counter and I'm going to assign a value

0 to it and here we're gonna increment

our even numbers counter

so now if you run this code

we get 2 4 6 8 10 12 14 16 18 and 20. so

these are the even numbers that they are

that they found that the our program

found in is correct but let's add a

print line at the end of our Loop to

print the total number of numbers of

even numbers found so let's put println

here and let's put let's refer our even

numbers count here

you have a numbers counter so if you run

this

let's put some text actually let's put

this in curly braces so let's type here

total

number of even

numbers found

equals

dollar sign

now if you run this

now you get total numbers of you know

when even numbers found this 10 and this

is correct so in an output correctly or

even numbers and it show that the total

number two newer numbers found this 10.

so let's now think about how this logic

works here

so let's look at the if statement that

we have here so what is doing in this

thing here so let's take an um an um

specific example a specific number to

store to see how this works so let's see

that here we pass four and it's gonna

use the logic to determine if this

number is an even number and if that

number is any of a number is going to

return true so this

this function which is a Boolean

expression is going to return true and

then we're gonna negate this value gonna

change this value from True to false and

continuously not gonna be called and the

code which comes below which is uh our

variable which uh counts how many even

numbers uh we have then it it increments

then it outputs that number in the

console so we have our even number four

output output it in the console

but if you pass here 3 that the number

is not going to be an even number

because has a reminder and this is going

to return uh false and because of here

you put the logical not operator that is

going to be switched to true

and the continue that's going to

continue keyword is going to be called

and our code Channel which comes below

here which is uh our variable even

number counter is not going to be

incremented

and our number respect to our number

three now it's not going to be outputted

in the console so this is how this works

so this is our solution to our

to our challenge of course you can

simplify this so if you hover over here

because you have all of those underlines

you can see that says remove redundancy

if statement

so this can be simplified to this this

does the exact same thing it is going to

return true if this uh divided by 2 has

no reminder otherwise it's going to

return false and this can can be

simplified even further because if here

we have a single expression and we know

when we have a single expression we can

write our function as a single

expression function so I can remove the

curly braces here

you can remove

the bully return type you can remove the

return keyword

I'm gonna put the equal zero and this

now if you run this code

this will work

the same

so we have the same output here

now I use the function here because

we've talked about functions in our

previous videos but you can remove the

function completely and add the logic to

determine if a new number is even or not

directly in the if statement so you can

put here

if

let's put parenthesis our number

now we're going to use the modulus

operator tool

not equal

to zero

then continue and this will have the

same effect so if you run this code

this will work in the same way

so we have the same code here because

this does the exact that the function

did previously it will check to see if

this is a new number if this is any of a

number

the

this is not going to be true because

hero checking to see if this is not

equal to zero and our number is equal to

zero so this condition inside the

parenthesis is going to be false the

continue is not going to be called and

the our code below is going to be called

our even number counter is going to be

incremented and our number is going to

be output in the console so this does

the same thing as previously

and we can do this challenge also in the

first of the four Loop if you want

so you can type here four

I in one

to 20 then execute the code inside the

curly braces and here you can

check if

parenthesis again if I present

2 not equal to

zero

then call continue

then

else is gonna execute the code which

comes below here

which is let's first

assign a value for your to a even number

counter to zero

because where when we're gonna enter in

the for Loop we should have a zero to

not have the previous value which was

20.

so let's put 0 here and here you can

increment or even number counter

plus plus and we're gonna print our even

number so I'm going to print I here

so if you run this code

let's run it from here

but let's add the printerland to make

things more clear so I'm going to add

here print

let's put

quotation marks and here I'm gonna type

total

number

phone

using four

Loop

r

let's put the dollar sign now let's

refer our even number variable

now if you run this

now I have the exact same output but now

we're using the

first you're using the while loop then

you're using the photo clip so let's

let's add the printerline statement

between our

Loops to make clear which Loop is

looping so let's draw the print line

here

and let's type here

now

we

so we are

looping

using the

four

Loop so now if you run this

to make more clear which Loop is looping

so now as you can see first the while

loop Loops then we have our printerland

which

says that now our while loop our for

Loop starts looping then we loop with

the for Loop

then at the end

we output the total number total number

of even only fever numbers found with

the while loop we have 10 then we output

the total number found the total number

of even numbers found using the for Loop

and r10 so our code works perfectly well

and

uh this do not does not represent the

the best

the best way to solve this challenge of

course that you can

find a totally different way a better

way to solve this so

this was my this was just my way of

solving the problem

so see you in the next video and how I

hope that you enjoy this challenge so

now it's time to start a discussion

about the race but first let's create a

new project I'm going to call it a race

for the language select kotlin for the

build system IntelliJ make sure to have

the jdk selected and check this little

box at sample code to have the main

function Auto generator for us and click

on create

all right now let's delete the code

inside the calibrases of the main

function

let's hide the project Pane and let's

start our discussion about arrays and so

far in our videos we've only looked at

how we can store only one single value

in a variable and that is very useful

but what if you have a large amount of

values that you want to store in our

variables let's say that you want to

store

30 or 40 different

values in our variable of a certain type

of course that you can declare a

variable for each of one of those

values then and then assign it to the

variables but what happens if you have a

1000 or 10 20

000

values in that case uh it will not be

practically possible to write each one

of those variables and then assign a

value to it in a colon help us here

because it provides us arrays and an

array allows us to store more than one

single element in a variable name so it

allows us to store more more elements in

one single variable name

now to declare an array we type Val

hero type

names

next we need to provide the type and the

type is going to be an array

and IntelliJ is giving us some

suggestion there so press enter to fill

it in that for you and now we put angle

brackets and inside the angle brackets

we're going to put what kind of data

this array is going to store so it's

going to store string or text or

sequence of characters

then you put equals and here we don't

type political value here we have a hero

type of function call and here we type

array of

and now we put our elements inside the

the parenthesis of the function called

separated by comma so I'm going to put

three names here so I'm going to put

John

Steven

and Megan

now let's uh let's see what we have here

so first we have the Val keyword next we

have a name for our variable and the

name is names then we have the type and

the type is an array which means that

you can store multiple values in our

variable and next we have angle brackets

and string and this means that this area

is going to store text or a sequence of

characters it's gonna store only strings

and then you put equals we have the

function called array of and then we put

our elements of type string which is

those names separated by comma but

because cotton has Type inference here

you can delete the type

and the type is going to be inferred in

the same way it was it was inferred

previously when I when you just declare

a simple variable so now the type is

still inferred as an array of strings

and now it's a good moment to introduce

type hints but we need to activate the

typings from settings and to do that you

can go to file

and click on settings or you can press

the the shortcut that you have here for

our particular system and for Windows is

Ctrl alt s so I'm going to press Ctrl

this

and here we go to if you have something

like this you go to editor click on this

greater arrow in front of editor

and then you go down here where it says

what it says inline hints click on on

this Creator are in front of inline

hints and from

those options select kotlin

and here you go to types and select

select local variable types then click

apply

and okay

now what you have here on the right of

our variable name is a hint so it's not

real text this is just there to remind

us what kind of type this variable has

and in our case is an array of string

and my suggestion for this to keep those

scenes activated because in this way

you'll always see what type of variable

is without explicitly declaring the type

so this is not uh this is this is just

there to help us to remind us what kind

of type a variable is

now let's say that I want to Output a

certain element in the array in the

console

to do that we cannot just type here

println and pass names because you need

more to be more specific than that we

cannot pass the array here and to get a

specific element in the array you need

to know that the elements in the array

are stored at indexes and the index of

an array always starts at zero so the

first element is stored at the index 0

and to get the first element we tab we

type here

square brackets and you put 0 and that

if you press Ctrl

if you press shift Ctrl p on it

it's a string because that is going to

return as the first value in the array

which is uh John so if you run this code

we get John but let's put some text here

to make it more

clear that this is the first element so

let's type first

element now let's choose the dollar sign

because this now is a placeholder and

let's put the curly braces around our

our element

and the quotation mark here now if you

run this code

you get first element John let's put

here some

so get the first element John because uh

John is stored at the index 0. and in

the same way

John is stored at the index 0 Steven is

stored at the index one and Megan is

stored at the index two so our index

starts at zero the index of an array

always starts at zero and you can

replace

a specific in an element in the array

also using the index operator so you can

type here

names again

square brackets zero

equals so now I'm going to assign a new

value to our element stored at index 0

which is now put here

the value

Alex so now if you run this code

now get first elementalics because

John was John which is at index 0 was

replaced with the value Alex so now we

have Alex Steven and Megan but what you

can't do is you cannot get an uh element

at neither and an index that doesn't

exist so we cannot get here let's say an

element of the index 4. because if you

run this look what happens

we get

an error which says array index out of

bond extension index 4 out of bonds

bonds for length 3. that is because the

index 4 doesn't exist and here we try to

access that value and that value at that

value and that index doesn't exist and

this is called the runtime error because

this error is is called only one while

our app is still running so you don't

have here an error like we have for uh

here we don't have a compile error which

you saw in our previous videos that is

because

the size of an array is not known we

tell our compile time also it's very

important to know here that the size of

an array is fixed so this will always

have the size three and uh the only way

to add new elements to it is to put the

elements directly here but there is not

another way to let's say that I want I

can put another element in the array

down here we can only replace elements

and then in the next videos you're gonna

see that there are other arrays

which are more flexible in the sense

that they can grow and Shrink we can add

the or remove elements from those arrays

but for now uh we're gonna look just at

the for this array

so let's change this back to zero to

don't have that error

and um

to avoid this kind of Errors you can

check to see what is the size of the

array in other words to see what is the

number of elements stored in Array using

the size variable so let's do that so

I'm going to add the printer lens

statement below for first one so I'm

going to type here println

and here I'm going to put the text the

size of the array is

and here I'm going to put the dollar

sign curly braces now I'm going to use

our names array

and you're going to put that

size

and this is gonna give us the size of

the array or or it's going to give us

the another words the number of elements

which are stored in this particular

array so if you run this code

you're going to see first output the

first element is Alex

and then you're going to see the size of

the array is 3.

so we get first element Alex and the

size of the array is three and this is

very important to note that the size of

the array is always one times greater

than the last index in the array so we

have an array which starts as index 0

and and ends at index two

which has a size of three so very

important to remember this to avoid the

crashes and your programming and of

course that you can put other data types

in our array so I can declare another

array here also Val

called

numbers

and we can use the array of again

and here I can pass some numbers

and now you see the type is an array of

Fint or an array of integers or whole

numbers and you can also mix types here

so you can put here

numbers and strings

so now we have another and this which is

between our angle brackets you don't

know you don't need to understand this

for now but this allows us to have array

of two types respectively of type int

and of type string and you can post also

a chart here if you want

let's say a so we can create an array of

any type if you want

so this allows us to

to put to mix our types in in the array

and because we've looked at loops and

how you can use loops

you can use Loops to Loop to this array

so I can put here

let's say that I want to use the first

array so I can put here four

name

in names now I'm going to refer our

array of strings

then

print

print A lender name

so now you see that we have this type

hint for our name variable that we have

here of course this can have whatever

name you want now if you run this you're

gonna see our three names outputted down

here in the console

so now we have Alex Stefan Steven and

Megan because uh now you're we're using

the for Loop to Loop through our array

names

and uh

this is interesting but what if you want

to Loop through our array numbers which

is a mixed array we have numbers and

strings and charts we can refer it here

let's call it

I

in

and now you see the type that was

inferred for the eye is this comparable

that we have here

so this

is going to Loop through our mixed array

so we put here I

now if you run this

you're gonna see our

numbers and in our name one and our

chart output down here

so now we get

first element Alex which is a

printerland statement then the next

print line statement the size of the red

then you have our

numbers in our numbers array

and then we have our name one and our

and then we have our chart

so this allows us to to Loop through our

mix arrays but what if I want to

to Output

only if I want to Output in the console

only the integers in this mixed are

equal numbers to to do that and this is

a

good good

way to introduce this keyword and with

this keyword we can check to see if

a literal or a variable is supposed

certain type so I can put here if

I

is let's say an INT

then and only then I'm gonna output this

value in the console then I'm going to

call our printerland and I'm gonna post

our password I variable to our println

function

now if you run this you gonna you

gonna see in our Loop only

the numbers here only the integers

because

this is keyword is checking to see if

our right is of type integer and this is

going to return true or false in our

case it's going to return true for our

for all of our numbers that we have here

so we have four five six seven and four

again

and you can check to see if this is a HR

I'm gonna see in the console only our

single element a I'll put it down here

so we have a or you can check to see if

this is a string

this element that We're looping at this

particular iteration let's say so if you

run this you're gonna see in the console

only name one because only name one is

of type string

so I get name one so this is very

important to know that with the is

keyword you can check to see if a

variable or a literally is of a certain

type or if it's not and based on that

too to just to do some

in our case you output that value in the

console if that condition is true

so this is how you can use the is

keyword and the for Loop to

to the for Loop to Loop through our

array and these keyword to check to see

if an element in our arrays of a certain

type or if it's not and let's change our

array numbers to a different name

because numbers is very specific it

implies that we only have numbers in

this array

but we have a mixed type so we have

multiple types in uh our array don't

have only numbers

and to do that instead of changing our

numbers array and everywhere in our code

where you're using the this variable we

can right click on it

go to refactor and click on rename or

you can click shift F6

now if I change the name of our array

numbers it will be change everywhere

where you call this variable so look if

I delete now the name the name is also

changed in our line of Code 9 we're

using in the loop so now if I change

this to let's call it mixed

elements

if I press enter now this will be also

changed everywhere or we call our our

variable respectively on the line 9 here

so now we have our our

array numbers change to mixed Elements

which is more uh descriptive because it

implies that we have an array of mixed

elements

so now it's time to do a challenge using

the knowledge that we have about arrays

but first of all I'm going to delete the

code inside the curry base of the main

function because I have the previous

project opened

now the challenge is to create a

function which is going to take as an

argument an array of integers then you

need to figure out a way to determine

which number is the biggest number in

that array then we need to return that

number to the function

and you need to create another function

which is going to do the same thing but

it's gonna figure out what is the

minimum number stored in that array and

then it's going to return that value

and third you need to find a way to

combine those two function in just one

function which is gonna

return the maximum value stored in that

array if you want or if you don't want

it to return the minimum value so try to

solve this Challenge and then watch my

solution

now let's solve this challenge

so first I'm going to create a function

which is going to be called the find

marks this function is going to return

the maximum value

and it's going to take us and I'm going

to Define here parameter and an array so

here I'm going to type a name for our

array is going to be called numbers

n is going to have the type array

of int of integers and it's going to

return an integer

now here I'm going to declare a variable

it's going to be a VAR and it's going to

be called Max

and in this Max variable I'm going to

store the first value in the which is uh

which is past which is in the which is

in the array so I'm gonna type here

numbers

square brackets

and I'm going to use the index 0 to get

the first value in the array then

I'm gonna Loop through this array

so

for number in numbers

then here you're going to check to see

if our

number

is greater than our first value which

was stored in the array or max value and

if that is true then I'm gonna store

this value which is now

greater than our than our maximum value

in our Max variable so here I'm going to

put Max now it's going to be equal to

this number which is found to be greater

than our previous max value

and at the end of this loop we're gonna

return

the maximum value

and we're going to do the same for the

finding the minimum value so I'm going

to copy this code

and this is going to be called find mean

but uh here gonna change a little bit

the logic let's call this variable mean

and you're going to change the condition

to less than min

[Music]

now let's call those function in our

main function and let's pass summaries

to see how it works

so first I'm going to declare a variable

it's going to be a Val and it's going to

be called Max

and now I'm going to call our find Max

function and here I'm going to pass an

array and I'm gonna I'm gonna pass the

array directly here so I'm going to type

array off and I'm going to pass some

arbitrary numbers like

then I'm going to create another valve

this is going to be called mean

now I'm going to call find

Min and here I'm going to also

call our array of function and here I'm

going to Define some

arbitrary numbers again

now I'm going to Output those value in

the consoles so I'm going to add

printerland here

I'm going to type max value is dollar

sign

and I'm going to refer our Max variable

defined above

another println

now let's run this code it's

get max value 7 which is correct this is

the biggest number then we get the

minimum value is 4 and this is also

correct so our code Works uh perfectly

well now the next thing is to combine

those two functions that we have here

let's

increase this now we need to find a way

to combine those function in one single

function which is going to also return

the maximum and minimum value but only

if I want to return to be returned so I

can determine if I want the minimum

value to be returned by that function or

if I want the maximum value to be

returned by that function so I need to

find a way to combine those two

and to do that I'm going to delete our

second function

I'm gonna delete the code inside the

this function

and I'm going to change the name of this

function to

find mean and Max and now here I'm going

to define a parameter which is going to

be a Boolean uh which is going to have a

Boolean type so I'm gonna call it

I'm going to call it search

Max

and it's going to be

a Boolean

all right

now

based on that Boolean value if is true

if I want to search the maximum value

here I'm going to type an if statement

so I'm going to put if search marks then

I'm gonna

add here the code to search for the

maximum value the first let's define um

a variable here is going to be also VAR

let's be called gonna be called

Max and uh it's also going to have the

first value in the array

which is also adding the zero now if our

search marks is true then we want our

maximum value to be search and return so

I'm gonna add here to the code to to do

that so I'm going to type here a for

Loop

which is going to go through our numbers

array

and here I'm gonna add the

the if statement which I'm gonna check

to see if our number

is greater than our Max then I'm gonna

assign our

our

number Tower Max

now I'm gonna go down here and and here

I'm going to add the else part so if

this uh

argument which is passed to our

parameter search Max is false so I don't

want to search for the maximum value

then that means that I want to search

for the minimum value so I'm going to

put here else now I'm going to add the

code to search for the minimum value and

for this you need to Define also

variable here

it's going to be called mean and it's

going to have the value of Max I'm going

to explain a minute how this works

now

here we're going to gonna also Loop

in our

numbers draw numbers array

and you're going to check if we

essentially we have the same code that

we have in our find the mean function so

here we're going to search if our number

is less than our mean

then gonna sign

our number to our mean

also I need to return this value

at the end of our Loop so here we need

to return

Max

and here you need to return mean

now let's press Ctrl alt L to format the

code

now let's delete the

those the code that you have here

let's delete also this code now

here now I'm going to put our fine Min

and Max function directly in the

printerland statement so I'm gonna type

here uh or to put in a variable for so

let's put in a variable first

so I'm gonna

Define a variable he's going to be also

a map it's going to be Auto Max and here

you're gonna type find

Min and Max

now here you need to pass an array first

so I need to pass

we need to call our array off we need to

Define some numbers here let's say uh

20

40.

50.

now we need to pass

an argument to our parameter search maps

which is going to be either true or

false so first I'm going to pass through

here

now if we print this value if you output

this value in the console

let's say that we type here

the marks

equals to dollar sign I'm going to refer

our variable

now if you're around the squad

we get the maximum value is 100 which is

correct now let's duplicate this code

that we have here and let's try it for

Dominion value

let's call it mean

and let's add the printerland uh

statement down here

let's type

domain

values

dollar sign mean

now if you run this code

and let's pass here to false

let's run this code again

now get the maximum value is 100 which

is correct

and the minimum value is 20 and we did

this with our find Min and Max function

by combining our two previous functions

and the our code on the networks okay

now it's time to

have a discussion about how this code

how this code how this solution to my

challenge works

so now let's see how our logic inside

our find Min and Max function works so

firstly we declare our function find the

minion marks then we declare two

parameters which is one is numbers and

is of type array of integers so here

we're gonna pass only an array of

integers

and the second parameter is called

search marks and it's a Boolean so you

can pass here only true or false then

we're going to say to the then we say

here that this function is going to

return a value

next on the line 9 we declare a variable

which is called Max

and we assign using the index operator

the first

element in the array in our Max so in

this case we're sine 20 in our Max

variable and then we declare another

variable called mean and we assign our

marks in our mean but our Max has the

value of 20 because we assigned it here

so this variable mean is also is also

also having the value 20. so both have

have the have the value 20 at the start

then we check to see if our search marks

parameter is true or false and if it's

true then I'm gonna execute the If part

and if part works in the following way

first we Loop using the for Loop through

our numbers array

then we check at each iteration if our

number let's say if 20 is greater than

our Max so if 20 is greater than 20

which is going to be false so the code

inside the calibration if it's not going

to be executed

then go for the next iteration so here

now we're gonna see it gonna check to

see if our number respectively 40 is

greater than our Max which is 20. and

this is going to be true and because of

that the code inside the calibration is

going to be executed so now we're going

to assign to our marks which is now is

20. our number which is 40. so now our

Max variable has a value of 40. and

we're going to we're going to do the

setting for the next for the 50. so this

condition is also going to be true so

we're going to assign it to our Max 50

and so on and so on

until it reaches 100 which is uh which

is going to be also true this condition

is going to repeat true and I'm going to

be assigning

100 to our Max

value of course that if we had another

number here let's say two is going to

Loop for that number also and this

condition is going to be false for that

because our number two is not going to

be greater than Max and it's going to

exit it's going to exit the loop it's

going to tell it's terminate and it's

gonna return the maximum value which is

100 like we saw in our

output in our console and at this point

when we return our max value the

function is left so it's not executing

any line of code

besides our

return Max is not going to execute the

subsequent code which comes below but if

our search marks is false it's going to

execute the else part so in this case

it's gonna search

for the minimum value so here we have

the same for Loop we search you go

through the

through all the numbers but here not

checking to see if our number is greater

then marks you're checking to see if our

number is less than our minimum number

so you check to see if our let's say uh

let's say let's take 20 30 40

is less than our minimum value which is

20. and that is going to be false so

this code is not going to be going to be

executed inside the calibration of the

if and it's gonna loop again and now

it's gonna check for the fourth and

that's also going to be gonna be

um

false so and so on so and so on until it

reaches 100 which is also going to be

false the loop is going to execute and

it's going to return 20. of course that

if we put here two

let's say now the minimum value is going

to be 2 because it's going to Loop for

another iteration and now one is going

to compare C4 number

respectively two

is less than our mean which our mean

previously was 20 that is going to be

true and it's going to assign our 2 to

our mean so if you run this code now

now you're going to see that the minimum

value is not 20 but it's but it's two

and after that we return this value to

the function which is called

so now get the minion values 2 which is

correct so this is how this uh code

works

and see you in the next video so now

it's time to start a discussion about

object orientated programming and

particularly in this video we're gonna

look at classes and objects but first

i'm going to create a new project

I'm going to call it op

for the language select Catalin and for

the build system IntelliJ make sure to

have the jdk selected and also I'm gonna

check this little box because this ad

sample code is going to add the main

function and that code for us

so I'm going to click on create to

create the project

now I'm going to delete this code

and I'm going to hide the project pane

now what is object oriented programming

object orientated programming is a way

of writing code in a sense in the way

you think about the real world so in the

real world we have objects

which can interact with it with each

other and which can do stuff but the

objects in the real world

have two major characteristics first is

the state so another the state of an

object in real life is represented let's

say that we think about of a car

the state is represented by the color of

that car by the number

of Gears that car has by uh by the brand

name of that car so this is the state of

a car the next thing that real life

objects have is functionality

so objects can do stuff so let's take

again the example of a car

a car can move a car can stop a car can

switch gears so

objects in real life have this uh though

this those two characteristics is those

two major character characteristics

represented by the state and the

functionality and the aim of object

orientative programming is to construct

objects using code in the way they are

they in the way they are in the real

life

but uh to do that let's say that you

want to build an object an actual object

in real life what you first need to do

the first thing that you need to do you

need to create a template or a

construction plan for creating that

object in which you're gonna Define the

generic things that a car has

and in programming you will do the same

thing but instead of using a piece of

paper or uh or a something

on on your PC to draw that that template

in programming you're going to use a

class so the class is used to create the

template which then is going to be used

to create an actual object an object

which is going to have a specific State

and a specific functionality

so um

in a way you can think that

the real world serves here as a model

for us to write code

and in this way in by combining uh the

state and the functionality in one

entity and have an object and as you can

see you can also have

like in the real life objects

communicating with each other you're

gonna see that this is really powerful

because you can you can model your code

in a more dynamic in a more you know

more create creative way so this is what

we're gonna do next so first let's say

that I want to create an object of type

car

and to do that we go down here and we

need to Define as I said the

construction plan the template for

building an object and in this case

we're going to build the car object so

we're going to Define what is called a

class and type the keyword class

now we need to name our class I'm going

to call it car and the name of the of

the

class should start with the capital

letter and if it has multiple words it

should be in Pascal case

curly braces

and press enter now inside the color

braces we need to define the state and

the functionality as I said for the

objects that are going to be built with

this template

and to do that we need to use variables

first for the state or

or in the context of object orientated

programming and more specifically in the

context of

classes they are called the classes and

objects they are called properties

and we Define them like we Define a

simple variable we Type R and the next

the first thing that that a car has is a

name or brand name so we're gonna type

name

but we cannot keep this variable

uninitialized we need to provide the

value to it and I'm going to put an

empty string

for uh for now so it's gonna have the

default value an empty string

the next thing is the model what kind of

model this car is so it's going to be

also a string I'm gonna assign an empty

string

the next property is going to be called

the color it's going to be also an empty

string

and the next property is going to be

called doors so this is going to store

the number of doors that a specific

object which is going to be built with

this template is going to have and we're

gonna put zero I'm gonna put zero now

we need to Define some functionality for

this class and for the objects for an

implicit for the objects that are going

to be built with this class

and um I'm gonna add two functions first

one is going to be called move

it's not going to take any parameters

it's gonna just output some text to the

console which is going to say that the

car is moving so we put quotation marks

the card is moving

now I'm gonna Define another function

call stop

also it's not going to take any

parameters it's gonna just outpost on

this the console it's gonna it's gonna

say that the car has stopped

now

we have the template we defined what our

car is gonna

have now we need to create an actual or

object an actual and specific object

using this template and to do that to

firstly declare a variable

and it's going to be a Val and I'm going

to call it car one so so our object is

going to be stored in this variable car

one

then you put equals and we type car and

now we have some suggestions and we have

our car that we Define below our car

class so we press enter then we put

parenthesis

so now we have created an actual object

a real object real in the programming

sense

and

which but now this actual object was

also this is called the instantiation

because we created an actual instance

but we don't need to bother with these

names for now

so we created an actual object using

this template that we have below but the

the properties of these specific objects

are those properties that we Define here

those default properties and to Define

some specific profit some specific

values to these properties that this car

object that created has we need to type

car one

dot so this is called informally dot

notation

and now we have some suggestions so we

have our properties that we defined for

a car

and now we type car one dot and we

choose the name

and now I'm going to assign a value to

this name I'm going to be called Tesla

so now our car one

object that was created has the property

name

Tesla now we do the same thing for the

next property for the model car one dot

model

press enter

and you put we put here s blade so the

model

next we also type car one

Dot now we're gonna assign a value to

the property caller

it's going to be red

next we type also car one and we Define

for the property names for the property

doors also value so we're gonna put here

four

so now our object R1

has for its property for for its

properties some specific values

respectively have for the name Tesla for

the model as played for the car for the

color red and for the doors 4.

so now let's output the property values

of this object that we created in the

console and to do that we put our

println function down here we put

quotation marks inside the parenthesis

because I'm gonna add some text here so

I'm going to type name

equals we put dollar sign then input

curly braces and inside the calibration

type

car one dot

name

and this is going to return the

the value of the property name for the

car one object that we created

respectively is going to return Tesla

let's press Ctrl d three times to do the

same thing for the next properties and

here we put car one dot model so this is

gonna return the

value of the model property

you're gonna put color

and here

I'm gonna type doors

now if you run this code

we get name Tesla

actually let's change the text here to

model to match the property name

model

color

endorse

so now if you're on this

we get the name Tesla Model is played

color red and doors 4 and those are the

values of the properties of this object

that we created here with our

template car

next what you can also do because in the

class we Define the two function we can

call those functions on this object that

we created

and to do that we also use the dot

notation we type car one

dot move and this is going to call the

move function

which is inside the car one

object

let's also

type car one dot stop to call the store

function so now if you run this code

we get uh

the values of our to our properties and

then we get the car is moving and the

car has stopped so the car one dot move

and car one the stop is calling the

functions

on this object that we created here now

let's uh

also add inside the functions

the name of the card that is moving so

he to do that with here dollar sign

inside the class

and we type name

and this is gonna reference this

property name that is defined here

let's also do the same thing for the

stop function let's type name

and now what this is gonna do because

those functions are in they are they're

living inside this object that we

created here car one and this object has

the name Tesla when those functions are

going to be called

they they gonna they're gonna say that

the car

and the name of the car which is in our

case for our objectives Tesla is moving

and then it's gonna say

the car again Tesla has stopped because

they are calling this uh specific object

that we created which has uh those

specific values for its properties

all right so now if you run this quad

we get as expected the car Tesla is

moving and the car Tesla has stopped so

the functions are called on this object

that we Define here and it's using the

property values that we Define for this

object

but as I said with uh you can create as

many objects as you want using the same

template and let's do that let's create

another object so I'm going to type here

Val

Car Tool

let's put equals we type car and then we

put parenthesis

and this create is now another object

but we need to Define some values to its

properties because now the values for

the properties are the default values

that we Define inside the class

respectively the empty string and the

zero

and to do that we type here car2

dot name

so now we are calling the properties

for the car 2 object and now we Define

some values to the cut to the properties

of the car 2 object so they are

this here is not overending

the value that we have here because

this

thing that we have here it's a distinct

object in memory uh from this object

they are this thing distinct objects and

they have their own properties with

their own values and their own functions

and now let's put here Ford

let's put the car to that model

let's put Mustang

let's put

car to that

color

blue

and for the doors let's put two

car two that doors

equals to two and let's output those

values in the console so let's output

the values of the properties for this

second object that we created in the

console and to that to do that I'm going

to copy this code

I'm gonna paste the paste it below here

and now I'm just gonna change

the

the variable to car two to to call to

call the properties on the cartoon

object so we put here car2

so now we're calling the properties

values on our car to object

and now if

I run this code

now we get name Ford Model Mustang color

blue endorseful and the first part is

from the our SEC for from our first

object which is named Tesla Model let's

play The Red Doors 4 and the Carter

slice movie and the cards has stopped

but let's add a space here

to make a clear uh to make a clear which

to make uh clear that this is the output

for the second object to make things

more clear and to add the space we put

here a printerland

statement we put quotation marks and we

put a backslash

n and this is going to add a space

between our println statements so it's

going to add a space between those

statements and we uh and those so now if

you run this code

let's increase this

we get the name Tesla so this is our

first object model let's play it called

Red Doors 4 The Carter size moving

because we're calling the functions on

the first object and the character has

stopped and then we get name Ford Model

Mustang color blue Doors 2 and we have

our space because you put here this

backflash and which adds that space for

us

let's um decrease this

and let's also call the functions the

move install functions on the second

object so I'm going to type here car2

that move

and card two dot stop

so now if you run this

let's

right click and just click on run

main.kt

now get

our first object which is with this uh

value for the value of the value for its

properties and then we get the two

functions called on our first object and

um

we get in our second object which is

values for its properties and then we

get the card for this moving and the car

Ford has stopped because now

um

we're calling the functions on the this

second object and this second object has

the value for the properties has has

different values for its properties so

this is why you get the card for this

movie and the car four has stopped again

they are different

objects they are distinct objects

created using the same template the same

construction plan

and

I'm gonna end this video now and see you

in the next video

so in the last video we saw how we can

create a class and then how we can

Define some properties and some

functionality to that class then we use

that class to create two objects and

then we defined for those objects for

the property of those objects some

values but the way we we Define values

to the properties of the object is to

call the the object and then the

property of logic and then we assign a

value and this is okay but if you have

more than than four properties let's say

you have 20 or 100 properties that will

be very tedious to write each one of

them and then assign a value

and a better way is to use what is

called a Constructor and as the name

implied The Constructor is used to

construct the object and the Constructor

is called at the time when we create a

new object so at the time of line we we

type this line of code when you finish

to type this line of code The

Constructor is called immediately so

that is the

the time where we need to pass the

values to our properties and to do that

we go down here where we Define our

class

we put

at the at the end of our

end of the name of the class we put

parenthesis and here we Define some

parameters the way we Define parameters

for functions

and the first one is going to be called

name it's going to be a string

the second one is going to be model

also string

the next one

color also string

and the last one is going to be doors

and this is going to be an integer

no we need to

assign whatever value values pass to

those parameters to the properties of

the class here and to do that just

delete the empty string here and type

name

and as I'm typing you see that we have

name and it has this p and this P stands

for prop for a parameter so you're

saying do you mean this parameter that

we Define up here and then we press

enter to fill that for us

then put we type here model

color

and then

doors

now if you go up here we have some

errors because now the the

when you create the object the objects

is expecting for its Constructor some

values so if you hover over here inside

the parenthesis it says no value fast

for parameter color Doors Model and name

so if you and if you look down here you

see that it has this public Constructor

so now what we need to do is to pass the

value that you passed down here to to

the properties by calling the

calling them on the object is to pass

them directly in the Constructor and

then the values the arguments if you

want are going to be assigned to the

properties so I'm going to put here

and you have this hint name Tesla

then we put a comma

so we have the again the hint which is

very helpful so for the model we put a

splade

and then for the color

we put red

and for the number of doors four

all right so now this is more concise

and now this is doing the exact thing

that we did previously but this is more

concise and we're using the Constructor

to pass the those values to the

parameters that we Define here and then

those parameters going to be assigned to

the properties respectively with those

properties name model core and doors

you're gonna see what is the difference

between the properties and parameters in

uh in the next video now we have also an

error here because

this also express some values to its

Constructor so instead of putting those

values here we put up here

Ford

Mustang

blue

and

two

now we can delete this

because we already passed the values for

this second object for its properties

now if you look at the codes the code is

more concise and more and easier to read

now if you run this code

you will have the same output

because the code

works well

so now if you look at the console let's

increase this a little bit

let's scroll up you have the name Tesla

Model let's Place color red doors4 the

cartridge lights moving the cut Isla has

stopped so you have the same output as

previously and down for the second

object we have

we have a

the name Ford the model Mustang the

color blue Doors 2 and the Carphone is

moving in a car 4 has stopped so our

code works well now let's close the

console but if you go down here you'll

see that you have this um

this uh the properties colored so if you

hover over them it says the property is

excretely assigned to parameter name so

it can be declared directly in

Constructor

now what is saying here in other words

is that we can put the properties

directly in the Constructor so you don't

need to define the properties down here

and then assign the value that is passed

to the parameter to the property and

the difference between the properties

and parameters parameters is that the

properties respectively those that we

Define in the in the header of the class

inside the parentheses 0

in the Constructor

is that

the properties are not uh storing the

state of of the class they are mastering

the state of the object there they are

only here to to get whatever value we

pass here

so whatever value you pass here

and his job is to only get that value

and assign it to the property name so

only the the variables which are

declared declared inside the class are

storing ultimately the the state of that

object so uh if I delete let's say if I

delete this here

now we have an error here because the

property name um it says on our result

reference name create member property

card.name and that is because the

property name is not declared it's not

exit it's not it's not a it's not uh

it's not a characteristic of a car we

didn't Define a property name because

here the parameters the parameter name

is only there to get the value and then

pass the value to the property

but we can let's press Ctrl Z to put the

code

back

now the error disappeared because we

have the property declared but because

the only thing that we do here is to to

to get the value that is passed to the

property as an argument here and for the

next ones and then assign it to the

property we can put those directly in

the Constructor so to do that we just we

can hover over here and

let's hover over

and click on this light bulb and click

move to Constructor so now you have this

you have the same

the same thing the same thing but the

only difference is that we have this VAR

keyword in front and that is responsible

for changing the

changing the parameter into a property

so

let's do the same thing for the next

ones

move to Constructor

and now we don't have that error that we

don't have a property for our class

because never because they have the VAR

so if you put the VAR or the VAR keyword

before declaring the the the parameter

it is going to be converted into a

property

so now those are properties of our class

car so they are storing actually they

are storing the the value that you pass

to to them

for the project that we create so uh

it's not like previously where you have

the property the parameter and then the

value is passed to the property inside

the class

so now if you run this code

foreign

we have the exact same output

the only difference now this that this

is uh more concise we have the

properties declared the right directly

in the Constructor so we don't have

declare the parameters and then we

assign the value that is passed to those

parameters to properties so you can

declare the The Constructor like this if

you want so the question may arise when

to use properties and when to use

parameters and to to

to answer that question

um you have you need to have in mind

that if you want to do some if you want

to do some work before you assign the

value to the property

because in our case uh the value that is

passed here to the to the

property in this case is directly stored

in the property so we cannot add some

validation to check to see if uh I don't

know if this name is if it starts to a

specific letter or if it's if it's an

uppercase or because the value that is

passed

that is passed as an argument to the

property there I say property because

they are there there are no declared as

properties it is directly stored as uh

as a the state of the object so

if you want to to do some work before

you assign the value of the property you

need to declare it let's say let's say

that you want to Let's actually delete

this and I'm going to declare it as a

parameter so let's say that you want to

to to trim the space that is passed so

let's say that I put some spaces here

let's actually press Ctrl G to see what

is happening if I don't do that

all right

I press Ctrl Z too many times so

let's let's again Paul let's put some

spaces here so now if you run this code

let's run it again

[Music]

now because the value that is passed

as an argument here to the object car

one

is passed directly to the property so

it's stored in the property and we we

don't do nothing with it we have also

the space but if you want to to delete

that space currently we can do that

because the values pass directly to the

property and there is nothing that we

can do from that point

and to to change that we can declare the

the property that is defined here the

property name as a parameter first

and we can declare it down here

we can declare down here now the

property

and we can put equals and now we we pass

the the

parameter to the property so it's a sign

name here

but before before that value is assigned

to the property name we put that

stream

and that is going to remove the spaces

so that Rim is a function which is part

of the string class because the the data

the all the data size that we looked at

are also classes so you can call

functions on them the way we called

functions so not our on our objects

so name that trim removes the space so

in this case it removes the space that

we have here

and after that it's assigning the value

to the name property so now if you run

this code now now we have the space but

if you run the code

so let's go up

run the code

now as you can see the space is removed

because

we pass the value to the

parameter now name then the parameter

name trims.space and after that it's

assign the value to the property name so

in this case so in this can be a

scenario where you can uh where you can

declare the instead of declaring

directly here the property can declare

as a parameter but what do what what you

can do if you want to add more code than

just one line of code in that case we'll

need to use what is called the

initializer blocks but

um we're going to look at the

initializer blocks and uh other things

in the next video because this video is

long enough so see you in the next video

so in the last video we looked at

Constructors and how you can use

Constructors to construct your objects

and it was an alternative to defining

the values to the properties of the for

particular object

and we've also looked at how we can

Define the this Source I should say it's

also called the primary Constructor that

we have here the parenthesis and then

the parameters or the properties

and we've also talked about how we can

Define parameters or Constructor and

then how we can convert those parameters

directly into properties and I've also

talked a little bit about how and when

to use

parameters and properties but in this

video we'll continue our discussion

about parameters and properties and

specifically going to look at how we can

execute more than one line of code when

the object is created because

now when the object is created and we in

the last video we trimmed this space

that we have here

you can execute only this line of course

so if you want to add more line of

course here to do some

um to do a more complex validation we

can't because here we can put just one

line of code

and to add more than one line of code of

code they need to use what is called

initializer blocks and the initializer

blocks are as you'll see some blocks

where we can put our code and you can

put more than online so forth you can

put as many lines of code you want and

that code is going to be executed when

an object is created on an instance of

our car of our of your class is going to

be created

so um this is what we're gonna do in

this video also I should say that you

can declare the class in another file

not just in this file like we did here

so we can open now the project pane

you can go to SRC go domain

go to kotlin

and here on kotlin you can right click

select kotlin class file

and from here make sure to select file

and I'm going to call this file classes

because you're going to put our classes

respect we're going to put our card

class but you can put more classes there

and uh

we're just going to copy this class that

we defined here

Ctrl C

and paste it here

now I have an error because this is

declared two times here and here and we

just delete it from here

and now the arrow disappears

and if you run this code

we get the same output because our code

works

perfectly well now the only difference

is that now we will put we put our class

in a different file and my suggestion is

for you is to put the class if you want

in a different fly file

because in this way you have the code in

when I'm going to organize the way

because here you have your class here we

have the code the objects that we

created with that class

and so on

no

for this video I'm gonna use I'm gonna

create a different class and

to do to do that I'm gonna declare

another class but I'm going to declare

it down here and I'm going to delete the

code that we have here

because we're going to use a different

class

and the class is going to be called user

so it's gonna represent the

the use and end user object so we type

here class

user now we create the Constructor the

primary Constructor we put parenthesis

we Define the parameters or the

properties for uh now I'm going to

Define directly the properties here so

we type here

keyword to have the property declared

VAR name

string so it's going to be we're going

to be store stacks

VAR

last name also string

and uh

VAR age this is going to be an integer

so this with this class we're going to

create some user objects I'm going to

put curly braces

and for now we're not gonna put any code

here so let's create a user so let's

type here Val

or let's call it user you can give it

whatever you want

so let's create a new object now so we

put

you type your user and then for the

parenthesis because I'm going to put

we're going to pass the values to the

Constructor here Sony to pass a name I'm

gonna type Alex

last name

no binka

and for the age 23

so now we created a new instance a new

object using the using the cluster to

Define below

but what if I want to to pass to to the

to the name

to the name name property only the the

names which starts with the letter A

in that case we need to declare this

property as a parameter first we need to

declare here the parameter and then to

declare the property below and before

before we assign the value that is

passed to to the property we need to do

some validation and if that argument

that is passed to the

parameter is starting with the ale

letter then you're going to assign the

value to the property name otherwise we

don't do that so to do that we need to

use initializer blocks because we're

gonna type more than one line of code

and the initializer blocks are used in

conjunction with the primary Constructor

so when we create a new object like we

did here the initializer blocks or the

initializer block is going to be called

immediately when the object is created

and to use an initial initializer block

we just but first let's declare let's

change this to to a parameter so let's

delete the VAR keyword in front of

the variable so now we have only a

parameter here now let's declare the

property down here

and now here we need to Define an

explicit type declaration so we need to

put here the type string because as I

said

um we're not going to assign the value

to the property directly here in one

line of code we're going to assign the

value to the property inside the

initializer blocked after we do the

validation respectively after we check

to see if the name that is passed to the

objects that we're going to create with

the class

starts with the letter a and if the the

name if the argument doesn't start with

the letter A then we're going to assign

an empty string and we're gonna output

something to the console so this is why

we need to provide here the type because

I'm not gonna

assign the value directly here so it

cannot infer what kind of data is going

to store

then you press enter

we type init

and here we have a suggestion we have

the unique keyword and then we have the

curly braces press enter

and now here we're gonna add our

validation and here we type if

parenthesis

now type name so now we're referring the

name parameter defined above here so if

name or the value that is passed to the

name parameter

first we're gonna

um

lowercase dot name

and to do that we type here name dot

lowercase because I want to have both

cases so if

if the user passes let's say a name of

the lowercase letter then we're gonna

check to see if it starts with OTA but

if the user passes the the name of the

an uppercase later we also want to check

to see if if that letter starts with a

and to cover both cases we lowercase the

input we lowercase the name so all the

lowercase all the letters then we put

here another Dot

starts with an input here

a

and if that is true

then we're gonna assign whatever value

was passed to the name parameter to the

name property

and we cannot type here name equals name

because now it's referring the

parameter here and to refer the property

that we Define here we need to use what

is called the disk keyword so the disk

keyword is used to refer them

properties of the class so it are here

this

so we're saying this name so now we have

this name and we have a different we

have this V variable here

then put equals

name so now we assign the value that is

passed to parameter only if that

valuable only if that value pass to the

parameter starts with the letter A then

we're gonna assign the name to the

property name

else if that is not true

put curly braces

and now we need to cover the else part

because if this is not true we also need

to pass a value to to the name proper

the name property cannot stay without a

value so we need to pass those over here

else we're going to put this

dot name

and I'm going to put

user so I'm going to put the user

text if the if the the name doesn't

start with the with the letter A

and we can also output some text.com so

we put println here

and we'll type the name

doesn't

start

with

the letter

a or

capital a

so now if you create a new object here

so we type here Val let's say for friend

we create a new object so we type user

now we need to pass the values to the

Constructor so you put here a let's say

uh

John

let's put

Smith

let's put

30. now because the the value that you

pass here to the parameter name doesn't

start with a letter a and start the

letter j

this value validation inside the

initializer block is going to be false

and then it's gonna it's gonna execute

the else part and it's going to assign

to the property name the user text and

it's going to Output the name doesn't

start with the letter A or a but if that

is not uh if but if it's true if it

starts with the letter A then this line

of code is going to basically the base

part n is going to be a sign it's going

to assign the value to the property

so now if you run the squad

we get the name doesn't start with the

letter A or a and it's correct our name

starts with the letter j

so if I put here let's say

Andhra

let's change this to

oh let's see here also

Smith

now if I run this

now we have no output because the this

the initializer block is is called

and the If part is evalu is to true and

it's assigning the the name

Andra to our name property so let's add

the print and then down here to see that

that is correct so print

let's print the value of

of the property name for our friend

object and we put here

and here we type

dollar sign

you put curly braces and you type friend

Dot

name

so now if you run this let's close

get name under let's put comma here

let's put

colon here

so our initializer blocked is called the

code inside the initializer block this

execute it and then this if check

average to true and then and then it

assigns the name Andra to our property

name but if we I put here John

let's type it like this let's say to see

that it's working because we lowercase

the letters

okay the name doesn't start with the

letter A or a name user so the

initializer block is executed

immediately when the object is created

and

the code incidential log is called it

checks to see if it starts at the lower

it starts with the with the letter A

and if not then it is assigned to the

property name user so we have this we

have user here

and then it outputs uh

this

text to the console so this is how we

can use the initializer blocks in

conjunction with the primary Constructor

and you can also put more initializer

blocks and the order in which you put

the initializer blocks matters in the

sense that the word in which you define

the initializer blocked is is going to

be the word in which they are executed

so the word in which you define them

it's important but most of the time I

think you'll you use only one

initializer block to put your code so

this is our discussion about initializer

blocks and in the next video we're going

to look at um

because

we talked about the primary Constructors

there is a another type of Constructor

which is called the secondary

Constructor of this secondary

Constructors

and we can also look at the default

parameters because you can put you can

we can Define default values to the

properties also the parameters of the

Constructor so we're gonna do that we're

going to do that in the next video

I'm gonna end this video a little now

so now it's time to start a discussion

about secondary Constructors because

besides our primary Constructor that we

Define and that we talked about in

Cotton you can Define multiple

Constructors and those are called the

secondary Constructors

now you may be wondering why you will

need secondary Constructors

and to answer that question we need to

consider the user class and those user

objects that you can create so let's say

that an user

only provides its first name and for the

property is last name and the age

doesn't provide any value in that case

you will need the you need a way to to

Define some default values for those

properties in case the user only

provides the first name

and uh

if let's say the next user does provide

the the first name and the last name and

the age in that case you're gonna use uh

the the primary Constructor but if it

doesn't provide those uh those values

then you're gonna use the secondary

Constructor but to make this more clear

lists uh let's add the code and we talk

we talk about was while I am writing the

code so let's delete the initializer

blocks that we have here because you

don't need this code

and let's also put the property now

let's define the property in the primary

Constructor so let's just put here a

VAR the work here in front of the

variable

let's also delete this printalian

and um

let's delete the values here

for the last name and the age so now as

you can see we have an error because

it's expecting here the

um it's expecting an argument a value

for the last name and for the age and to

solve that problem we just go down here

and we type Constructor so this is the

keyword for declaring the secondary

Constructor

and we press enter to fill in that for

us and we put parentheses and now here

in the parenthesis we Define the

parameters and I am specifically saying

parameters because as you will see the

secondary Constructors cannot declare

properties they can only declare

parameters because

anyway Let's uh so let's define here the

parameter so I'm gonna type here name

let's put here the type of this variable

is going to be a string

now if you hover over here

it says primary Constructor called

aspect insert this call so what all the

secondary Constructors need to do is

they need to call the primary

Constructor because ultimately the

primary Constructor is the one which

declares the property the properties for

the class and stores the values for uh

of that Pro of that property and to do

that to put here

colon

we type this

so we put colon this type that this

keyword then you put parenthesis

and

to the disc call we pass the name so

this name that we defined here

and for the next two values we're going

to pass some default values so here I'm

going to put user because let's say that

because as I said in the beginning this

user is gonna not it's not gonna provide

the last name so let's put your last

name not user last

name

and for the age let's put

let's put let's say zero

all right now

what this is going to do is now if you

look up here don't have an error so

let's uh so now we don't have that error

because now it's using the secondary

Constructor is passing the value here

Alex to the secondary Constructor that

we Define here and here we Define only

the parameter and the value that is

passed as parameter then

is passed to this to the primary

Constructor because by putting colon and

these and parenthesis we're calling now

the primary Constructor

and is the primary Constructor the one

which

ultimately declares the property and

stores whatever value we send to him in

this case we send

the value Alex and for the last name we

uh we just send the last the text last

name and for the age you put 0 because

this is our way to saying that this user

doesn't want to provide its age so this

is why you need to call the the primary

Constructor from the secondary

Constructor because again the secondary

the primary Constructor is the one which

ultimately declares the properties of

the classes the one which stores the the

the properties of the class the

secondary Constructor only

only takes parameters and then calls the

primary Constructor so in fact if you

put here the VAR keyword

you have an arrow which says

Varon secondary Constructor parameter is

not allowed that is because the

secondary Constructor cannot call it

cannot declare properties

only the primary Constructor

and because as I said you can declare

multiple second Constructors we can type

here

Constructor so we can declare another

secondary Constructor and here you can

put

name

here we put string

then we type last name also string so

this secondary Constructor is only going

to take

the

name and the last name as arguments the

age is not gonna this uh is gonna not

gonna take any value for the age now we

need to put colon

we type this parenthesis because now

we're calling the

primary Constructor and here we we pass

the name that is passed to this

secondary Constructor this that we

Define here

the last name

and for the age we're going to put 0

because this uh

this second secondary Constructor

doesn't

take any value any arguments for its uh

for its age

but because as I said ultimately the

primary Constructor is the one which

defines the properties we cannot we

cannot call the prime Constructor and

not pass any value so we pass this

default value here

and uh

so now if you go up here and let's say

that I delete the age

now our code works fine because now what

is happening here is this the the value

for the

for the name and the last name is passed

to this secondary Constructor this one

here

and the secondary Constructor then calls

this primary Constructor and it's

passing the two values to the Primal

structure name respectively this name

John and last name Smith and for the age

it passes the default volume zero so

this is how you can this is why we can

pass here uh

this this is why you can avoid here to

pass let's say uh you can avoid to pass

the last name and the age for the first

because it is using the first secondary

Constructor

and this object that we created here

it's using this secondary Constructor

because here we're passing only the

first name for the value for the first

name property and the value for the last

name property so it's passing them to

this secondary Constructor which then in

turn calls the primary Constructor it

passes them to the primary Constructor

so it passes the

the values for the two properties and

the last one is defaulted because

okay because you don't provide the value

for the age

with Pro with secondary Constructors you

can also put you can at the end of the

call of the primary consultor you can

put curly braces

to have some code executed when you

create an object with that specific uh

secondary Constructor so you can put

here curly braces and inside the color

blades you can have some code which is

going to be executed you want to create

an object with this let's say secondary

Constructor

an object with this secondary

Constructor

so it's up to you what the code you put

there but to illustrate this let's put a

println statement here in the

in this secondary Constructor so let's

type here println and here I'm going to

type

second and I'm going to type second

because uh

this is the first Constructor this is

the primary Constructor and this is the

one which ultimately does all the work

is the one which stores the value for uh

the properties

and this is the third

Constructor so let's copy this

let's put here to Third

just for illustration

Also let's output in the console the

value of the the property values for

these objects so let's type here

printerland

and just type here name

equals let's put dollar sign

let's put curly braces

let's use our user

variable now let's call the properties

on this public so let's type name

now let's press Ctrl d

two times and let's put here now

last name

and the age

and let's also other space between

the information about the two objects to

make things more clear so let's put the

printer and heroes and put a backslash

in here

and let's copy the code that we have

here let's paste it below

let's change this

object to our friend object that we

created just like your friend

and I'm gonna speed this now a little

bit because

it will take some

now let's output this information in the

console

so now what we have is we have

we have

second and then third because this first

object is using the

the second Constructor

so this is why we have here a

second

and then the next line of code is called

and we have third because this one this

object that you have here the front

object it's using the third Constructor

this secondary Constructor so this I

have here third

then we have the values for the

properties for the I didn't have the

values for the properties of the user

object so you have

here the output

down here

and

then we scroll down and have name John

last name Smith age

zero because we have a

the property values for the second

object for this object

and this is okay this works completely

fine the thing to have in mind is that

now we're using the secondary

Constructors and the secondary

Constructor then calls the primary

Constructor and the primary Constructor

is the one which ultimately stores the

value source which ultimately declares

the properties and stored the values

but one thing to notice in the output is

that the first object has the name

Alex and the last name last name and age

zero that is because the first object is

only

taking a value for the first name

property or for the name property

and for the last name and the age

because it's using this secondary

Constructor is only passing in the first

name which is Alex that we have here

and then it's using the default values

last name and a zero it's passing those

default default values to the primary

Constructor so it's calling the primary

Constructor is passing the value Alex

and then the default value last name and

zero this is why I have last name and 0

here and for the next one we have

name John and last name Smith this is

because now this is using um the next

secondary Constructor and that takes as

arguments the name and the last name and

then it's calling the primary

Constructor it passes the

first name and the last name and the

aegi is defaulty so you have zero so

this way you have here John and Smith

and then zero so it's important to

to notice this and to think about how

this uh to think about this thing

so this is our discussion about multiple

Constructors respectively about

secondary Constructors in this video you

saw how you can

pass a different number of values to our

properties ultimately you pass all the

values to the properties by calling the

primary Constructor and providing

default values because in this way you

can satisfy all the possible cases so if

in our case one user only provides the

name and the last name is not provided

and the age is going to use the this

secondary Constructor and it's going to

pass the last name as default and the

zero and this is uh in a way

similar to overloading functions but is

not the same because what you're doing

here is overloading the constructors and

this in this way you can you can think

about

why why we're overloading the

constructors and that is because

if we didn't have secondary Constructors

we would have to create another class

with a different number of parameters

or the different number of

properties and we will need to to create

each object with that specific class

let's say that we create this object

which only takes a value for its

parameter name then we have to create a

class which we're going to Define only

the name property and it's gonna then

we're gonna create that obviously with

that class

and if an object if an object

let's say if an user creates uh if your

user doesn't

providing the the name and the last name

but now the age you have to create

another class with only the name and the

last name defined as its properties and

you can see that this this uh is uh this

will generate uh duplicated code and so

on and we can solve all of this by using

secondary Constructors and

calling the

primary Constructor so see you on the

next video and because quoting is about

conciseness in the next video we're

going to look at how we can do the same

thing that we have here with default

parameters because

um

kotlin supports for its properties for

its parameters in the constructed

default values so the same thing that we

have here can be achieved by providing

default values to the properties so see

in the next video

so now it's time to look at default

values for our properties and first

let's delete the secondary Constructors

and now I have a

error series because it expects the

value

expecting the values for the next

properties here

and um

let's also change this to

to first name because I refer to it to

the first name but it was called This is

it is called name so let's call it first

name so go right click on it

go to rename and then to refactor

to change it in all the places and let's

call it

first name and as you can see now let's

change it in all the place then press

enter

so now we have the first name and the

default properties are default values to

our properties are basically they are

similar to default values that we

provide to our parameters when we talked

about functions

then what we do is uh

let's say that I wanted to provide the

default value for the last name we just

put equals so inside the primary

Constructor we put equals and here we

put let's say

last name so now this has the last name

as a default value if a value is not

provided for the last name and here you

can put also equals

n equals zero and now the error as you

can see disappeared from both of our

objects because now

what is doing is when we don't provide

the value for the

last name and for the age then the

default values that we provide here is

going to be are going to be used as the

values for the properties so this is a

way of achieving the same thing that we

achieved previously with the secondary

Constructor but this is more concise

and the question is when to use one on

one or the other is

the um the answer may be if you want to

execute some code when an object is

created let's say

with one of the

let's say if you wanted to if you want

to exit some code when you create an

object then you can use secondary

Constructor because as I said secondary

Constructors can have a block of code

when uh which can be executed but the

this uh the default because primary

Constructor does not don't don't have a

don't have a block of code they you

cannot do that maybe you could use a

initializer blocks

but that is uh open to discussion

so this is how you can provide the four

values to

to the primary Constructor

in our code was fine it's

In This Very concise very beautiful and

the next thing that you can do is that

you can also use named arguments so you

can use the because here you use the

first name and the last name and if I um

because those are those are both strings

here you cannot see the distinction but

if I put here uh now I can use the name

argument so I can put your first name

equals so it's similar to the way we

have name arguments in our function and

here you can put the text

John

also here and this uh

with this thing you can you can change

the order in which you need to define

the values for the parameter so let's

put your last name

but we'll say this better let's create

another object

is

user 2

let's type user

and let's put here you wanna

but let's use the name arrowman so let's

put first the age so we're going to

provide the value for the age let's say

um

19.

and um

Let's uh let's type first name so here

let's put um

IWANNA because it's a string

quotation marks

and for the last name let's say

let's type also the name argument last

name and for the last name let's put

here um

Dobby

let's say

right so what we did here is that

in the primary Constructor the order for

for the properties very specific we have

the first name and the last name and

then we have the age but with the name

arguments we first provided the age

so we provided a value for the age

product which is the last one so and

then you provide the value for the first

name and with the name arguments similar

to the way used in our functions you can

pass the values to the properties in

which order you want irrespective of the

word in which they defined here so this

is very powerful if you

if you ask me it's very interesting

because you can pass the values to the

properties irrespective of the world in

which they are defined

so this is our discussion about default

parameters and the name argument in

with the primary Constructor and you

cannot use default parameters name

arguments with the secondary

Constructors they can only be used the

primary Constructor

so see you in the next video

but uh before I end the video Let's

actually run this code to see that it

works in the same way it was working in

previously when we use the secondary

Constructors

so let's run this code

so you have name Alex last name last

name because it's using this default

value that defined here then we have h0

because it's using this value of defined

here so this is

this object

and then we have name John last name

Smith a0 because now it's using this

default value so the same thing can be

achieved using um

default parameters and we didn't

output the values for the

for the second for the third object that

you created here in user 2 but you can

do that as a

as a challenge for if you want you can

output the values for this object so see

you in the next video

so now it's time to start a discussion

about Getters and Setters because so far

enough videos even though we didn't see

explicitly the Getters and Setters here

we've used the Getters and Setters every

time we got the value of a property and

every time we assign a new value to a

property

and um

the getter and Setter in kotlin are

implicit so they they are declared they

are Auto auto generated by default by

color by cutling for you so every time

you and let's say you get the value of

an object let's say let's get the value

of the user that's first name what

actually happens here is not that we're

getting the value of the first name

directly even though if you look at the

code this what it seems to be what has

happened is that every time we get the

value of of a property that value is

returned by the getter so you never

access the property directly and you

never never does change the the property

of uh the value of a property directly

you every you every time you you are

using the Getters and setters

and now this discussion is touching on

on a very important Concept in quadrant

programming and that is encapsulation so

encapsulation basically means to

to protect to hide the inner workings of

the class from being accessed outside

and if you look at this syntax that we

have here it seems that we actually

accessing the values the properties

directly but as I said that is not the

case because we're always using the

getter and the setter and because we

always use use the Getters and the

Setters the getter to get the value and

the set to to change the value of

property

that means that your data your

properties are never accessed directly

thus the laws of encapsulation are not

broken the the in other words you you

never allow somebody outside of the

class to access directly that property

so this is why uh

this is why we have Getters and Setters

and this is why the Getters and Setters

in Kathleen are implicit so they are not

even declared explicitly here we don't

see any

code which it will imply that we have a

get a Setter here because they are

automatically and by default generated

every time you define a property so

every time you define a property either

in the primary Constructor or

inside the class the

Getters and the get the getter and

Setters automatically generated for you

but uh let's say that you want to

override the getter and Setter to

provide some additional

additional code to the code that is by

the by default provided you can do that

by changing let's say

this property to a parameter

and to override the getter in Setter and

to see how the Getters and Setters

actually look under the covers

just type here VAR

we Define the property first name

we assign the first name parameter

property now the Getters and Setter need

to need to be overrated

immediately after the with after we

Define the property so we cannot have

some code here and then to override you

can to override the getter and Setter so

we need to

we need we need to Define them

immediately after we we Define the

property so we type here get

and as an attempting you see that I have

some suggestions then you go down here

you and you can choose you choose this

one

so you have parentheses curl braces and

inside the curly braces you press enter

here you type return

field I'm going to explain immediately

what is this

and now we need to override the setter

and to do that to just type set and I

have some suggestions

we also go down here we choose this one

press enter and hero type

field again this

under this identifier and input equals

to value

so this is the implicit code that is

generated that is auto-generated by

kotlin for you every time you you you

define a property in your class either

in the as I said in the primary sector

or inside the class

so because in fact if you look here we

have some underlines and if you hover

over here it says redundant getter

remove redundant getter because uh and

also for the set it under setup because

they are redundant because they are

implicitly again Auto generated by

cotton for you so this quad is redundant

here you don't need this code because

Scotland already provides this code

under the covers for you but if you're

not such satisfied with this quad with

this implicit code that is uh Auto

generated by cotton can you can um you

can override them like we did here and

provide some additional code so we can

put here let's say some text

so so that every time we get the value

of our

we get the value of our first name

property you're gonna have a pretty

pretty prefix and let's put here a first

name

let's put the colon dollar sign

and field we're gonna play I'm gonna

explain immediately what this field is

but let's say first however

how this works and here let's put a

printerland to print a value that is

passed to the setter and then assigned

to the first name property so let's put

here dollar sign

value was

assigned

to First

foreign

now I have name so we have our text that

we have here name and then have first

name Alex let's actually delete this

text to make things more clear

let's delete this text

let's delete this

now let's run this code again

so what we have now is that every time

we get the value of our property name we

have we have that prefix first name that

we have uh overwritten here in the in

the getter so the getter is called as I

said the value is returned by the getter

so together is called it Returns the

value of the first name property so this

field is is the first name property but

is uh it has this identifier field

because of uh of an important reason

which I'm gonna explain immediately so

it Returns the value of the first name

property

with the prefix first name and we have

first name the text that we have here

then I have the value of the first name

property we have Alex in our case

because our object has the name Alex as

the property name Alex

then for the

for the setter we don't have any code

here also we have the first name prefix

for John so our our every time we get uh

we get the first name property by

of our objects created by by using the

user class it gets that value it returns

that value through the getter and the

setter we don't have value was assigned

to first name property because the

setter is called only one we change the

value of uh of uh of our properties and

to do that we type user like we did in

our first video about objects or another

programming

DOT first name

equals let's say

oh

let's put here

Vlad

so now if I run this code

now the setter is going to be called

because the setter is called as I said

only when it we change the value of our

of our of of our property

so we get uh

get Vlad was assigned to first name

property so the code inside the setter

was called

because this code that we type here is

calling the setter so our code only only

always goes to the setter and the code

that we have here user that first name

Vlad what it's actually doing is

passing that value Vlad to

here so this for this uh this parameter

it that we have here is receiving the

value of Vlad and then that value is

assigned to first name property again we

have this field the identifier for a

reason and the reason is going to be

explained immediately

so uh

the Getters and Setters are working well

and they are as they said again the use

always when we get or

assign a new value to our properties

now what is this field identifier why I

didn't

type here uh

first name and here I didn't put uh

first name equals to Value so whatever

value is passed to the setter then that

value is assigned to the first name

property so why

why don't we don't do that

and um the reason uh for that is that

because

this syntax user.first name user first

name

user.first name equals Vlad is calling

the setter

if we put here first name equals value

so first name equals Alex it will be

equivalent to to call the setter again

so this line of code that we have here

it will call the setter again it will

pass the value to the center and then it

will assign the value again to the first

name so it will call the Center inside

the setter forever it will be it will

generate an error and to avoid the that

recursive call that infinite call we

need to use this field identifier so the

field in an identifier is the first name

property but it has this

special

use that it bypasses that error that it

will be generated if you use the first

name here

so in fact let's um

let's put here a first name to see

so if we put here first name

instead of field

and we have an error here

let's put here a

field

because

I want uh I want the error from the

setter to to get in the console not

there or from this thing here so now if

you run the squad

as I said that will generate a recursive

call it will call the getter over and

over

so get the error

so you see we have Vlad was assigned to

first name property Vlad was assigned to

first name property vad and so on that

is because

the setter

it's calling the setter forever

it's a recourse of call because this

line of code here it's calling the

setter it's it's a assigning the value

to the sending the value to the seta

then it's that values assigned again to

the first name property which in turn

close again the setter and so on so this

field the identifier is used because it

has this characteristic of

bypassing that error this is why we're

using the field instead of the first

name

property here and it's basically the

first name property but it has this

specific characteristics which

characteristics which bypasses the

recursive call which we got when we've

used the first name property

now if you run the code

now I have no error because the recourse

of call is not happening because you're

using the field which bypasses that

error

so this is why we're using uh and we're

having an encodone has this thing filled

and this the field can only be used

inside the getter or the setter not uh

not anywhere else also either getter or

the setter

and the same is true about the getter

this is why you need to use the field

identifier inside the getter also

because it will generate the recursive

call again

now to to illustrate better because you

maybe find the

confusing this discussion about Getters

and Setters what they really

are they are equivalent to having two

functions which

update the first name property and

return the first name property so they

they are equivalent to having

a function called Set uh

first name

and this function defines a parameter

called let's call it new value

is going to be of type string

let's put curly braces and here we type

this DOT first name

equals

new value

and the geta equivalent to having a

function

called

get

first name

and this is not going to take any

parameter is going is we're going to say

say explicit here that it's going to

return a string because it's gonna only

return a value

and this is going to return the first

name property

so we put here this

that first name so the getter and Setter

that we have here are similar this is uh

illustrative for you to have in mind to

having two functions which are setting

the first name property or updating the

first name property and are returning

let's change this to First

and are returning the first uh the value

of the first name property but because

constantly and it's about conciseness it

has this special syntax and they are

automatically generated for you because

uh as I said accordingly it's about

conciseness so you don't need to declare

those things every time you create a

class so let's delete this so have in

mind that under the covers is we have

something like this so let's delete this

now the arrow disappears

also you can have a shorter way of

writing the centering letter if you

don't provide some additional code so if

I delete this

and this

I can put here get equals field

and um

yes this is this okay because this is

like having a single line a single

function a single line expression

function because we're returning the

value to the function get so you can

write it like this because or I I show

you this photo because maybe you'll see

a written in this way and you may be

wondering why that's because this is

like having a a single body expression

function

so you can write it like this if you

want but again if you hover over this is

still already done let's so let's press

Ctrl G to have the previous code

so this is our discussion about Getters

and Setters and see you on the next

video

so now it's time to start a discussion

about the late init keyword and how we

can use the latent keyword with the our

properties but first let's delete the

code that we have from the previous

video because don't need this code let's

also delete this code

let's delete those two objects

that's

and lazil is property let's declare it

here so let's put the VAR keyword

and let's also delete the default values

here

and let's press Ctrl alt form on the

code

now we need to provide the values here

because we don't have default values for

the last name property and the age so

I'm going to type here

dobbyn let's say

age 23.

now

as you already know if you declare

inside the class

a property let's say that that property

is called favorite movie and it's going

to store the favorite movie of the user

you cannot let this variable

uninitialize so let's say that is going

to be of type string

so you cannot let this variable

uninitialize you need to provide a value

either by assigning directly

a literal value here or by receiving the

value through through a parameter

defined in the Constructor

but um that is not completely true

because

sometimes you'll want to to have a

property declared inside the class but

you don't have to assign a Twitter

valued uh right away so you want to

assign the value to the property later

and to be able to do that you need to

use the late init keyword and to use the

late neat keyword you just type here

late it and you have a suggestion later

need

and later need search for initialize

letter later so what we're saying here

is

initialize let later this property

favorite movies so you say in the to the

compiler hey I'm gonna

initialize this uh property favorite

movie but I'm gonna initialize it later

so believe me and uh

to do that you just

type user

that so you whatever object you create

with this class and you have a defined

latent property

favorite movie

and you put equals and then you put

let's say Interstellar here

so now we've initialized our favorite

movie property but we initialize the its

value later so we didn't provide the

value here directly inside the class

either by as I said by providing a

literal value or by

providing a value through the parameter

defined in the primary Constructor so

this is how you can use the

latent keyword if you want to

assign a value to a property later so

you don't want to assign the value

directly or or you don't want to cite

the the value more correctly said

right away so you want to assign the

value later you just put the latest

keyword in front of the property and

then you declare the property as

you the way you declare a simple

variable

right and if you don't initialize this

this property and you want a new output

the value in the console so let's say

that I put here a printerland

and I type user that

favorite movie

and I don't initialize it

and if I run this code now this

favorite movie property is not

initialized doesn't have a value

we're gonna have an error

and the error is uh

it's very specific

so you have exception thread main

kotlin unofficialized projects access

exception we have and then it says later

need property favorite movie has not

been initialized so it's very specific

in saying that hey you didn't you said

that you're going to initialize that

property but you didn't and uh down here

you try to to Output in the console the

value of the favorite movie but we don't

have provided a value and it's better to

have this uh this is called an exception

so it's better to have this energy Pro

and initialize property access exception

then having a null pointer pointer

exception because

this is why kotlin actually enforces

this behavior of uh

assigning values to the variable to the

variables so it Cotillion forces to

assign values to the variables and not

let them uninitialize because if you

don't initialize

uh you don't provide the value to a bear

to a variable or two property then when

you try to access that variable or that

property and let's say that you have an

app

that is gonna throw which is going to

throw what is called the null pointer

exception and that is very bad because

those kind of excursions are called only

at runtime and not at compile time so

your app can work let's say completely

well and when you press a button I don't

know to to see the the favorite movie of

a user then you get an error and adopt

crashes so this is why you need to avoid

those null pointer exceptions and kotlin

as I said enforces this behavior of

providing um

values always to the variables to the

end to the properties

and if you don't want to provide the

value to the

property then you can use this late on

it and it will show this uh different uh

exception called latent property

favorite movie has not finished so it's

it's a different exception than the null

pointer exception but with the latent

keyword we can still have as I said that

behavior of not initializing the

variable but you still need to

initialize it later because this one is

called late init

but this is not uh saying that the

latent

error that is shown here is not also uh

having the same behavior like the null

pointer exception that I talked about

because the late init error that is that

we have here

if you have an app it will uh also

Crusher up but the later neat keyword

allows us to not provide as I said the

value literally a little value here or a

value through the

constructed by the by assigning the

parameter here with the value pass of

the parameter

and the

is uh is our

responsibility to initialize that

variable later so that you don't get

into the same problems that we talked

about so we don't get this error so it's

up to you to

to declare a a property as late and

eaten then provide a value later because

if you don't it will also Crush you up

when you try to access uh by uh I don't

know in Europe somehow you want to

access the value of that property

so I always have a have a decent mind

that you need to initialize that

variable later

also I should say that the latent

keyword only works with

classes

and it does not work with primitive type

so even though the as I said the

integers and all the data types that we

looked at and also the

Boolean and Char are classes

we cannot use latent adhered with them

because if I put here in you can see

that I have an error

that it says latony modifiers not

allowed on properties of primitive types

because uh I'm not gonna get into much

of details but all we need to know to

know is that

when

the int class and all the Primitive

types which are classes uh at least

at the surface when they are compiled I

think they are compiled into primitive

types so they are not compiled into

classes so this I think is the reason

that you don't you can't use this latent

in the keyword but for other classes

because a string is is a data type but

it's a special data type as I said you

know in a previous previous video and

because of that you can use the latent

keyword and you can use the latent

latent keyword

with other classes so I can put here

user if you want

and it works because users is also class

is this class that we Define here

so it's good to to to remember this

now I'm going to end the video and see

you in the next video but first let's uh

press Ctrl G to have the code that you

had previously

so uh now I'm gonna end in the video and

see you in the next video

so now it's time to start a discussion

about companion object but to do that

I'm gonna delete the class that we have

here

and I'm going to paste it inside our

classes file because we're gonna need

this class later in our videos

and I'm gonna also delete this code

because you don't need this code

and let's say that

let's create the class called calculator

is not going to Define any properties

you know it's going to have a simple

function called sum which is going to

sum two numbers

so we're going to Define two parameters

here a

it's going to be an INT a b

also on it

n is going to return the sound so we put

colon at the end of our

parenthesis or function we put int

because this is what you're going to

return

and we put calibration type return a

plus b or you can put this in a single

body expression function so you can put

the code like this if you want

so we can put here uh you can also

delete a type you can put here equals

and this will work but I'm gonna

keep it like this

now if you want to use this sum function

that is defined in this uh

class calculator first you need to

create an object without class so we

type here Val I'm going to call it

calculator

and I'm gonna create here a new object

calculator

Constructor doesn't have any properties

any parameters and if you want to

to call that some function we need to

use our calculator

variable or I'll confrater object or

reference and we put dot like we did in

our research so use the dot notation so

now we're calling the sum function on

our calculator object that we created

online too let me put when we call our

function sum inside the buttons you pass

two numbers let's say 5 and 10.

and uh

let's capture this value that is

returned by the

by the function in a variable so let's

put here

Val result

because that is going to return the sum

of 5 and 10. of course that you can put

that in a printerland

statement if one but I put it in this

elaborate way to to make

more clear what you're doing and here

I'm going to type println

result

so now if I run the squad

you're gonna see 15 in output line in

the console

so let's increase this

so we have 15. and the way we did it is

we created an object like we did so many

times then we want that object use the

dot notation to curl that function on

that specific object on this object and

that function takes as arguments as easy

as

values to its parameters to integers we

pass 5 and 10 and then it returns as

well and we retain we

we capture that value in the result

variable and then we want to put that

value in the console and get 15. but um

if you think about the way we did this

thing is that the only way we can call

the sum function is by creating an

object and then calling the function on

that object but it doesn't make a lot of

sense if you have a calculator class

to call

to create every time through our code

every time you need to use the sound

function to create an object throughout

your code a better way it would be if

you could call that sum function

somehow without declaring uh without

creating an object and then using the

the object to call the function and to

you to do that we need to use what is

called a companion object and to use a

component object we go inside our class

we type companion object so we can press

enter to fill in a component object for

us or you can type that then we put

curly braces and inside the curl brace

you put our codes and in this case we're

going to put our sum function

now

you you're going to see something now we

have an error here

now this

function this sum function doesn't

belong to an object to an instance that

is created with the class calculator

instead it belongs to the class itself

and not to the not to any object that we

create

those the variables and the functions

that are the functions that are declared

inside the component object are also

called

class variables or

class functions because they don't

belong to they don't belong to a

specific object that you create with

that with that class instead they belong

to the class itself so you don't need an

instance to call that function and uh

that uh

implies that we can't use it with an

object because it belongs only to the

class and let's see how you can use it

using the class without creating an

object we just type calculator

dot sum and here you pass again

5 and 10. so

we just use our name or the name of our

class we put dot sum and now we can use

the function without uh then without us

needing to create an object and then

calling the function on that object so

this is a very powerful because uh

you will need this in your code

sometimes when you don't uh it it

actually doesn't make sense to create an

object every time you you call let's say

a sum function for a calculator

now we can also retain that value that

is written in a variable also result

put equals

now we can print this in the console

so we get also 15 but this time we

didn't create an object we didn't create

a new instance and we call that we call

the sum function on that object we just

type the calculator class and call that

function on directly on the class

all right so this is how you can use the

companion object

and this is also true for a variable so

if you put a variable here let's say Max

and you put an arbitrary number here

let's say 100

we can call this variable

also by typing calculator the name of

the class Dot and have our Max variable

uh like in contrast with how we did the

how you call the the properties in the

past by creating an object and using

that object to curl that property on

that object in this case you just use

the class and call the the variable and

this also can be can be useful if you

have some

constants to which you want to use in

our throughout your quad so let's delete

this so you can use also with variables

and if you think about uh

uh at our

one of our first lessons about the data

ties when you look at the minimum and

maximum value I I type something like

this Val Max

and I

to get the maximum volume I type int

that

max volume and this is using also a

companion object because as you can see

the integer class because the Primitive

types are also classes at least on the

surface

not when they are compiled on the jvm

they also use companion objects like we

have here to to get to to get the

properties and to get the functions

because you don't we didn't create an

instance of our integer of with our

integer class and then call that max

value on the object in fact if you hold

control on this integer class and if you

actually hover over it and after you

hold Ctrl it says

public companion object of int so if you

hold Ctrl and if you press and if you

press click now

it will open this primitives.kt file

and inside

primitive.gt if I have our end class and

we have a companion object inside the

companion you have this public constant

value we're going to look at the

constants in the negative but the point

here is that it's using the companion

object to to declare is declaring the

the this minimum value the maximum value

inside The Company audio so that you

don't need to create an instance an

object every time you need to use them

so

this is our discussion about compounding

object and see you on the next video I'm

going to close this file now

and if you if you want you can look you

can look in that Primitives file if you

want just take a look look what is there

so see in the next video

so now it's time to start a discussion

about Singleton but first i'm going to

copy this code I'm going to copy this

class with the companion object and I'm

going to paste it inside the classes

file

and if you don't have this code Discord

because you create a new project just

type the code that I'm going to type in

a second so let's delete the code inside

the main function

now what is a single then

a Singleton is a design pattern in

kotlin with which you want to have

only one instance of a particular web

object so you want to have only one

instance of a particular object and you

want to have that instance available

globally tutorial program so you don't

need to create an instance every time

you need to use that

that object in a different part of your

code

and there are

project practical reason for having only

one instance of a class let's say that

you have a database

you want to you want to have only one

instance of that database which creates

the database which gives the data and so

on because if you have multiple

instances if you able you are able to

create multiple objects with that class

then you'll have multiple databases in

your

phone or on our PC and that is not good

because you're using more memory than

you should and you have a duplicate

duplicated

duplicated and unnecessary code

so to solve that what we need to do is

to create only one instance of a

particular class and uh

having this example in mind this is what

we're gonna do next and first I'm going

to show you how we we did the

in the past how we created a Singleton

without having the object keyword so you

go down here

and first we're going to create a

Singleton in the world wave so we type

here class you're going to use our

example database not going to create a

real database we're gonna just use this

as an example

and to make to restrict the creation of

instances with this database class we

need to make the Constructor private so

we type private and uh when you use an

access modifier with the primary

Constructor you need to use the

Constructor keyword other otherwise will

have an error so if I delete the

Constructor keyword here

I have an error which says

after modifiers or Pro of primary

Constructors so we put Constructor

and inside the

inside the glass

now because we cannot create instances

of this class that means that we cannot

uh access any of the properties or the

functions of this class and to to to

have access to a function which is going

to return an instance of this class we

need to use a companion object because

with companion like this you can have

properties and functions being allowed

to being to be accessed outside of the

class without creating an instance

and here we type again

the private access modifier we call this

instance and it's going to be a database

a nullable database

should be valid there

instance

database and as I said nullable so we

can assign null to it

then we Define a function called get

instance

and this function is going to return an

instance so for instance of our database

class

only one instance one one single

instance not multiple instances

and to do that first to check to see if

the instance is null so if this is the

first time that we want to access

this uh

instance then it's gonna return true if

not it's going to return false so it

checks if it's equal to null if it's

equal to null so our instance hasn't

been initialized

you know type instance equals to

database

right so if the instance is equal to

nulled that means that the instance has

been initialized we initialize the

database so we can initialize the class

inside the class we cannot initialize it

outside

else

so if this condition fails the the code

below is going to be red else you're

gonna return the instance so if the

instance is not null and this uh this

code is not going to be a

this condition is going to be false and

the code will not be as a good inside

the if it's gonna we're gonna return the

instance but here you go you should

return the database

and it should be a knowledgeable

database

what is our Arrow disappeared let's

delete this

let's press Ctrl alt there

all right now how we're gonna use this

first

we type here

Val instance

input equals and type database Dot and

I'm going to use our function get

instance

and uh if we we print this instance in

the console so we type here instance

if you run the squad

so now we're calling the

fan gets instance inside the company of

object

we get this code that this code

represents the

location memory of the instance so we

have this code here and if we try to

instantiate this class so we put here

the Constructor if we do that then we

get an error because the Constructor is

private so we cannot create an instance

and

when you call the get instance function

so when we did this

first check to see if the instance was

equal to null and it was equal so now

then created the instance and then it

returned the instance

but if you call this

function again so let's call it

instance2

because database that

get instance now this will evaluate to

false because the instance is not equal

to null we have an instance and it's

going to come down here and it's going

to return the instance they're already

creating instance so if I

put a printerland here

and I'm gonna output the instance tool

you're gonna see that there is no

instance to you have the same instance

because it's you it's returning already

creating created the instance so we have

the same code here meaning

this translates to that you have the

same instance so no matter how many

times you use database I get instance if

it's the first time then it's going to

create the instance and it's going to

return in essence if you call that again

like we do for instance 2 then it's

gonna return the already created users

so in this way you have the same

instance no matter how many times you

you call the get instance function

because it will always return the same

instance because we

because of our logic that we created

here but there is a shorter way to

achieve the same thing by using the

object keyword and this is what we're

gonna do next

so let's close this

now let's see how we can create a

Singleton and which is going to have the

same behavior that we created here so

it's gonna be only one instance using

the object keyword so I'm going to type

down here object

I'm going to call it database

now let's delete this code because we

cannot have a

two objects which have the same name

and we cannot have a Constructor here

because it's a Singleton so you put only

curly braces

you can have properties and functions

inside the Singleton created with you

with the

object kios leslie.com inside Dimension

frame function too

so uh

Now by just typing object database now

we have the same thing that we had

previously just by typing object and

database so this will always return the

same

instance and we have only this code

object and the name of the object so the

in the instance of the object is going

to be created the first time you call it

and we can put an initializer block to

have some code to be executed here so

let's put a printerland

let's say yo

this is a database created

and to you to use this just type let's

put a printerland

and type database

and now if you run this

and the first time you call the

Singleton that time the Singleton is

going to be

it's going to be created so I have

database created then we have this code

which represents the

or Stringer is the string representation

of the object and if you press Ctrl D

here look what happens

we have

database created so because this is only

one instance this object is a single

turn it calls the initializer block only

once when it initializes itself so when

we call the database for the first time

it is initialized and it calls the code

inside the init block and after that it

only

it output this thing representation the

second time we call the database we

don't have database created because the

instance was already created and the

initializer block is not called again

because we have the same instance so if

you can press Ctrl D here multiple times

and we're gonna see that we have the

database created with only one

one uh once because

that is called only when the

only when the object is created for the

first time so on this line in the next

slide we have the same uh the same

objects we have here this

this is why I have the same code because

it's basically the same instance so as

you can see this is a lot simpler and um

more concise than the code that had

previously so this is how you can create

a Singleton in kotlin this is why this

is how we should do it

so see in the next video

so now it's time to start a discussion

about lazy initialization but first i'm

going to copy this Singleton and solid

hour classes file

and if you don't have this file because

you created a new project again just

type the code that I'm going to type in

a second so I'm going to delete this

now what is lazy initialization

lazy initialization is used when

creating an instance when creating an

object is expensive and the expensive in

the programming context means that it

will take some time

it will use some memory and in that case

it's recommended to use lazy

initialization and to illustrate the

lazy initialization I'm going to use our

user class so I'm going to compare user

class inside order

main file not because I can create here

an object by having the file here but

because

I want to

put the code here to make clear what I'm

doing so I'm gonna delete the code from

here

and if you don't have the file just

create a new class called user Define

the properties for its name last name

and age I'm gonna delete this because

you don't need this

and inside our user class I'm going to

put initialize an initializer block

which is going to be called every time

you create a new object and here we're

gonna put a printer printer print line

statement

which is gonna say

user

when I call the first name property so

the value that Sparks the first name

property

was created

so let's create a new object here

let's call it user1

equals user

now let's put some values to the

properties of the Constructor let's say

Alex

now if you run the squad

we get in the output user and the value

for the first name property Alex was

created

if I press Ctrl d

and I change this to user 2.

and let's change this to a generic name

user

for the last nameless

pass just the last name text

and let's skip that age so if I run this

code now

because now on the line 3 you create a

new object the initializer block is

going to be called again because we

create a new object which is user

user was created

now to initialize our user 2 with the

lazy initializer we just delete this

and to illustrate how the initializer

block works we delete this and we put

after user to buy so we put the keyword

by

and we type lazy and as I'm typing you

see that I have that black lace press

enter and put the curly braces and

inside the calibration we create our

object so user

and here to pass the values to the

properties so let's put

user one

let's pass last name

and for the age let's put

zero

now

previously when the

user was initialized

in the world way

I found out the user user was created so

the initializer block was called when he

created the second level but now

if I run this code

look what happens

we get only our first object we get the

initializer black only called for our

first object and for our second object

initialize using the lazy initialization

we don't have the initialize of law

because the object is not initialized is

the object is only going to be

initialized

when you use that um

object in your code so if you use that

object somewhere in a code the object

then is going to be initialized and it's

going to be used so if I type here a

println

which says

user DOT first name user tool

now if I run this

now because I know I'm I am using the

user 2 object now the user to object

instance is going to be created and then

I'm gonna see user 2. first name so now

we see user let's see user 1 was created

so the initializer block was called

because when we called the

the print line user to that first name

then in that at that moment it created

the instance and then it outputted the

user one so this is why now we see here

user one one was created and previously

we didn't because

now we're using the user to object in

our code so unless you are not using the

the instance the object that have

created using the lazy initializer it is

not going to be initialized so this is

initializer this is initialization by

lazy

so see in the next video

so now it's time to start a discussion

about enum classes but first i'm going

to copy our user class inside our

classes file

now I'm going to delete this class and

also I'm going to delete the code inside

the main function

now what are the num classes

in on classes are used when we want to

represent a fixed set of values so when

you want to represent we want to create

some constants you should use genomes

because

enums are like constants but they are

more powerful because you can they can

have properties and they can also have

functions

and you know I'm sorry

usually used in um if conditioners or if

statements or in in the when statement

or if I want to call it the if statement

expression or when statement expression

now let's see how I can create any

number class

and to create any num class just type

here enum the keyword enum

and as I'm typing I have some

suggestions

then we put class and let's call this uh

you know class directions

Direction actually

then you put curly braces

and inside the enumclass direction we

Define our enum values so here we Define

North

put comma enter

South

you can put them in one line just to

have comma between them

East

and west

right so we have our

enums values defined inside our

Direction enum class and those enums

values that we have here here they are

instances of our enum

class Direction so they are an instance

there an object created inside the

enumclass direction just that they have

this different name and as you can see

the name of the you know values should

be all the names should be in uppercase

letters now let's see how we can access

those you know values

to access the you know values we can

let's put the printer line here

we type Direction

then we press Dot

and we have some some suggestions here

we have west east north and south so we

have our enum values suggested so let's

choose West let's output this in the

console

so we get West so this is the

enum value

West and um

let's press Ctrl d

let's change this to East

so let's change this to East

let's change this to North

and South

now if you run this code

we get our enum values West is north and

south

so this is how you can get the you know

the how you can get the genome values

that you define inside your genome class

now we can also

you can also pass

you can define a Constructor here for

our enumclass and Define some properties

like VAR let's call it Direction

of type string

and VAR

distance

and this is going to be an integer

now if you hover over here you have some

underlines and the underlines if you

have always says that you Nam has not

the full Constructor so now because as I

said those elements values are instances

they are objects created inside the

original class now we need to pass some

values to

the properties that we Define here so

let's pass some values here so let's

press let's type here first

North again I'm gonna speed up this now

a little bit

all right so now what we did here we

passed some values to the

you know values Constructors because in

in up here in our enumclass direction we

Define some properties and those

properties because the enum values are

objects they are instances for a number

directions even though they have those

different names you need to pass to the

constructors then some value so we pass

here north south east west and password

for for the densest distance 10 20 50

and 40.

now

let's see how we can Loop through our

enum's values and to do that we just

delete this code

and what if and here we add the four

groups so we type four

let's call it the

Direction

in and here we type

Direction our enorm class

dot values and this is going to return

an array with all the enum values that

are defined in our class

now let's put calibrases

and let's draw the printerland

and let's put in the direction

so now if you run this

you'll see the same output

but in the order that they are defined

inside the class

so we see the same not the same output

because they are outputted in the order

in which they are defined in the class

so we have north south east and west and

this is how we can Loop through an enum

to through the enum values of a num

class so this is how you do it so now

let's delete this code

now if you want to access the arguments

that are passed to our enum values

Constructors

we just first put a printerland to

Output the values on the console

and type Direction so our rename class

Dot

and we choose our in our value let's

choose North and from here and here now

we type also that and now we have some

suggestions we have Direction distance

and we also have a name

and name is uh

is a variable which is not defined by us

so we've only defined those properties

direction and distance but name is

variable which is built in in the

enumclass similar to the the the

direction that values which return the

array is the the direction that values

values was also uh property which

actually it was an array property which

was built into the enumclass so we

didn't create that it's provided by the

enumclass so this is why we have that

name there so let's put Direction

let's press Ctrl d

uplicate this code and let's change this

to the instance

let's press Ctrl D again and let's also

print that name

which is the built-in variable which is

the built-in variable provided by the NM

class so let's put here name

now if we run this code

get North so this is the value that you

pass here we get 10 and then we get

North with capital letter so

so we get the value that we passed here

for direction and distance which are

defined in Direction class so we get the

value spots for this specific

you know more volume and then we get the

North and this is the built-in um

variable which is uh provided by you by

Dynam class

but as I said you can also have

functions so let's have a function which

Returns the direction and distance so

let's type here fun

we call it print data because it's gonna

print the data of a specific in our

value

it's not going to take any input put

curly braces

but now we have an error here that is

because uh when you declare a function

inside the nenum class you need to

provide a semicolon for the last in um

defined in our class so I need to put

here a semicolon and there will

disappear and let's print the data here

let's add the println

let's put quotation marks

as for Direction

equals

dollar sign

Direction

and

distance

equals dollar sign

distance

now if you call this wanna

our

wanna enam valueless type direction that

let's say West

Dot and now we have our function print

data

so this print data function is going to

put in the data of the West enum that is

defined here so it's going to Output

West and 40 in the console so you know

if you run this code

we get direction West and distance 40.

so this is how you can use functions

inside the noun classes

next let's see how we can use our enum

Direction and generally how we can use

enums with the when statement expression

so I'm going to close the console

I'm going to delete this code and the

print data function works for all of our

Renown values

so let's delete this

and here I'm going to Define a variable

it's going to be a Val it's going to be

called Direction

we're going to put equals and hero type

Direction our enumclass let's choose

East

so now the enum

actually is the west east so now our

Direction enum is is going to be stored

in our Direction variable let's use the

when statement expression

and we type here when

Direction

then you put curly braces and here we

type

Direction

Dot

East I'm gonna put

Auto

execute this course so print the Len

the direction is

yes and we'll do the same thing for the

next one so let's press Ctrl d

let's change this to

West

North

and South

so this is how you can use the enorms

with the one statement now if I run this

actually I should change the text there

but I'm going to change it

so let's close this to change the text

here

now if you run this you're gonna see

the direction it sees because this

condition is true

our Direction argument here is going to

match with this Branch so it's going to

evaluate true and then it's going to

execute this code you can pull

calibrations if you want but as I said

in our discussion about the when

statement expression

you should do scary braces if you have

multiple lines of code in our case we

have one single line of code but you can

put curly braces if you want and of

course that this Branch will always

evaluate to true because here we type

direction that is

into our Direction variable literally

directly and this value doesn't change

but in real up this value could come

from a database or from the user input

and in that case you need to use the

function which is also built into the

enorma class and that function

is called value of so we type Direction

dot value of and this value function

check his checks to see if our value

that we pass here exists inside our

enumclass so it checks to see if is

defined here and it all it also returns

that value so if I type here

yeast

now if I run this code

we get an error which has no num

constant direction that is because

all of our enum values have they are

defined in uppercase letters

but if I put here that

uppercase

and if I run this code

now we get the direction assist and uh

this value that we have here is this

value as I said could come from a

database or from the user input and the

direction that value of is going to

check to see if this

you know value exists in our rename

class and it's going to return that

value

in our variable so it's going to assign

that value to our variable

so see you in the next video

so now it's time to start a discussion

about inner classes so what are inner

classes in our classes are classes which

are declared inside another class and

they are generally used when you know

that there is a very close relationship

between two classes so when it doesn't

make sense to put two classes separated

then you should use inner classes and

let's look at an example and we're going

to imagine here that we have a list View

and this list video list view is gonna

display some items in a list in on the

screen and here we're going to use inner

classes because for displaying the each

individual item we're gonna create a

list view item so first you create a

class called list View

foreign

we Define a property called

items and it's going to be an array of

strings

now we put curly braces and inside the

class we type the keyword inner

then class and now we create a class

which is going to represent the

individual items in the list view so we

put type here list view item

put the

the primary Constructor but we put not

we don't Define any properties and then

we put curly braces

and this inner class let's use gonna

have a function called display

item and it's going to display the value

the values of an item of other specific

position so we put here position let's

say

Define a parameter to our function

it's going to be an end

and we're just gonna type here print

line

and the inner classes are have access to

the properties of the outer class so

inside our inner classes that we have

here we can have we can access the

properties that are defined inside the

outer class list view in this case we

have a single property called items and

here we can type

items and notice that I can use the

items property which is defined in our

outer class list view we type position

and this is going to return

an item at this position which is passed

as an argument here to to do our

parameter position

and we go up here and we type Val

list View

because we're going to create an

instance for this View

type list View

now we need to pass an array to our

primary Constructor to the property item

so we type here array off and we Define

some generic names here let's say uh

name

one

and name four now how can we access the

display item function which is inside

our inner class

list item list view items this this

should be list view item not items so

let's change this go right click on it

and go to refactor and rename

let's call it the items

press enter

now to access the display item function

which is inside the original class list

view item

we type list View

so our instance our object list view Dot

list view item and then again that and

now we have our function display item

and we can pass here let's say yo

let's say uh two

now if you run this code

so get in the output name tree and this

is correct because it entries the other

index too

and this is how you can access the

display how this how you can access

generally a function inside then uh in

inside an inner class you just create an

instance of the outer class then you

call a

you call the name of the class of the

name of the inner class and then you

call the function Auto so see you in the

next video

so now it's time to do a challenge using

the knowledge that we have about object

oriented programming

and the challenge is to create a class

which is going to represent a bank

account of a person and this class is

going to have three properties the first

one is going to be called the account

name the second one is going to be

called the balance and the third one is

going to be called transactions and it

should be a multiple list

also you should create functions for

deposit and withdraw and also function

for calculating the balance and you also

you also should add the

the checks for checking to see if the

amount is uh when you deposit the amount

is greater than zero and all of that

stuff so do this Challenge and then

watch my solution

so

my solution is this I'm going to create

the class

and for this challenge you should also

have in mind the discussion that you had

about access modifiers so I'm gonna call

it account

and I'm going to Define here a property

called the

we're gonna be called the Val is going

to be called the count uh

name

and it's going to be of type string

of type string

and we put curly braces and inside the

class I'm gonna declare the next two

properties and those one uh first I'm

going to declare it without the access

modifiers because IntelliJ probably is

gonna figure out that we need to put

access modifiers on them so I'm gonna

type VAR balance

and I'm gonna assign 0 so this is the

default value and

VAR transactions

and this is going to be equals to a

multiple

or Multiple List

of int because this this is going to

store the transactions

so these are the three properties of our

class now I'm gonna create the first

function which is going to be called

deposit and this function is going to be

used to deposit

money inside our bank account and it's

going to have a parameter called the

mount

it's going to be of type

end

and first we need to check if this

amount is greater than zero because you

don't want to deposit uh negative value

so if amount is greater than zero

and we choose greater than zero because

we also don't want to deposit zero so

this is why I put greater than zero then

if it is greater than zero then I'm

gonna add that to our transaction

list that add

amount

and then I'm gonna say balance

I'm gonna update our balance I'm gonna

put plus equals

amount then we're gonna add the print

line which is gonna say

I'm going to put the quotation marks

dollar sign

I'm gonna type amount

I'm gonna put dollar sign this dot

balance

so I'm gonna output in the console what

is the balance but if the amount that is

passed to this function is

a negative sum I'm gonna go in the else

part and we're gonna say

another print line here

which is going to say cannot deposit

negative sums so we're gonna put here

okay cannot

deposit

negative sums

so this is our deposit function next I'm

going to create another function and

this is going to be called withdraw and

this is going to be used for withdraw

withdrawing mine so we type fun with

draw

and we put parenthesis and this is gonna

take a parameter called with draw wool

so uh

it should be like this

and it's going to be an integer we put

curly braces

now

it should be with

with here with

with

draw

now we need to convert this into

negative sum because now we're uh

withdrawing money from autocon so first

we need to check if

and we're going to put minus because I'm

gonna convert this into a negative sum

withdrawal is less than zero

so if this is a negative

sum which is going to be withdrawal

you're gonna

I'm gonna add to our transactions is

transactions that add and you also put

the minus sign so you put minus with

drawal because now we're adding now

we're uh we're taking money off the of

our account then I'm gonna put this that

balance

my uh Plus

equals minus

withdrawal because now we're taking

money

and then we're going to add again a

print line it's going to be this one so

I'm going to copy this

but now this is going to be the

withdrawal

else if

this is not

a negative sum so

I'm gonna put else

I'm gonna type put in the line

cannot

wait

draw

negative sums so if he tries to withdraw

a negative sum then we're going to say

cannot withdraw a negative sum here we

put the minus because we we're taking

money out of our account so we need to

check for uh that's why we're checking

here if this is less than zero

shouldn't be negative here

and we're going to create another

function and this is going to be called

calculate balance so we type here fun

Let's uh

bring this

fun calculate

calculate

balance

this is not going to take any parameters

and it's going to return an INT

put curly braces

and I'm gonna put this that balance to

zero because

I want to calculate the balance I'm

going to add the transactions to this

property balance so here we're gonna say

and here we're gonna loop we're gonna

type four

turn suction

in our list of transactions

so

for transaction in our list of

transaction I'm gonna type this Dot

balance

plus and equals transaction

so our transaction and then we're going

to return the balance at the end of our

Loop when we end we ended looping so

return this Dot

balance so this is our solution to the

challenge and let's um let's create an

account and let's pass some values here

so I'm going to type here

Val

Alex account

I'm gonna put equals

account

I'm gonna put now I'm gonna call the

primary Constructor so I'm going to type

here Alex

and we still use I'm still using my name

here but

so I'm going to type here Alex account

Dot

deposit so I'm going to deposit let's

say one thousand

and uh

I'm gonna type OLX account that withdraw

I'm gonna withdraw

500 then I'm gonna put Alex account

and I'm gonna put uh

deposit the negative sign let's say

minus 20.

and Alex account Dot

that we draw also negative sign let's

say minus

100.

now if you're on this code

foreign

deposit balance is now 1000 so we have

our call card called from our deposit

function

then we have

so I should I didn't change the text

here so that's why it's saying uh

this should say with uh withdrawn so uh

sorry let's change this to

with

drone

so let's run our code again

so get 1000 deposit balance is now one

thousand now we get our uh correct

output to get the

then 500 withdrawn so get our call the

call from our withdrawn function we

withdrawn balance is now uh 500 then

then you get cannot deposit negative

sums cannot withdraw negative sounds

because we passed the negative numbers

to our deposits and withdrawal function

and here we're checking to see if those

are negative or positive so this is our

solution to our

to our Challenge and I hope that you

enjoyed this and that you solved this

and this is not again the best solution

this is just

my solution probably you'll find you

figure out a better solution

maybe your concern better and more

concise solution than this actually I

forgot before we end our video Let's

actually called also our calculate

balance function so I'm gonna type here

uh

Alex account

dot calculate balance and this is going

to return an integer so I'm gonna type

here Val because that function returns

something so I need to capture that in a

variable so I'm going to type here valve

balance

equals Alex account

so no other print line here

and there's gonna say

balance

is dollar sign

balance

format the code

now if we run this code

[Music]

now get our previous output to 1000

deposit balance now one thousand five

hundred withdrawn balance is now 500

then cannot deposit negative sums and

cannot withdraw negative sounds and then

get balances now 500 so our code works

well

so this is our code

one more thing that I forgot is that I

said at the beginning of the video that

you should have in mind the discussion

that we had about access modifiers when

doing this challenge

and I forgot to to add the access

modifiers for balance and transactions

because now what can I do for balance I

can

use our Alexa account

s and I can type here balance and now

because that is uh actually that is not

really probably because when I talked

about uh get us unsetters we said that

that when you access a property you

always access access that property

through get a run Center so we never

access directly the getter and the

property but uh

still you should not make that that

public because by default if you don't

provide an access modifier they are they

are by default public so if I type here

now account balance as you can see I can

call that and I can type here 1000 and I

can deposit now I can change the balance

of the account directly from here and if

you hover over here

uh IntelliJ figure out that you should

make this private so make this private

the balance and also the transactions

because

you don't use them inside the primary

Constructor and they are used only

through the to the functions that we

defined here the because of that you

should you should declare them as

private because you use them only

through the function that you declared

inside the class so declare them as

private and now you if you hover over

here it says you cannot access balance

because it's private in account and now

we cannot change the balance from

outside the class we can only

deposit or withdraw uh

sums of money from our bank account

by interacting only with the functions

and not by calling the property directly

and assigning a new balance to to the

property so make them private and this

way you cannot uh you don't uh you don't

access the property outside of the class

and you change data directly the the

value of the property again it's not

really direct because the code that you

had previously so this code actually is

calling the setter here but still you

can change the value of

the balance but now because we made that

private and the the compiler is smart

enough to figure out that because they

are not declared inside the primary

Concepts and they are only used you only

interact with them with the deposit and

withdraw

and uh calculate balance functions so we

don't use them outside you don't have

them also declaring the primary

Constructor it knows that you should

make them private because you interact

them only with the functions and now we

have this underline because it's private

so we cannot change this balance now

from outside the class we can only

interact them again using the functions

so I'm gonna delete this and

now see you in the next video

so let's delete this

so actually I'm gonna let it there

because

it's uh good to see so now it's time to

start a discussion about inheritance so

what is inheritance inheritance is the

concept in the quarterly programming

context with which you can create a

class using another class so in other

words you can get all the functions and

properties that have that the class

already has in your new class and in

addition to the getting all the

functions in properties in uh in your

new class you can add some new functions

some new properties which are specific

to this new class but they aren't for

the also this the class from which you

are inheriting is called The Base Class

the parent class or the super class

and you might be wondering why is this

useful why you want to do this

let's say that you have a very big class

which performs very hard task and has a

lot of functions in coding but you want

to create a new class which is slightly

different from this class what you can

do is you can get all that code copied

in the new class and then you can add

your new functions or new your new

Behavior to this class but dust is not

recommended because you have duplicate

code what you can do instead is you can

inherit all the functions and properties

which this big class already has and in

your new class you can add the new

functionality the new Behavior which is

specific to this new class but it's not

for the base class so in this way you

can uh you you avoid the duplicated code

and you increase the code reusability

and for that I'm going to show you in an

example and I'm going to create two

classes one is going to be called car

and one is going to be plain so we

create here a class we go down here

we type class

I'm going to call it car

is going to have some properties the

first one is going to be called name

this is going to be the brand name of

the car

the second one is going to be color also

string

engines the number of engines that this

car has

gonna be an integer

and Val

this is going to be

doors so we're gonna store the number of

doors

all right

we put curly braces because I'm gonna

add some functions to this class

to function actually fun

move

and uh

fun

stop

and here you're gonna say

print line

quotation marks dollar sign

the name of the car

is moving

similarly

down here in the stop we're going to say

the car has stopped

car

has

and we're gonna create a another class

called plane

which is going to have also Val

name

we're gonna be a string a vowel color

also string

Val engines an integer and the valve

doors

unint

put curly braces and we Define those two

function we're going to copy those

I'm gonna paste it here

so now what we did here is that we

created two classes

they both both have

uh properties for the primary

Constructors and but as you probably

already noticed we have the same

properties we have the name here we have

the name there we have the color there

we have the color here the color there

the engines also we have here and there

and we also have those functions which

are almost actually are the same so we

have what I said at the beginning

duplicated code so it doesn't make sense

to put this code here another

another way to do this is to create a

base class a generic class called

vehicle which is uh and usually

you create the generic class when you

know that

several classes we are gonna share those

properties and those behaviors so a car

in the plane both are Vehicles so we can

put here instead of declaring the the

name and the caller inside the our

inside our classes car and plane you can

put this in a vague vehicle uh

class in a base class and we can inherit

those properties in our classes instead

of declaring them your inside our car

and playing class so we can put here Val

name

it's going to be a string

and the Val

color also string

now I can copy those two

let's put curly braces here and I can

paste it paste it here so

now I can delete those those two

functions from our classes

and what we can do now is we can inherit

those properties so we can delete those

properties that we have here

we cannot we will as you'll see you'll

need to declare them as parameters but

uh

they are not they're not going to be as

properties in any they're not going to

be as properties here

now

what we can do is that we can inherit

this shared those share the functions

and those shared properties because both

the car and the plane

have a function to move have a function

to stop the baby they both share a name

they both share a color so they both

share those properties and uh functions

but uh

as we did previously is to put them

inside the the the classes would not

it's not good because we have a

duplicated code and in this way we put

this code here

and now we can inherit this code so we

can have this code directly you can you

can have this code the being uh

being accessed inside the car and plane

without declaring them in our classes so

to inherit from our class first you need

to Mark the class from which you want to

inherit as open because by default they

are they you cannot inherit from a class

so if I enter in head from a class we go

at the enclosing impurities of our

primary Constructor

we put colon and we type the name of the

class from which we want to inherit and

in this case it's going to be vehicle

now

if you hover over here

it says that this type has a Constructor

and thus mods B initialized here and and

next you say this type is final so it

cannot be inherited from and that means

that we cannot inherit from this class

unless we mark this class with the open

keyword in front of it so we need to put

here open

and now if we hover over here

now we don't have that error you only

have this here which is this type has a

Constructor and dust must be initialized

here and that basically means that it

says hey I'm trying to use this vehicle

class I'm trying to but in order to use

it I mean I need to initialize this

vehicle class so we need to pass some

values to the

construct of the primary Constructor

that is defined here you need to pass

some values to the name and to color

and to do that we Define here name we

type name

actually dot let's press Ctrl Z

let's put a space and we type name

we put

comma then we type

color

and notice that we don't have the VAR or

the Vault keyword in front of them that

is because the those

parameters that would have here because

they are parameters because they don't

have the value of our keyword they they

uh they they they have to be declared

only as parameters because the

properties are going to be declared and

they are declared by the primary

Constructor of the base class from which

we are inheriting from

and because of that we don't we we don't

we don't need to declare the property

again here as you can see if I put the

VAR keyword we have an error says name

hyzer anyway what that means that we

cannot have the property declared here

because the properties uh already

declared inside the Base Class inside

the vehicle class from which we are

inheriting from what

we can do here is Define the parameters

and the value that is passed to those

parameters so the primary Constructor of

the class cards are going to be passed

to the to the vehicle primary

Constructor so hero put the primary

Constructor so you put two parentheses

and we type name

and this name is this one from here

so we type name then we put comma

because we need to pass a value for the

second one for the color and the type

color

and this color is this color here

so we type color

now

if you go up here let's say for let's

look first as the actually let's also

inherit from inherit inherit also one in

on the plane class so we put colon also

here

we type vehicle

and now we need to also to Define here

the parameters the name and the color

so we type name

and at the time you can see that

IntelliJ has given us some hints so if I

type here color it says that color

string so it's very helpful

put colon we pass those values to

primary Constructor of the base class so

we type name

color

and now if I go up here on I create some

instances of our car and playing class

so we type here Val car let's put equals

this type card

let's choose for the name BMV

and the

for the color let's say red

for the engines one for the doors four

now I'm going to create also an object

with our plane so let's press Ctrl alt

form on the code

valve plane

equals plane so our class

we type here

for the name

for the color let's say

or white

and

blue

and

for the engines for

and for the doors let's see

also for

now if I go down here and I

type here car dot

if you look here you see that you see

that we have the function stop and the

function move but we didn't declare them

here you deleted them previously but

because we are inheriting from the

vehicle class because when you are in

editing the properties and the functions

from the class from which you are

inheriting from are part of the new

classes so the the the

move function in the stop function they

are now part of our color class

similarly it's the same in the same the

same here the the the the move and stop

are uh are part of the class plane so if

I type here car

dot move

and car dot stop and then plane dot move

and plane so look like then now I can

call them like they are declared inside

the our current plane but they are not

there but because we are inheriting they

are part of the class so you can think

of

you can think of this thing that we have

here as a single entity so it acts like

a single entity

so now if you run the squad

we get in the output band there is

moving then they have stopped bearing is

moving doing has stopped

and uh

this uh we get this output because as I

said those functions are part of our

classes and they are using the name

so in this case the name that is passed

to the car this name that we have here

been there

is passed to the to the primary

Constructor of the base class and then

it's used here so it's calling the

movement save and there is moving so in

the same way for the for the plane we

get the same output it gets the value

that is passed here viewing and then it

outputs the viewing is moving and then

it has stopped

but we can do even more we can

override those functions inside our

class so we can create a version which

is inside the base class from your

inheriting we can create a version of

this function which is specific to the

car or which is specific to the plane

and I'm gonna look at that

so now I'm going to override the move

function from our Base Class vehicle

inside our playing class because I want

to make the move function specific to

the playing class so I want to provide I

want to provide an implementation of the

move class which is specific to a plane

so how is our plane is moving a plane is

moving by flying so I'm not satisfied

with only this generic function which

says the name of the vehicle is moving

and to override the function you first

need to Mark Mark the function similarly

to

to the class we need to mark it with the

open keyword so we type open because

kotlin wants to make things explicit so

we put open and now we can

we can

we can override that function inside our

plane class

so we go to to override the function go

to code

generate and here we have vehicles in

harsh code tostring and go to override

methods and you you have those equals

hash code into string and those are here

because every class that you create in

Kathleen is implicitly inheriting from

the any class but we're gonna look at

that on but more more on that later

because you look at that in a different

video and we have our function move and

stop so

the the idea is telling us that those

are the functions which can be

overridden inside our plane class from

our vehicle Base Class so we choose move

we press ok

and you have this super dot move we're

gonna see middle what is this and you

can also override it

by just typing the keyword override and

you can override the function so you can

type here override

and while I type in you see that we have

this override fan move stop or and have

also those so we choose move

and I have the same thing

now super dot move means that call the

function so super means call something

from our base class in this case call

the move function from our Base Class so

we're gonna let that here but in

addition to that we're going to add a

new function so we're going to type here

fun

flying because the plane is flying

and we're just gonna have a simple print

line

which is going to say

put quotation marks let's bring this

down a little bit

a plane is

flying so now I'm gonna call this

function flying inside our move function

which was overridden

so we type flying

flying

now if we run this code look what

happens

if you look in the output to get when

they is moving when they have stopped

and then we get the apply the plane is

flying and then we get bearing is moving

and bearing has stopped so what what has

happened here now because we where when

this function move is called here on our

object plane created

now it's using our own implementation of

the move function and after that so it's

using our own implementation it's

calling the flying function that we

created here because a plane is moving

in a specific way is flying and then

it's calling super dot move and super

dot move is calling the function from

the base class so it's calling the

function from here so

by doing this we made our function

specific you make we make the we made

the function move specific make the

function move unique to the plane class

because a plane is moving in a specific

way and we can do the same thing for the

the car class so

uh now you may be wondering why why I

didn't declare the inside our Base Class

the engines because they are also

present in in both classes because when

you create a generic class you should

only include the commonly used functions

commonly used and shared functions and

the properties so uh

all all vehicles have a name and the

color but not all vehicles have engines

so this is why I put the engines here so

in addition to the name and the color

which all vehicles have we added some

new properties which are specific to a

car

similar here in addition to the name and

the color which all vehicles have and

we've inherited that from our class we

add that also engines and doors which

those things are in addition they are

specific to a plane and more we also

override the move function because we're

not satisfied with the with the simple

move function from our Base Class to

include to provide our own

implementation of how a plane is moving

so you can see that by using an

inheritance and

adding new properties are the new

function you can create more specific

classes you can inherit you can also

inherit from the car class to create a

more specific class you can create a

Tesla

class and so on so this is what

inheritance is and

see you in the next video

so I thought that's a good idea to show

you another example with inheritance and

I'm gonna go down here

and I'm going to define a class so I'm

going to type the keyword class and I'm

going to call it View

and this view is going to represent a

view in uh which is used in Android and

if you want Android is just a

rectangular area on the screen and it's

responsible for drawing and event

handling

but you're gonna mimic as I said what

the view is and uh you know Android or

not going to create a view like the one

that is in use like the one that's used

in Android

and we Define the primary Constructor

but we're gonna not Define we're not

going to Define any properties the

primary Constructor

and this view is going to have a simple

function called Draw and it's going to

be responsible for drawing this view on

the screen so we type draw

and create a function and we're gonna

add the simple a simple print line here

which is going to say drawing The View

so we put here

quotation marks

drawing

The View

and uh we're gonna

mark this class as open because I want

to inherit it from it

and we're also going to Mark the

function open because I'm going to

override that function in the

in the next class and because this is a

generic View and it's going to have this

draw function only this function what

you can do let's say that I want to I

don't want to create a simple uh a

simple view which is just a rectangular

area on the screen but I want to create

something more specific let's say that I

want to create a button

in that case instead of creating the

button the button from zero because this

View

in Android

has a lot of functions and properties

which are responsible for creating the

view for handling the events and so on

so

um because here we have on your simple

function but the real view has hundreds

of functions and instead of creating our

button from zero we can just inherit

from The View and we can inherit all the

functionality that the view already

provides to create a generic View and we

can add some

unique characteristics characteristics

which are specific to a button and in in

this case in this way we increase the

quad reusability and it's more easy for

us to to create the button than rather

than creating the button from zero

because the view already provides all

the tools that you need to create a

generic view only we you're gonna

inherit all that behavior all that

properties but I'm gonna but we're gonna

change it a little bit so this is what

we're gonna do with the inheritance so

we create a new class called button

so we Define the primary Constructor

and here you can Define some properties

which are specific to a button not not

just to a generic view which is a

rectangular area on the screen so we put

Val text and this is going to be a

string so our our button will have a

text which can be plugged in or signed

up

and also it's going to have orientation

because it's going to be one on a place

on the screen

on a specific place on the screen and

this is going to be a string

of course you just only Mimi only mimic

the

View and button which are are used in

Android this is why we have so many we

see we have so little properties and

only a simple function function in our

view draw and we put uh here colon to

inherit from our view because I want to

inherit all that functionality all that

properties that a view provides but I

wonder is just to change it a little bit

to make it more specific I want to make

a button and of course as I said this is

just uh

an example we don't have but in a real

in a real app this will have a hundreds

or thousands of line of code and we can

inherit all of that behavior or that

functionality in our new

New View which is a button but you can

override some functions and you can

provide some you can create a more

specific view than a general review so

you don't need to define the primary

Constructor

and you put the curly braces

now I want to override the draw

functions so that when the draw when the

draw function is called I want to not

drawing just a generic view but to draw

a button of course as I said this is

just an imaginary example so we type

overwrite fundra

and instead of calling the super

function which is inside our

our Base Class View or parent Class View

I'm gonna type here print line

and we're gonna put the text drawing

the

button

of course and now what we did here we

created the generic view as I said and

again this can have hundreds of maybe

thousands of line of code and functions

and different functions different

properties

but we we inherited from The View class

and we added some new functionality so

instead of just drawing a generic view

now of course we just add a simple print

line here but here you can have the code

which is going to be responsible for

drawing a speed a button so it's going

to be more specific than a simple

generic view so you can put here a here

is the code

for

creating

the button so I'm going to put the

Commander and let's say that

now I want to create a run around button

so instead of creating again all that

functionality which which is present in

the view and then create and then use

and then creating all the functionality

which is uh inside the button the button

class we can make this button uh open

and we can inherit all the functionality

that the button provides and we're gonna

add something new something unique to

this class that we're going to create

and this class is going to be a round

button so we type here round

button

and here you need to define the text

because we're going to inherit from the

for the from the class button

orientation and notice that I don't use

the VAR or the VAR keyword here because

they they're going to be only parameters

because the properties the actual

property is going to be are going to be

um

ultimately declared

in the button class

and

one thing which is going to be specific

to a round button that you're going to

create is going to be Corners so we type

Val corners

it was gonna be an integer because the

the corners basically here are going to

pass a value which is gonna in our

marginal example this is going to

receive a value which can be

um in degrees so how many degrees do you

want to how many degrees you want to

make the buttons by how many degrees you

want to make the button around so it can

pass here 20 or 30 anyway so we put

colon here and we inherit now from our

button

class

right and here you need to pass the

text

the orientation

and that's all

and you put calibrases now let's

override the draw function in our round

button because when that function is

called now I want to add some specific

code some new functionality which is

specific to a round button so it's not

gonna draw just a button but it's gonna

draw a round button so we of course

we're just going to add the simple print

line there but um

in a real app you can put here the

actual cottage which is gonna draw the

round butter so we put here print line

and we put

rowing

the round

button

now let's create some instances let's

create some objects with those classes

and we type here

Val view so we're going to create our

generic View

we Define the battery Constructor we

don't have to pass any values there

because we didn't Define any properties

inside our view

then we create a button so we put here

Val

button

equals button

and you don't need to pass some value so

I'm going to pass here the text let's

say login

the orientation let's say yo

it's going to be a text here so we put

here let's say Center

next we create a new

a new object but this is going to be now

a run around the button so we put around

button

equals round button

and you pass the text here round

button

actually let's pause it takes uh sign up

and uh

for the orientation let's put

Center also

lo you need to pass also value from for

the colors let's put let's say 30

degrees it will be

in our imaginary example now let's call

the function the function draw on all of

our objects that we create so we type

view dot draw

then we put button

dot draw

and round button

dot draw now if you

just press Ctrl Z

if you run this code

so we get in the output drawing The View

then we get drawn in the button drawing

The View again drawing the round button

and let's uh think about why you have

this output so first we see that we have

drawing The View which is the function

inside our view that you created here so

it's from the generic view so it's

drawing The View then we have drawing

the button so

here

when this line of code is uh red it says

drawing the button so you have

we have our we have our code which is

you know which is with just a print line

but uh so this is drawing the the button

then it's calling the super function so

it's calling the function inside the

view which it says drawing The View

next

when this line of code is red so round

that round button that draw now it's

calling the draw

function which is inside the round

button so instead of just now calling

the because now we are inheriting from

the button class

now uh we override this function here

and it's it's uh using our

implementation for creating a round

button it's uh executing this line of

code and then it's calling the super

implementation which is drawing the

button you can actually remove this if

you want because you let's say that

calling the code inside from the button

is not gonna affect our logic so if I

delete this now if you run this code

now we have drawing the view drawing the

button and we have drawing the view

drawing the wrong button so now it's not

calling here the super so it's not it's

not calling the code which is inside the

for the bottom because we are inheriting

first from the button but but the button

is also inheriting from The View so it's

calling first this then it's calling

draw which is inside here and we have

the round but now we only have drawing

the wrong button because here now it's

using our own implementation of the draw

so we have drawing the round button

so buy this just by

by making our view because as I said in

real app this view is can have hundreds

of maybe thousands of line of code of

different functions and properties so

you don't want to create all of that

from zero when I want only to let's say

I want to create a button I can't

inherit I can inherit from The View

which already provides all the functions

and properties and logic to create a

generic view but I can override some

functions which are responsible let's

say for like this draw and I can ex I

can Define some logic and when all of

that functionality is called It's Gonna

not draw only a generic view but it's

gonna draw a button so by inheriting

from The View we increase we increase

code the code reusability

and this is very very powerful because

let's say that uh like in our example

you want to you don't you're not

satisfied with the simple button which

is used in Android and you want to

create a draw a round button so instead

of creating all of that logic from zero

you can just inherit from a simple

button you can have override some

functions and when when you execute that

quad now it's gonna execute the code

with the implementation with the

additional implementation the with the

additional code which is which is gonna

generate a round button and it's just

not just a simple button so this is our

this is our discussion about inheritance

I hope that this clarifies some things

and see you in the next video

so now it's time to start a discussion

about seal classes so often you want to

represent only a fixed set of

possibilities so however case can either

success or it can fail a user can be a

standard user or a pro user and in those

cases you may be thinking now that you

can use enum classes but enumclasses

have some limitations and let's see why

you should use instead seal class so

let's type Main

I'm going to create a in on class here

call it the result

and let's put curly braces unless I hear

success

and the error

now the problem with this is that we

cannot encode more information here so

let's say that I want to to to Define

here a non-resolvable resolver error and

resolver error and I cannot do that here

so if I type here x dot exception I can

I cannot do that because

um the the the the types that are

defined here cannot have properties so

you cannot Define this here and for that

we need to use seal classes and to use

the seal class you just type seal class

I'm going to call it result

it's going to have a parameter called

Val message it's going to be of type

string

and you can Define your uh

classes which are gone inherit from the

seal class either outside the seal class

so you can Target class let's say

success

and let's type here

meh

message of type string and we can

inherit from the seal class so you can

type here result

message

or you can copy this and you can Define

it inside the enumclass so you can have

it as an SSC class so I'm going to type

another class here called

error

foreign

the parameter here

actually not

that is the parameter

message

foreign

the parameter here

message I'm gonna hit it from the result

you know from the results class

I'm gonna pass that parameter here

to the parent class

now

I'm gonna Define a function down here

called get data fun get data and it's

gonna have

a parameter called the result

it's going to be of type result

and here you're going to define a when

statement so we type when

the type result and now is the

interesting part

now if you hover over this underline

over when it says when expression must

be exhaustive unnecessary is error

comma is Success branches or else Branch

instead so in other words what is saying

here by uh

to have the one statement exhaustive

it's it's what it's saying another word

is that it needs to cover all the

possibilities so both possibilities that

we have here and you can type yourself

this or you can click on here click on

this right bulb and you can click here

add the remaining branches and as you

can see it's added the remaining browser

you add is result. error and easy result

does success and those are exhausted

those are the only two possibilities

which are

which are possible so here I'm gonna

type result

let's define a function inside the CL

class so I'm going to type here fun

show

message

it's gonna have a print line which is

going to say result

dollar sign

message

so and now I'm going to put dot

show message and also here result

dot show message and now this is

uh this is uh exhausted because now we

call we covered all the possibilities

which which are uh inside the which are

as a consequence of the fact that we our

classes are uh inheriting from the seal

class so if I add here an uh else Branch

so if I put here else as you can as you

you'll see this is going to be redundant

because we covered all the possibilities

and the compilers know this and this can

be very useful in some scenarios

so if I put calibrations as you can see

this is gray out because it's redundant

because it knows that we covered all the

possibilities and the possibilities are

only those two result that error and

result are success so if I type here now

success let's type success

equals

result

bo2s there

equals result dot success

let's type success here

let's press Ctrl D let's call this error

result let's change this to error so

this is how you create instances of the

nested classes let's call this failed

now let's type here get data down here

get data and let's let's pass success

and let's run our code now as you'll see

the message

result success

so we get results success in the output

so

if I change now this to error you will

see that this is the result error

because it is executing this branch

so if I run this

to get result failed

now the interesting part here is if I

add here another

possibilities so

if I type here class

progress

and I type message

string inheriting from result

let's type let's pass the message

parameter to the super class to the

parent class

now what what we have because our class

is a sealed class what we have here is

that the when is underlined and is

trying to say something to us is saying

that when expression must be exhaustive

unnecessary is progress so it knows that

there is another possibility for uh for

our result uh we it can be therefore you

need to add that possibility otherwise

I'm going to show you this order and if

you try to compile this code I'm gonna

throw an error so what what you can do

here is can we can type here is a result

to make it exhaustive so to cover all

the possibility result of that progress

let's put Arrow

result dot show message

and now if I create here another Val

let's call this uh

progress

because the result dot progress and

let's define for the parameter

progress here

now if I change this to progress

now you'll see the output result

progress

so we get result progress because

now this branch is true but if I let's

if I delete all of those look what

happens if I click here or is where you

have those on this underline it says add

else Branch so when expression must be

exhausted so it means that it needs to

cover all the possibility

possibilities are necessary is error is

progress and is Success so if we click

here on more actions

and if you click here on ADD remaining

branches it it knows that those are all

the possibilities on which our result

can be and it automatically generated

the

the check for us and we we only just

need to type here result

not show message and that's all and with

tnumps you you don't have this

auto-generated code for you because uh

I'm gonna explain immediately

so with with an enum if you add another

so if you have a Nemo class I can delete

the code now here because

but I'm going to keep the code as it is

and because all the subclasses of excel

class are known by the compiler the

compiler can fill all the possible cases

automatically for us if I declare this

Arrow class as a seal class and I

declare two classes inside here called

let's say class

resolvable error so or recoverable error

and it's going to have a parameter

called exception it's going to be of

type exception

this one

and we're going to extend from our error

seal class

not this one

this one

and we're going to pass the message

parameter

and I'm going to declare another class

non-recoverable

and it's going to have an exception

so this is more powerful than

in arms because you can have all those

properties and you can you can have

subtypes and and all of this and within

arms we cannot do this so let's extend

from error

let's pass the message

and now I need to delete this because we

cannot instantiate The Shield

class so let's delete this

now if I delete this now from here look

what happens I have an underline and it

says

add

it says one expression must be

exhaustive so it must cover all possible

cases that that exists for this cell

class unnecessary is not recoverable or

is recovered or branches so if I click

on this and I click on this bulb I click

here add remaining branches as you can

see it adds all the possible cases so

all the possible cases that we Define

inside our seal class for us

automatically

and we just type your result

dot show message

again here result

message and you cannot have this

Behavior with the non-class so this is

why still classes are more powerful

because they can have subclasses you can

have

more information on the classes so you

can have exceptions passed to them and

so on so

this is our discussion about seal

classes and see you in the next video

so now it's time to start a discussion

about abstract classes and to show you

what abstract classes are I'm going to

use the code that you had in our

discussion about inheritance but I'm

going to change it a little bit so I'm

going to delete for this class plane

because you don't need this class and

I'm gonna

I'm gonna delete this instances that we

have here

and I'm going to declare this as a VAR

and this also has a VAR so they're gonna

be now properties

I'm gonna delete the properties that we

have here

and uh

now we have this here so I'm gonna

delete these two

let's delete this

let's delete this

now

what are abstract classes

um abstract classes are very similar to

interfaces the only difference is that

in abstract classes you can declare

properties which can have a value and

you know in interfaces you can't

actually you can but you you need to

provide an access modifier

and to declare an abstract class we go

in front of the class that you I want to

make it abstract and we type the

abstract keyword

let's put the class keyword

and now our class is abstract and with

an abstract class you cannot create

instances abstract classes are created

only to be inherited from uh by other

classes and now if I try to create an

instance with this abstract class and I

just type Val vehicle

and we type vehicle here and we put the

parenthesis to call the go to the

Constructor

you have an error here and says cannot

create an instance of an abstract class

so you cannot create an object with an

abstract class an abstract class can

only be inherited by another class and

abstract classes usually are used like

interfaces when you want to provide

what you need what needs to be done but

you don't provide how it's going to be

done so you can you need abstract

classes are used when you define

uh functions without the body and the

code the body and the code is uh it's uh

implemented it's provided by the class

by the class which inherits from the

abstract class so they are as I say

similar very similar to interfaces the

only difference that in abstract classes

you can have properties so you can have

here a Val

text

and you can assign here some text in uh

in an interface you cannot do this let's

type some text

so this is the difference and uh

also

um you I should say that um

you can Implement many as many

interfaces as you want

but in a class but you can only inherit

from a single class right this I forgot

to say when I talked about the

interfaces now

if I want to make uh if I want to if I

to make now our functions abstract you

just type the keyword abstract in front

of them and now they are abstract and

we're gonna do the same for our stop

function but now we have this underline

because as I said abstract functions

like interfaces or the functions which

are defined in the interfaces cannot

have a body so we need to delete this uh

you need to delete the body you need to

delete the curly braces Because the

actual body the actual logic and the

code is going to be provided by the

class which uh in head is from the

abstract class now

to inherit from an abstract class we go

here

we put colon and we type the name of the

abstract class from which you want to

inherit vehicle

now

if you go out here now we have this red

bar which says

so need to call the Constructor here

all right actually I think I can delete

this but you still you still need to

call this Constructor here even though

we don't have any any parameters any

even though we didn't even Define The

Constructor here now we need because

like the interface the abstract class if

you inherit from an abstract cluster and

if that abstract abstract class has some

abstract abstract functions defined in

it you need to implement those functions

inside your class and you go here when

you have this underline if you click on

this right red bulb and it says

Implement members order make car

abstract and we click on Implement

members we select our two functions we

click ok

and now we have our functions

implemented inside our class

so this is what

Astra classes are and they are as I said

very similar to interfaces but the only

difference is that you can Define in

properties inside the an abstract class

and you can provide the value to that

property and here because we we have

inherited from our abstract class here

you can provide the actual

implementation of the functions so as

you can see we have this to do which

says not yet implemented so they are

they are very similar to interfaces

so I'm gonna end this video now and see

you in the next video but before I end

the video I saw that it's a good idea to

to say to you where abstract classes and

interfaces are used uh interfaces that

are using many many places but um

abstract classes are particularly used

when uh and also interfaces are used

when you have some apis and those apis

want to just to define the functions and

the API is going to generate the actual

code for uh for those function so you

can have a let's say a database

and you can provide the the name of the

functions which are responsible for

creating the database for getting

getting data from the database but you

don't provide the actual code which is

gonna select that data from the database

and return the data you just you just

declare your functions as as the

abstract

and the API is going to generate all

that code which is responsible for uh

returning the data and from the in other

words the API is going to do all the

hard work you just you you just Define

the the functions if you have if you

let's say if you have a property Define

an abstract class in the abstract

functions or interfaces and the API is

going to do the hard work of uh based on

what your function is doing by

editing the name of the function is

going to generate all that code which

you you've you've had to generate if you

do if you don't have if you're not

before the API to generate that for you

so this is where uh interfaces and after

classes are sometimes used and the

interfaces also are used in event

handling and that is one in one um uh

the you need to write code to respond to

respond to respond to graphical using

using your interface uh movements

so I'm gonna end the video now see you

next

so now it's time to start a discussion

about data classes but in order to

understand what data classes are we

first need to understand what is the

difference between structure equality

versus referential equality

and to restart the difference I'm going

to create two variables the first one is

going to be a Val it's going to be

called name one

and we're gonna assign the

follow Alex three so it's going to be of

type string

now I'm going to press Ctrl D to

duplicate that line of code

and to declare the second variable which

is going to be called name two and it's

also going to have the value RX

now I'm going to add the printerland

here to output something to the console

and here we're gonna type name one

equals equals

name to

so the equals to operator is doing what

is called is checking for structural

equality because it's checking to see if

the content of the variable name 1 is

the same with the content of the

variable name too so another loss is

checking to see if they have the same

value and it's they have because both

have Alex assigned to them so now if I

run this code

because this is a Boolean expression

this is going to return true or false

and we're going to see in the console

too

but if I change this to Alexandro now

the variable name 2 has a different

value so this is going to return false

because they are not structurally equal

their content is different they have

different values

now get false and this is called the

structure equality

the next type of equality is called

referential equality and the reference

equality is used when you want to check

if two variables or two objects are the

same so what the referential equality

operator is doing is checking to see if

a tool as I said variables or

objects are the same so if I type here

name one

and to use the the reference we call it

operator we put three equal sign here

and if I type here name one equals name

one so is name one equals to itself that

is going to be true

so you're gonna see through on the

console

outputted so we have two but if I change

name one equals equals equals to name

two that is going to return false

because they are distinct objects in

memory they are different uh variables

they are not the same there is the name

one

name one variable is not the same

variable as name two so we're gonna have

false here

so we have false so this is the

difference between a structural versus

referential equality so now I'm going to

close this

I'm going to close the console

and having this in mind we're going to

start a discussion about data classes

so let's go down here and let's create

our user class because I want to compare

to user objects to see if they are

structure equals so you type user let's

define the primary Constructor and let's

define three properties for the primary

Constructor the

first name

the last name

and the age

which is going to be an integer

now if I want to check if to

user objects are a star vehicle let's

first create those objects so let's

delete this code

let's say Val user1

equals now let's create our object and

let's pass some values to the

properties of the Constructor let's pass

here Alex

for the last name let's put open

and for the age 23.

now let's press Ctrl D to duplicate that

line of code and to create the second

user object let's give a different name

to this user too

now go down here let's press Ctrl alt

alt for mother code

we typed our println because I want to

check to see if our users objects are

equal

if they are structurally equal so we

type here user1

equals equals so the equals to operator

user 2.

now if you run this code what do you

think you're going to see in the output

what you're seeing the output is

actually that you'll get

we get false and you may be wondering

why because if you think about the air

structurally because they have the same

values for the properties but they are a

distinct objects but we said previously

that the equals

equals uh two equals operator is

checking for uh structure equality so

why get false here

this is because previously the equals to

operator is

it was checking to see if the two

strings are structurally equal and the

equals to operator is actually

a function which is defined inside the

any class and the string class what

actually was doing when we called the

equals to operator

it was using its own implementation of

the equals method so

uh every class that we create in kotlin

it's using its implicitly inheriting

from the any class and because every

class that we created it's implicitly

inheriting from the any class

every class is also going to use the

functions which are inside the any class

so if you don't override those functions

inside your class to provide your own

implementation like the string class did

for our strengths then it's going to use

the implicit uh the font is going to use

the function which inside which are

inside the any class and

if you type here user1 that and we have

equals to string and hashcode those

three but those three functions we are

particularly interested and the

particular one is interested inside the

equals they perform when you don't

Implement inside your own class those

functions

again particularly on Bay course class

the equals class spin forums only a

referential equality so it only checks

if those two users

are the same users and if they're not

that's gonna it's gonna return false so

this is why you get false here because

it's calling the function the function

which is inside any class so what you

have implicitly is something like this

so for any class that you you use or for

any class that you declare

you have implicity inherited from the

any class and because of that you have

those

three functions which

are defined and if you don't override

them to provide your own implementation

then it's going to use the code that's

inside there so actually if you press

Ctrl here

and if you

click it takes us inside the any class

so it's clear that this function

the equals function is inside any class

so because we don't have any functions

here so we need to override that

function inside our class and we need to

provide our own implementation to Define

our own code to determine if two user

are one of when two users are slash

legal this is what you're going to

Define inside our class so let's delete

this

let's pull the curly braces and actually

I'm gonna bring an image to show you how

the quarter and hierarchy actually looks

so this is the image and at the top as

you can see we have the any class and

then we have our Boolean class our

string class and then we have our user

class and all of those classes I have

here the Boolean the string the number

they are inheriting from the any class

they are

using the the functions which are

defined inside the any class and they

override the they are overriding those

methods inside them and they are

providing their own implementation for

those functions and this is what we need

to do also here as you can see they will

inherit from the any class implicitly so

we need to override them to provide our

own implementation for the equals and

for the next two because we're going to

look at also at the

as you saw there you are going to look

also at hash code because uh equals and

hash code are very linked together and

also the two string function

so we need to override those uh inside

our class

so this is what we're gonna do

so we go inside our user class and here

we need to override the equals function

when we type overwrite and while I'm

dumping you can see that we have three

suggestions here and we select the user

function because this is the one that we

are interested in and as you can see

here

this function is part of the any class

so we press enter we delete the super

return

and the first thing that we need to

check is to see if the current instance

the current object that on which this

equals function is called it's comparing

with its cell so we need to check if the

the if the object is the same as the

object that we are comparing with and to

do that we type here if this

so if this current object is current

instance equals equals so three equals

we check for referential equality to see

if they are the same equals

other so

we type other and if that is the case if

we call we are comparing the same object

so you have user one equals user one

then gonna return true because that

means that uh we are comparing uh with

the same object if that is not true then

the next thing that we need to check we

we're gonna go down here so if not we're

not comparing with uh it's not comparing

with itself then we need to check if the

other so the object which is passed as

an argument to this function which is

called on another object

and to check if other so the argument

is user because we we can uh we need to

check the expressly to see if this

object which is passed as an argument to

our equals function is an user object

because we don't want to check for a

different object we need we need to to

see if this is a new zero object so only

if the object which is passed as an

argument to the equals function is an

user object then we're gonna

return true so here you need to type now

we need to determine we need to Define

our Logic for what it means for two

users to be equal and it's up to you how

we Define this implementation I'm going

to type here only return so I'm gonna my

logic is if they have the same first

name the same last name and the same age

we're gonna say that they are equal we

have the same user of course that in a

real app you can have a more complex

implementation so we type here return

this so This current instance that first

name so

and then put two equals other so

if so the other is because we check here

explicit to see if this is another also

because this condition is going to only

pass if the other

argument is unusual so we type other

because we have access to the first name

and the last name and the age if other

DOT first name so this is our first

condition and

this Dot

this dot last name

so I'm going to check for the all uh for

our three properties

equals equals other so other is another

object because we determine that here

that last name

and

this dot age equals equals H so this

entire expression that is that we have

here is gonna return through only or if

all the our three conditionals so if

this DOT first name is equal to other

that first name so with the object that

is passed as an argument or equals and

if this data last name is equal to other

that last name and if this dot h is

equal to

the other that age let's put here

then and only then we're gonna have a

true here so it's going to return

through otherwise if one of those

condition fails it's gonna return false

and down here you need to if other let's

say doesn't pass so if other reason is

not an user we're gonna return false so

in that case going to turn false now if

I run this code look what happens

so previously we had the false and now

we have true here and this is because

now it's using the equals function which

is inside our user class so it's using

our own implementation so it's checked

here to see if uh our

first name is equal to other DOT first

name so it's checking uh so it's using

our logic inside the the equals function

so this is why you get true and you can

also you can now if you press if you

hold Ctrl here as you can see now it's

taking us inside the equals method that

we defined here it's not taking it

inside the ionic class this is because

now it's using our own implementation so

first to check to see if we are dealing

with the same object then we're going to

return true you're not going to go any

any we're not going to go down and if

this is false you're gonna come up here

you're gonna check to see if other the

object which is passed or equals

function is an user instance it's an

user object then I'm gonna check to see

if our current object that we are

calling on the sqls

function is equal which exclusive for

the first name there is the same we

check to see if the last name is the

same and we check to see if the age is

also the same then in that case it's

going to return true and we're gonna say

that they are structurally equal so

we're gonna see through in the output

else if other is not an user so if you

pass

to the function argument the different

parameter a different argument here

let's say you pass an integer then it's

going to return false here we're going

to come down here so now if you look

here you may be wondering how this

syntax works so we have those signs but

it's calling the equal smart function so

you can also put that equals if it makes

more sense and if we put that equals

user so

so uh

now you can see that but if you hover

over he says replace with the two equals

so it's the same thing as having that

equals and

passing that argument here which is user

two so again we are invoking uh the

equals method on the user one object

let's press Ctrl J

and we are passing our user to objects

and we are using our logic that we

Define here to determine if they are

equal

and you can replace this back to to

equals or if it makes more

visual sense to put the to use the

equals you can use equals it's the same

thing

we put user to here

so as you can see we are calling the

function so this means this object so

this object so defined here this if uh

when we're saying this that first name

it we we mean this object so if this is

that first name it's equal with the

other other that first name it's then we

are calling this object that is passed

here as an argument so

um next we will look we're going to look

at the next two functions because as I

said the equals and hash codes are very

linked together and we're going to also

look at the the two string function and

now if I change the first name for the

user 2 to join and if I run this code

you will get false in the output because

when it will come inside this if it will

check to see if this does first name

equals to other at first name and

because we're using the end operator and

all the conditions have to be true this

is going to return false so I get false

in the output

and um

but if I change it back to Alex

we'll have

true so the objects are structurally

equal based on the logic that we defined

here

inside our equals function

and as I said you also

you can also look at equals and hashcode

because they are very linked together

and uh

the rule is whenever you override equals

you also need to override the the

hashcode and if two objects are

comparing equal so if they are equal

based on the equals method that you

implemented they also have to have the

same hash code

and I'm not going to go into that but

this is because they are

the hash code is used for performers

reasons in collections but we're not to

go now into that we can just type here

override

and uh

here we just type return zero and this

uh this is not gonna affect our equality

at all as I said this hash code it will

make it makes more sense to implement

the hash code in a different way in

collections because they are used um for

performance reasons particularly with

hash set and hashmobs

but uh for now for our simple class

because we don't use this class inside

any collections we don't need because uh

we don't uh don't have to think about

that per the performance reasons to of

the hashcode and uh just if you want to

just uh just return zero here like I

return and yeah because it would work

fine it's not it's not going to affect

at all our equality but remember that

rulebook that if you override the equals

you also have to write a hash code and

uh if two objects are comparing equal

the more must have the same hashcode

this is true as I said particularly in

collections not now because we're not

using this code inside collection

and we can also override the tostring

method and this two string method is

used to return the string representation

of the class so we also type here

override and we have our two string

function I I said method a few times but

there are functions not methods methods

are different thing in Java

and uh

let's delete this function let's type it

again so if you can see

so this is going to return the string

representation of the class so I need to

to return here the values that that are

passed to our to our first name and last

name and age and if we don't do that if

you add the print line here

and you type user1

let's press Ctrl D and user 2. you're

gonna see just some

you're gonna see the user and you're

gonna see some numbers but we're not

gonna see the string representation of

the those two users you're not going to

see Alex so the name and the age so you

see this

dosto but uh

to avoid that because this also use

useful because previously when we want

to

um output in the console the values of

the properties you have to call those

properties and to add multiple Sprint

lines but with the two string

function we can just type here return

and type here

user

we put parenthesis and here we type

first name

we put equals we put dollar sign

let's put a single quotation mark dollar

sign

first name

let's put the quotation marks here a

single quotation marks

last name

equals dollar sign last name let's put

inside the single quotation marks this

comma and age we put also equals dollar

sign h

now if you run this code again now it's

going to use it's gonna one

when we are typing user one and user two

is going to use the tostring function

that we defined inside your class and

it's gonna return the string

representation of that specific object

so if you run this code

we get user and get first name Alex last

name Adobe in h23 and get for the second

you use their first name Alex last name

domain and H22 so now get the string

representation of this class and this is

uh more beautiful because you don't have

to work to always type that user one

user1.a user1. last name the first name

and so on and for the second object two

because now it's using the

two string function with our own

implementation inside here so this is

how you can use the tostring function

and this also how you can use the

password but as I said hashcode is it's

a different discussion which you're

gonna have when we're going to talk

about collections for now just type

return 0 here and then we could all fine

and if you think about there is a lot of

code just to compare to two objects we

have almost

38 line of hold just to to determine if

two objects two user objects are equal

and we also have to override this not

necessary but it will also variety this

hash code and the two strings so there

is a lot of code just to do this uh

simple thing to compare it is to user

objects are equal and because kotlin is

a byte it's about conciseness we don't

actually need to do all of this stuff

but I I thought I thought that it is a

okay to explain to you what you'll need

to do in the past in order to understand

what now questioning is doing better

especially with data classes so you

don't need to

type yourself all of this code that you

have here you can just so if I delete

all of this code let's copy it first and

then delete it

so if I delete all of this code also the

and now if I run this

now we get false and we get those we get

user and this tag text because now we

don't have an implementation of the

equals of the two string of the hashcode

and

if you think as I said it's a lot of

code to implement all of that and in

kotlin you can just type the data

keyword in front of the class and all of

that code not specifically the same

logic but all of that code that we typed

there is going to be generated

automatically for you so the equals

method is going to be generated the hash

code and the tostring and uh

the properties which are going to be

included inside the implementation of

the equals of the inside the of the hash

code and inside of the tostring are

gonna beat all the properties defined

inside the property Constructor so all

the properties defined inside the

property inside the primary Constructor

they're going to be used inside the

implementation of the equals of the

hashcode and the tostring so we just by

putting here the data keyword all of

that code all of that logic is going to

be implemented for us so if you run this

code now

so look at this how concise is compared

to what happens just one line of code

and if if you run our code with the data

keyword

now as you can see we have true and we

have user first name Alex last name so

you have the same output as previously

and just by putting data keyword

all of that was all of these calls so

let's press Ctrl J all of this squad

not specifically

exactly this code but all of this code

all the implementation of the equals of

the harsh code entry was generated

automatically for us just by uh just by

putting the data keyword in front of our

class

and if you don't want to have a property

included in the implementation of the

of the equals or of the harsh code or of

the the two string you can just omit it

from here and you just you declare it

inside the

inside the class so if I put let's say

here so if I delete this because data

classes cannot have parameters

and I declared it inside the class

inside the data class

VAR age and it will sign a value because

we cannot let

the property uninitialized

let's delete this now

and if you run this now that property is

going to be excluded from the

implementation of the equals the hash

code and of the two string so we have

here true but we have a first name Alex

last name Domi so now the limitation for

the equals only includes those

properties the first name and the last

name and also for the to string function

we have only the first name and the last

name the age is excluded but

let's put this back

so this is what data classes are they

are generating

Auto automatically for you the

implementation of the equals the harsh

code the two string also for the copy

function and for the component function

but we're going to look at the copy of

copy function in a separate video and

they generate all of that code that we

saw previously that we talked about

automatically for you by just putting

the data keyword you put the properties

inside the primary Constructor and all

of that implementation is going to be

generated for you for all the properties

you find inside the primary construction

and just that just by one line of code

all of that logic is generated

automatically so this is what data

classes are see you in the next video so

now it's time to start a discussion

about interfaces so what are interfaces

interfaces

um are you used when you want to have a

commonly used Behavior shared among

different classes but you don't

want to provide the actual code that

goes inside the class which is going to

implement that interface but you just

want to define the the name of the

function and the parameter of the

function

the actual code that goes inside the the

functions that you define in inside the

interface

they are particular to each object that

implements that specific interface

so

let's say that you have

let's say that we have some classes

let's say that you have a class car a

class which is uh which represents a

truck and you have a class which

represents

represents a plane you know that all of

those three objects have in common the

same behavior of starting the engine

let's say and you don't know how each

particular class is going to start the

engine but you know that each class is

gonna have in common this function which

is

I'm gonna start the engine again you

don't care about the particular how each

particular object is gonna uh what code

goes inside the or what uh logic goes

inside The Stand start engine function

for each of those particular objects but

to care only about the fact that they

they share the same behavior they they

all start the engine but how they start

the engine is uh particular to

each class and this is what we do with

interfaces we only Define the what so

what what we Define in the interface

only the

the the what and the what represents the

what those classes are gonna have in

common so you know that they uh gonna

have a function called start engine but

you don't

don't know how each object how each

class is gonna Implement that logic and

to that to to for that I'm gonna use an

interface so we Define what needs to be

done but we don't Define how it's going

to be done so to declare an interface we

go down here at the enclosing calibrates

of the main function and we type

interface and while I'm typing you see

that we have this suggestion we type we

press enter we can type the entire word

if you want so interface and I'm going

to call this internal phase engine and

the name of the interface should be with

a capital letter to start a capital

letter it should be in Pascal case if it

has multiple words

and the interfaces cannot have

Constructors because they cannot be

instantiated you cannot create you

cannot create an object with an

interface interfaces are used only they

are created only to be implemented by

classes they cannot be instantiated so

we need to put curly braces and inside

the

the body inside the curly braces we

Define what needs to be done so we

Define only

the function we Define we type the name

of the function I'm going to call it

start engine

but we don't Define the actual code for

the function so we put parenthesis here

you can Define parameters if you want

but we don't put curly braces and we

Define our logic here because we only

care what about what needs to be done so

we only care that each class needs to

start the engine how it's going to start

the engine it's up to each particular

class

so we go down here we type class and I'm

going to create a class called car and

it's going to have a

a vowel name which is going to be a

string

and a vaal color which is also going to

be a string

and now to implement the interface we

put colon and we type engine

and as you can see we have this engine

we have this eye for interface here so

you press enter then we put curly braces

to define the body of the class and now

we have this underline here and this

underline is here to tell us that we

need to in now we need to implement the

we need to implement this function

because the the interface and the class

have a contract is like a contract and

the contract says that if you implement

an interface you need to you must

Implement you must override the the the

function so you need to implement the

function you still will this is why we

have here here an underline so if I

click on this underline what if I hover

over there and I click

on the light on the red bulb you see

that it says Implement members so click

on Implement and have our

start engine function we click OK and

now our functions was overrided here and

we have this to do which is not yet

implemented and this is there to to tell

us that this method this function is not

Yeti implemented so we need to define

the actual logic here

and let's create two other classes let's

um

call this class track

and it's also going to have a name a Val

going to be a string

and the color also string

and we're gonna also

implement the interface so I'm going to

put colon engine then we put curabase

now we need to implement because I said

there is a contact between the interface

and the class and the contract says that

you need to implement the function

so we Implement a function here and we

create another class

call plane

and it's also going to have a Val name

it's going to be a string

and the eval color also string

and here we put colon and we're gonna

also implement the interface interface

inside the plane class

and we put call on engine our interface

then curly braces and now we have the

underline because again we there is the

contract now between the interface and

the class and that says that we need to

implement this function inside our plane

class

and we have those to do and those to do

are here to tell us that we need to

we need to imple we need to provide some

logic to the to the actual function so

let's delete those and let's put some

code here

so let's delete this

and let's add the print line here which

is gonna output some text to the console

and we're gonna have the text the car

you

starting

the engine

and I'm gonna copy this because now I'm

gonna put here some text

I'm gonna say the

track is starting the engine

and here is going to say the plane

starting the engine

now what what we have done here and what

we did here is that

we know that each class respectively our

car our tracking and our plane they all

gonna start the engine so they all gonna

share this start engine Behavior the one

all all gonna have this functionality

what we don't know is how each class how

each how each object is gonna start the

engine because they uh they start the

engine in a different way the engine of

of a car is different from an engine of

a truck or form of Lane so we know that

each each is gonna

not that each class or each object is

gonna start the engine but we don't know

how they're gonna start the engine so

here we go inside the actual uh feelings

inside the the function which we type

the text the car is starting the engine

the truck is starting the engine and the

plane is starting the engine so they

differed in how they start the engine

but you know that they all gonna they

all gonna start the engine so this is

what interfaces allows us to do

you can define an interface if you know

that uh multiple uh related or unrelated

classes or objects are gonna have a

function shared between them but you

don't know how each particular class or

object is gonna em is gonna uh how is

gonna how it's gonna have the actual

quad uh how it's gonna write the actual

code for that uh that behavior so you

don't you don't care about the how you

want to care about the what so we know

that in this case they will start the

engine but they they start the engine in

a different way and you can have here

another class let's say

that a person which creates another

another class and the class is gonna be

called Tesla and in that case again you

can implement the interface because you

know that uh the Tesla is also going to

start the engine you don't know how it's

going to start the engine but you know

that uh is going to start the engine and

how it's going to start the engine is

specific to a Tesla car so this is what

interfaces allow us to do we can Define

uh some shared Behavior between

unrelated or related classes and that

the actual code you don't Define any

code inside the interface you can put

you actually can Define here a property

but uh uh usually you just Define uh the

the function the function name some

parameters and the actual code goes

inside the class which implements that

interface let's let's actually create

here a class Tesla so we put here

let's put this a little bit up

so we put here class Tesla is gonna have

also a Val name

it's going to be a string

Naval color

and this is also going to be a string so

now we can Implement our engine

interface so we don't know how Tesla is

going to start the engine but we know

that it's going to start the engine so

we we Implement our interface put call

on the name of the interface and now we

need to implement the start engine so we

click on that

and we have not yet implemented let's

put our print line here I'm going to

press Ctrl V

and here's gonna say that's like

starting the engine

so this is what interfaces are and

see you in the next video

because I'm going to end the video now

so here you should say a Tesla is

starting the engine not the Tesla

so I collected that

so I thought it's a good idea to show

you a practical example using Android

studio where interfaces are used because

the when I first learned about

interfaces and after classes I found

them very confusing because they are

very very abstract and you don't see

when when you somebody teaches you those

Concepts you don't see how where and

where you're gonna use those uh those

Concepts interfaces and Abstract classes

and for that I'm going to open Android

Studio

to show you a practical example using

interfaces and also organize Android

Studio to show you a practical example

where we're going to use

abstract classes so I opened Android

Studio you don't need to it looks

similar to our IntelliJ because this

IntelliJ IDEA just that is it's Android

Studio so we will click on my

application

and here we have this thing called main

activity we don't need to worry about

this because as I said I just want to

show you where interfaces are used in

Android Studio to get to get an idea

about where they are used because I said

they are very abstract

foreign

this thing called activity.main This is

where we put our

UI this is where we put our buttons this

is where we put our list views uh this

is where we put generally our UI so as

you can see we have two buttons here and

they have an ID

again I'm not gonna go into this but if

you click here on design you see that we

have a login button and a sign up button

so this is the UI part of our app now

I'm gonna click on this main activity

and this is the part where you're gonna

put our code and now I'm gonna link

those UI buttons so let's go to quad so

this is the code for creating those

buttons

and I'm gonna link those buttons because

they have this ID here login button so

you have here Android ID

and if you come from Android Studio you

already know all of this I'm gonna link

those UI buttons with some objects

buttons by typing some code so I'm gonna

Define up here

foreign

called the login button

and it's going to be of type button so

this is a class button

then I'm gonna Define another later need

because I'm not gonna initialize it

right there so Laten it again

sign up button

and it's gonna be also of type button so

types like we have types for uh for our

numbers or for our just that this is a

class and this rep this class is a

button objectives and now we need to

link those those objects that we created

here

with those buttons that we have here

those UI buttons so to link those login

button and sign up button

and to do that to go down here

and I'm gonna I'm gonna go I'm gonna do

this in the longer way and here we type

login button again you don't need to

understand all of this this is just to

illustrate how interfaces work I'm gonna

put equals

and now I'm going to type find view by D

and this function find view by ID is

gonna link our button object that we

created here with our UI button

that we have

that we have created here and it's going

to be linked by its ID by this ID that

we have here login button so if you go

here hero Type R so R is this thing

which uh

again don't need to understand all of

these arrays is used to to get that ID

to link our button object with our

button UI so if you put r dot ID that

and we have our uh our two IDs so we

choose login button

so now we have linked our UI button with

our button object so we linked the UI in

other words with the code now let's link

also the setting up button so we type

here saying a button

equals find view by ID you you also type

r dot ID Dot

sign up button again you don't need to

type this code because probably you

don't even have Android Studio this is

just to illustrate something

now

to illustrate how interfaces work let's

say that I want to

let's say that I want to

to do something when this login button

is clicked and I want to do also

something when design up button is

clicked and it and here interface is

coming to action

what you can do is you can say login

button

Dot and there is a function called set

on click listener and this function what

is doing is

basically setting a listener on our UI

button so that when you click on that

button the function called on click is

going to be executed and the code that

is going to be in that function is

particular to each to each button so

you you you're gonna set the interface

so you types don't click listener and

hero type also this illustrates uh the

object also this by by this example we

also illustrate how the Anonymous

classes work so you type object

and now I'm gonna inherit actually gonna

Implement our interface on click

listener and this only listener

interface has a function called on click

and here to put curly braces and press

enter and now here we have this

underline

and if you click on it you have this red

bulb because it wants us to implement

to implement that on click function so

we're going to implement that

and it says to do not get implemented

I'm not going to do the same thing for

the

for the sign up button so I'm gonna copy

this

I'm gonna paste it down here

and I'm going to change this to sign up

button so now each individual each

individual button has us on has its own

on click listener interface and the code

which is going to be executed inside the

one click method because this function

is going to be triggered when you click

on the button is particular to each

button so this button is going to log in

the user this button you're gonna put

here the code to sign up the user but

you're not going to do that we're just

gonna

we're gonna uh show some text on our

emulator so we type here to show the

text toast

dot make text here you need to pass the

context so don't worry about all of this

main.activity here you need to pass uh

how how long this is gonna show on the

screen so we choose a short

and we put that show to show this on the

actually here we need to pass the text

so what text is going to be shown on the

screen so here we're gonna say

login button

clicked

and now here we need to pass how long

it's going to slow so you type those Dot

short so if it's going to be shown for a

short time and then we need to call the

show functions to show this on the

screen and I'm going to press Ctrl uh C

to copy this and I'm gonna paste inside

our on click function in our interface

that we have here for the sign up button

and this is gonna say

that

sign up button was click so sign up

button

clicked

now in Android Studio we have what is

called an emulator and this is basically

a virtual phone and we can run you can

run our code and we can test our code

and we're gonna see when

on that emulator those buttons and when

you're going to click on those buttons

this code that we Define here for for

those buttons because we linked them so

we linked

those two buttons that have the login

button the sign up button so this is

their UI code this is their XML code

you we've linked them through code here

and when we click on them the Code in

each particular object is going to be

executed so now if you run our code go

up here and I'm gonna create now the

virtual font to see our buttons and I'm

going to click on them to see how it

works

I'm going to close those

now it's connecting to the emulator

and this is our virtual virtual phone so

this is the virtual phones and here we

have our UI that we created here

so this is what the users see so this

the what the user say and this is what

we see it's the this is what happens

under the covers and now if I click on

this login button look what happens

it says login button click so it

executes this on click function inside

the login button if I click on the

sign up button it says sign up button

click because it's executing the one

click function and the code inside the

one click function for the sign up

button

and they are as I said linked by the by

the ID so this is what happens under the

covers this is what the user see

now

this is where interfaces can be used

because if you think about you can have

multiple uis different uis text views

lists or

items into the items in a list which can

be clicked and for all of those you know

that always go all of them are gonna

have on click function all all all have

this shared functionality to respond to

a click you don't know how that is gonna

how that that one click function is

gonna

execute or what code that on click

function is going to have for each

particular UI uh UI but you know that

each each one is gonna have one click

function what is going to go inside the

one click function for you for each

particular

UI it's up to that specific UI so it's

up to a button it's up to a text views

and so on so this is a over interfaces

are used because we know that they're

going to have a shared functionality of

one click but you don't know how that

functionality is going to be implemented

by each particular uis in our case we do

we know that login button and sign up

button

both both are going to have a one click

both we're gonna are gonna respond to

click events but you don't know what

they're gonna do so this is why you

define the interface and the developers

who write code gonna Implement that

interface and you're gonna write the

actual implementation for that function

which is specific to that object so this

is our discussion and if you find this

confusing

particularly if I'm confusing the uh if

because I touch a little bit on Android

Studio in our discussion don't be

because I just wanted to show you a

practical example where interfaces are

used using Android studio so let's click

again on login button

we get our toast login button clicked

slang a button click I can create

another button which can be used for a

I don't know if you forgot the password

and you you're gonna implement the on

click listener you're gonna override the

you're gonna override the one click

function I'm gonna write some code to

to send uh to send to help help the user

to recover its uh puzzle and so on so we

can see how interfaces can be used

practically in this with this example

so this is our discussion see you in the

next video so now it's time to start a

discussion about object expression but

first let's go down here and let's

create a class to see why you'll need to

use object expression in the first place

so you type here class

and now and our class is going to be

called button

we put the parenthesis to create the

primary Constructor we Define some

properties here the first one is going

to be text is going to be of type string

the second one is going to be also

available it's going to be called ID

it's gonna be an integer

and the last one is going to be called

One Click listener

it's going to be of type on click

listener so this is an interface that

we're gonna create

so let's go down here to create the

interface we type interface on and while

I'm jumping you see that you have this

suggestion one click listener

to create the interface press enter

put curly braces and let's define a

function fun

on click

let's put parenthesis

now let's create some objects with our

class button to see why you'll need to

use a

object expression in the first place so

let's go here

and here we type Val let's call it login

button

put equal and now let's create our

object type button

with parenthesis now let's pass some

values to the properties of the primary

Constructor

and this is gonna have the text to login

for the ID we type some arbitrary

numbers here one two one three

two

and um

now here you need to pass the one click

listener interface but do not we cannot

type here one click listener because

we cannot instantiate the interface so

what can we do to solve this problem

one solution to this would be to create

a separate class here

let's put it actually above the

interface

let's call it click listener this class

class

click listener and this class is going

to implement the interface

so we put here colon and we call our on

click listener interface put curly

braces

and now we need to implement that one

click method that one click function

that we defined actually

inside our own click listen interface so

press ok to override to add on click

function

and we have that

to do we have this to do which says to

do not yet implemented so this is there

to tell us that it's not implemented yet

so let's delete this

all right so what we could do is you can

create here an instance of the

click listener so we type here Val

click listener

equals create the instance we'll create

an object

and now you can pass here the click

listener

so now the arrow disappeared but if I

create a second object let's call it the

sign up button

equals button

let's pause the text so

sign up

for the ID was an arbitrary number

now

in our imaginary or

example that we have here

actually if you think for real scenario

with this button and the one click

listen what we want to achieve here is

that if you have a graphical user

interface

you want that you want to detect that

click so you want to declare detect one

that specific button let's see that this

button object that we have here is

linked to an uh graphical button which

on which you click

because they are linked when you click

on that you want to

call that one click function that is

defined inside our interface inside here

but you want to

make that one click interface we want

you want to make that one click function

particular to particular to that button

which was clicked so we we want to to

implement that uh one click function to

that particular button which was clicked

and the code that goes inside that

particular button so let's say that you

click on the login button it's

particular to that button so that in the

next

when the next button is clicked so when

if the user clicks for when the sign up

button then you know you need to also

have the one click

method called but that one click that

one click function actually I I keep

saying method but the one click function

should should have its own

implementation a particular to the sign

up button so what can we do in that case

we cannot pass the same instance here to

click listener because even if it

creates here another instance of the

click listener it will still have the

same code which is here so if you put

here some code this is code blue it will

be shared among all the objects that are

created so what you can do in this case

is to use an object expression

or also called anonymous

anonymous anonymous classes so what you

can do here we can type object

we put colon and now we

are inheriting from our we are actually

implementing our own click listener

interface so we type object colon on

click listener and we put curly braces

because here we declare and we also

instantiate the the anonymous class so

we need to implement that right there

so by doing this now we where we made

the one click function particular to

each object Which object button so we

can do the same thing here we type

object

and we Implement our on click listener

interface we put curly braces now we

need to implement

for this particular object for the sign

up

or button objects we need to implement

that function so if we type we click we

click on this object

we go to the red light bulb and we type

we press here where it says Implement

members so implement the one click

function and now what we did here is we

made the one click

function particular to each object so

each object now we can put here some

code

code

or we can type here this sign

in

actually let's type login

that was it so now we can have code here

which actually log logs in if you have a

real graphical user interface which logs

logs in the user and here you can also

code to sign up the user when that

specific button is clicked

so by doing by using the object

expression also called Anonymous class

because you don't have

reference you don't have a name for the

class here you just it's just Anonymous

for this

is anonymous so we cannot it does not

have a name so we cannot call it later

or do something because it's created and

also instantiating right there this is

why I need to implement the one click

function and now we made the one click

function particular to each button and

you have more

objects more objects of created with the

class

button they'll have their own

implementation of the one click

function which comes from the interface

on click listener

so this is why you need to use the

anonymous

class or also called object expression

and it's an expression because the value

that the because the object is always

it's created and instantiate it is

actually declared and instantiated at

the same time it's also assigned to the

property that we Define here so this is

why it's called an expression

so see you in the next video

all right so now it's time to start a

discussion about delegation but first

I'm going to add the main function here

now what is delegation delegation means

giving power given Authority from one

instance from one class to another class

and delegation is usually used in in

scenarios where inheritance starts to

break so when inheritance starts to

break when you want let's say it for

whatever reason to inherit from two

classes in that case you can't use

inheritance because with inheritance you

can you inherit just from only one class

but with delegation you can plug in

multiple

implementation of classes in your own

class and let's see how we can do that

so I'm going to create first an

interface called a and it's going to

have a function called print

then I'm going to create another

interface called B and it's going to

have a function print though

then I'm going to create a class so I'm

going to type here class and it's going

to be called first delegate and it's

going to implement the fun the interface

a here

and I'm gonna press Ctrl o to override

the function print here

and I'm going to create another class

called second delegate

and this is going to implement the

interface B

so I'm going to place Ctrl to override

the

function print tool

now I'm going to create up here a class

called up

so class up

now let's say that I want here to

inherit from the first delegate and from

the second regulator so I'm going to

inform the single delegate and if I type

here first delegate

I put parenthesis opening parentheses

closing parentheses

if I put curly braces now I have another

line because I should Mark this as open

so mark this as open and also this as

open because I want to show you that so

now if I want to inherit from the second

class called from the second delegate

because let's say that I want to use the

the print to function in my class for

whatever reason

I can't because I can't only inherit

from one class so if I hover over here

over this underline it says only only

one class may appear in a super type

list so in other words saying that you

can only inherit from one class and that

is all we cannot inherit from multiple

classes now with delegation we can plug

in multiple implementations in our own

class so what I can do here I can say

hey

implement the interface a so I'm going

to inherit it from the interface a but

you're gonna say use for the interface a

the implementation of the first delegate

class so this is what we're saying here

a and the keyword by first delegate

we're saying hey use for the interface a

the implementation which is provided by

the first delegate class and that will

work

then I'm going to put a comma here and

I'm going to say use also for the class

B so I'm going to type for the interface

B so we're gonna type B and we're gonna

use the by key you're also going to type

here by

second delegate so you're saying use for

the interface B the second delegate

implementation and it's gonna use that

and it's gonna be him

happy with that so you're gonna see that

we get no error and now I can override

here both the print function and also

the print to function so if I press Ctrl

o now I can override also the print too

and this is inherently very powerful

because we can plug in here multiple as

many implementations as you want in your

class so this is in the if you think

about in contrast with the inheritance

this is very powerful because here you

can plug in as many implementations as

you want and this is very powerful so

this is how you can use the delegation

in kotlin and next we're going to see

how you can use delegation with

properties

so to use delegation on properties first

I'm going to paste some code here so I'm

going to paste this code here and what

we are having here is a class format

delegate which is inheriting from the

read and write property

and it's overriding the get value and

the set value so we are overriding the

setter and the getter inside our class

and we provide our own implementation so

whenever you

you use the setter to set a value or to

get as a get a value we we are

overriding those functions and we

provide our own implementation next I'm

going to create here up here a class but

first I'm going to delete the code and

the interface that we have here because

we don't need this code

so I'm gonna delete this code and I'm

going to create another class

and this class is going to be called

user I'm going to put curly braces and

I'm going to define the properties

inside the class so I'm going to type

here VAR

first name so what we are doing now is

we're creating a property first it's

called first name and I'm going to use

the by queue we're just going to say buy

format delegate so what we are saying

now is use the

use the the we use the code which is

inside the format delegate Whenever you

set the value or get the value of the

first name I'm going to put the

parenthesis here next I'm going to type

another VAR last name and I'm going to

type here again bye and I'm going to

type format delegate so format delegate

and we put parentheses

so not now what is going to happen is

that whenever you set the first name

properly or we get the name the first

name property is going to use our

implementation that we provided here

this is what you're gonna do and the

same it will be true for the last name

because we are using the

delegation here on properties so I'm

going to create here a user so I'm going

to type here Val

user want to type equals to user

and I'm gonna type here with

I'm gonna type user this is the

scope function I'm going to put curly

curly braces and I'm going to type here

first name

equals Alex

and the last name

equals uh

dominga

now if I output those values in the

console look what happens so if I tap it

here print line

user that first name

and print line let's actually use again

with the scope function type here with

just pass here user curly braces

print line

first name

and print line

last name

now if you run this code you're gonna

see

that it's using the overriding functions

that we provide in our format delegate

so we have in uppercase letters like we

defined here we're gonna wanna whenever

we set about is going to be set it with

the it's going to change the value to

lowercase letter so get Alex and obinka

in lowercase letters so this is how you

can use the delegation on properties and

see you in the next video

so now it's time to start a discussion

about collections but first i'm going to

create a new project I'm going to call

it collections

for the language select link for the

build system IntelliJ make sure to have

the jdk selected and also check this box

to have the main function Auto generated

for us so I'm going to click on create

to create the project

now I'm going to delete this code

because you don't need this code

and also I'm going to hide the project

pane

now what are collections collections are

a group of objects stored together in a

single variable they are similar to

array but there are there are different

in some sense and

they are mainly three collections that

we're gonna look at and this those are

the list the set and the map and the and

those among those

collections they are separated in two

groups so there are multiple multiple

lists and set air maps and immutable

lists set and map and in the motable

means that you can write to that

collection so you can add or or remove

elements to it so the that collection

can grow or Shrink as you add or remove

elements to it and immutable means that

you can only add elements when it when

you instantiate that or we're going to

create that collection you cannot add

later elements to it the size is fixed

so we cannot add or remove element so

this is the difference between immutable

and multiple and we're gonna look at all

of those in this video so we're gonna

look at the list collection first and

we're gonna look at an immutable list so

we type Val I'm going to create a

variable names and here we're gonna type

to create an immutable list type list of

and here we need to specify the type

what kind of data I'm going to put here

and do type name one

name two

and name three now we cannot add or

remove elements from this list because

it's an immutable list but we can get on

Elemental output in the console and to

do that to type it in the line

we type names we put square brackets and

we get the element by its index and the

index also starts at zero so let's get

the first element

so if you're on this code you'll see

name one I'll put it in a new console

so you get name one so this is an

immutable list we cannot add or remove

elements from it after we

instantiate it here what now now let's

look at an mutably so a list which can

within which you can add and remove

elements to it so to do that we just

change this from a list of to a mutable

list of

so we type here multiple

list of

we can also remove the explicit type

here the explicit type declaration

because it's gray out

so remove that because it can be

inferred and to this mutable list now we

can add or remove the elements so I can

type here names

and we have this add function so you can

type add

and here we can

you can pass a new element so we pass

here let's say name

4. now if I uh let's type here names

that for each

print line

not in this footage

name start

for each this for each

opening the line

now if you run this

now you see you have name one name two

name three and name four so name four

was added to our Multiple List because

our list is multiple we can add or

remove new elements so down here I can

I can type names dot remove

and we can remove an element and we can

remove the element I think by

you can remove you can use this function

remove art so we can remove the element

by its index so we can type here the

index

and that is going to remove the first

element so if you run this code

name one is not going to be showed

so as you can see name one was removed

because you can you will remove it by

its index and we can also remove an

element

using this function by typing uh

the elements so we type here let's say

name

two

name two elements and now it's going to

be removed so now if you run this

name two was removed so those are two

functions with which you can remove

elements from the Multiple List so this

is a multiple list you can add and

remove elements to it and the list is

gonna grow and Shrink as you add or

remove elements to it so this is the

Multiple List

now

we're gonna look at a set and I set this

a collection which can store only unique

unique elements so you cannot have

duplicates so if I delete this

so in our

in our Multiple List you can put an

element which is the same

so we can have two elements which are

the same so if you run this

we have two elements which are the same

going to see name one at the end I have

name one name two name three and name

one again so you can have uh duplicates

in our collection on our multiple list

but in a set you can't so but first

we're gonna look at uh first we're gonna

look at the immutable set and then add

automotive ball set so we type here uh

let's delete this

we type here set off

and here you need to buy string

now if you run this

you will see that name one is not shown

two times here it's going to be shown

only one time so you have name one name

two and name three this is because the

set Seto the set is not allowing

duplicates in its uh in its list so

and this is a new multiple set set off

so if I type here names dot add we

cannot add or remove new elements but

you can also use

a multiple set of so we type here

Moto motable

set off

and now we can add and remove new

elements so you can type names that add

so here you can add a new element let's

say

name

so now if you run this

I'm going to name one name two name

three name four

and our duplicate is not strong because

I said a set can only

can only store unique elements not

duplicates but if I add objects that we

create look what happens so let's create

some user objects but first let's create

the class user

and it's gonna have a

Val name is going to be of type string

no let's create some user objects here

and we're gonna add to our multiple set

so we type here

wall user one

I'm gonna type here

user

I'm going to pass some generic names

like

name one and let's control the a few

times

let's change this to name two

five

and let's add here a Val

user

six

equals user

and let's type here Alex

let's press Ctrl d

and let's change this to

user 7.

now let's add those to our

multiple cells so let's change now the

type

so it's gonna store user objects

so we're gonna pass here

user one I'm going to speed up now this

a little bit

I'm gonna press Ctrl tilde format the

code

now we can add this we cannot add here a

name for because now the type is user

now if you're on this let's actually

change this print to it dot name

and let's let's run our code now

and we have name one name two name three

name four Nerf name five and have Alex

two times so why you have duplicates

even though I said that sets can only

store unique elements this is because

previously the set actually what is

doing to determine to if to if it has

duplicates is using the equals to

function that we talked about in our

video about data classes so when we

typed only strings then it was using the

equals to function which was overridden

inside the string class to determine if

there are uh if there are equal strings

but now because our class doesn't have

the equals to function over it then it's

in its class it's not it's using the

equals to function which is inside the

ending class so you cannot determine if

two users are are equal and to change

that just let's hide the console

and just put the data keyword here

because now this will generate the

equals to function the the the two

string and

the harsh code so now if you run this

now we have Alex only one time because

now it's using the equals to function

which was uh Auto generated by by uh by

the class user because you put here the

data key or so this is a good thing to

have in mind that

this model set is using the equals to

function to determine if the object that

you add to to yourself are equal so

having this in mind now let's look at

Maps so I'm going to close this

I'm gonna delete this

let's delete this

let's delete this also

now let's look at the next collection

that you can use in kotlin and that is

the map collection and use collection

when you want to store key value pairs

so I'm going to create a variable it's

going to be called users

and I'm going to put equals and I'm

going to create first animated bottom up

so we type map off

map

of and here you need to specify what

kind of type is going to be the key so

the key is going to be an integer but

you can put any class you want here

and the value is going to be a string so

I'm going to store the ID and the name

of an user

and to Define our element in pairs here

we type first the key and then we use

the to keyword to map our kit to its

element so 2 is value actually so type 1

2 and now the value let's first type

Maria so

now we have one element stored in paired

and the key is one the value is Maria

next I'm gonna do the same for the next

one so I'm gonna type here

two

two

the keyword tool

Alex

and then

I'm gonna type three

the keyword tool

so inside you need to type the value now

now type John

now to get one element from this map

with I print line

we type here users

square brackets

and we type we get the element by its

key as you can see by this hint key so

we get let's say the element one

so now if you run this code

we get Maria because Maria has because

this key code is for the for the key one

the value mapped the value which is

corresponding to this key is Maria and

if you change this to two

gonna get Alex if you run this

so this is an immutable map in this map

you cannot add or remove elements but if

I declare this as

a multiple map so let's change this to a

multiple map

so we type here mutable

map off

let's delete this from here

and let's pass here and

and string

now here now

so you need to put this in parenthesis

here we can add new elements so we can

type users

actually first let's Loop through our

mob to see

our elements and I'm going to use the

for each function to Loop so we type

here users dot for each

and we choose this one

and inside our Rook without we are the

print line

and

I'm going to put quotation marks and

here we're going to put dollar sign I'm

going to type T and T is the key and

dollar sign U and U is the value so now

if you're on this

you get one and Maria to and Alex Three

Angels so we have the key and we have

the corresponding values and you cannot

have duplicated keys and this is very

important to know you can also add or

remove elements

from this map so to add an element you

just type users

and we need to specify the key

so we put square brackets so we type

user square brackets and then put a key

let's say five

and we put here equals to and here we

need to pass a value so the value is

going to be let's say

Vlog now if you run this

we get one and Maria two and Alex three

and John N5 and the corresponding value

Vlad so Vlad has the Kiwi the key value

five

so you can also remove an element from

our map so we type users dot remove

you know you you here for this remove

function you pass the key of the of the

element that you want to be removed and

let's say that I remove two so I remove

Alex from our map so if I run this quad

now Alex you know it's not going to be

shown here

because it was removed on the line for

so Alex was removed so this is how you

can use the immutable and multiple Maps

this is how you can add and remove

elements and this is how you can Loop

through our map and this is our

discussion about Collections and see you

in the next video now it's time to start

a discussion about collections

operations and we're going to look at

the first operation and that is

Transformations and we're going to start

with the first one called mapping so I'm

going to create a new project

I'm going to call it

collections

operations

IntelliJ then click on create

we go on the left hand side

click on kotlin right click new I'm

going to create a new file I'm going to

call it Main

I'm going to create the main function

now

what are transformations as the name

implies Transformations are some

functions which which you can change a

specific collection

and we're going to look at the first one

which is called mapping so I'm going to

create first

a collection so I'm going to type Val

numbers and it's going to be a set so

I'm going to type numbers you're gonna

equals to set off

and here we're gonna Define some values

like one two

three four five

let's press Ctrl alt format the code

now I'm gonna add the print line here

now to use the mapping collection

transformation we just type numbers

and one important thing to know here is

that those

transformation functions are Lambda

functions so if you don't know what

Lambda functions are I suggest you to

watch the section which is called Lambda

function to First understand what our

Lambda functions because it will be it

will be very hard for you to understand

how this how those Transformations work

without understanding how Lambda

functions work

now

here we're gonna call our our

transformation function which is the

first one which as I said is mapping and

the function is called map I'm going to

explain immediately what it's doing and

as you can see it's a Lambda function so

we're going to press enter

and we we are inside the curly braces

and here we're gonna Define our

transformation so what we are saying

basically here is

do whatever I I type here in this those

uh

curly braces apply to every element of

the of this cell of this collection so

if I if uh now to refer to to refer to

the parameter uh uh to the specific

parameter from the numbers uh uh set we

type it

and I'm gonna type it times

10. so what we are saying here is

do this transformation so change this uh

change this uh this set by uh more

multiplying every element inside the

this uh this collection inside this set

by 10. so if you run this now every

number is going to be multiplied by 10.

so if I run this

now we get instead of one two three four

and five we get 10 20 30 45 so what this

map function and what this uh it times

10 did is is it transformed it changed

our set into into what it it was changed

by the expression that we defined here

so we said that I want every element we

said here to be multiplied by 10 and

then we display them here

we can go even further we can put here

an if statement so we can type here if

let's say it so it refers to the to a

specific element in that list in that

set so if it equals equals so if equals

three I'm gonna say

we're gonna say here

it

times

100.

so what we're saying here if one of

those elements inside these numbers set

is equal to 3 then multiply it change it

so change it by by the by this

expression so multiply it by 100 and you

can also put the else part so we put

else you want to put it times

so if the else part is true if the else

part is executed we're saying if the

number is not equal to 3 from this list

then multiply the number multiply the

number by uh by 10. so now if I run this

look what we have here

so we have 10 20 and we have 300 here

this is because this transformation now

is using the if and else statements that

you define here so this is what the map

Transformations allows you to do you can

change you can transform a specific

collection by an expression that we

defined inside the curly braces of the

map function

all right

now let's look at how we can can use the

map transformation to with another

collection unless you'll see how can you

can use the map transformation with a

map so with uh with the with the map

which has a key and a value so to do

that we just type here

Val

numbers map so I'm going to type file

numbers

equals I'm going to type map off

and here I'm gonna type

key

one

two

one

then

key

tool

two two then

key three

two

three

and

finally key

for

two

four

now if you wanna use the map map

transfer transformation function on the

map map collection you you you can use

it either on the keys so you can use it

on the keys or on the values so we can

type here print line

number map

number map so we choose here

Dot and we choose map Keys now so choose

this one map key Lambda expression so

map keys that one and here to type eat

dot key so get the key Dot uppercase so

that is going to upper case all the keys

inside our map then I'm gonna add an

another print line here

I'm gonna type number map

Dot

map

now choose

map values

and we type here it dot value

so the Value Plus

it dot key dot length

now if you run this though this

expression is going to be applied to all

of our keys inside our numbers map that

we Define above and the second

transformation is going to be applied to

all the values that we defined inside

our numbers map so if you run this code

now I'm going to see the

the keys being first uppercase and then

we're going to see the keys plus plus

the length so you see

first I get 10 20 300 for 40 and 50 for

our first transformation that we have

here

then we have our second then we have our

transformation on the map on the map

keys so we have we have the key and then

we have the value we have he and the the

key is uppercase because we said uh to

to do that by using this expression it

so it uppercase all the keys as you can

see here and then you see the

corresponding value for the key

after that you see

you have numbers map map that value so

you have it that Value Plus e dot key

dot length so we have key

and I have one and we have six we have

key two and half seven so the key the

value of the key is uh is uh to the

value of the key it's added the length

of the

so to the value that we have here we we

have added the the value length of the

string key that we defined here so this

way I have uh we have uh two then we

have seven then we have eight and then

we have nine

so this is our and you can also

you can also do for the uh map for the

map transformation and we type here

numbers

Dot map

and we choose this one index not node

and here we choose we have two

parameters first is the index and second

is the value so I'm gonna choose the

name for the first one index and for the

second one let's call it uh

value then you have the auto and here we

have we're going to Define our

transformation of our expression which

is going to be applied on our numbers so

here I can use the index so again I'm

going to type here an effects if a

statement so if

index

equals equals zero then I'm gonna

assign so I'm going to transform to the

to that we're going to put the value

null

to the index 0 else

I'm gonna type index

times value

so else you're gonna uh multiply the

index time times the value so if you run

this

we get 2 6 12 and 20. so for for the

first one we get nothing because we for

the first index we signed null

and then uh it's using the else part to

to transform our numbers list and it's

multiplying the index by the value so we

we first uh

we first have this indexed uh

zero multiply the here we have null for

this because we checked here so we have

not so at the index two we have one

times two and uh we have two then here

we have add indexed two we have two

times three so the index is two the

value is three and uh we we get uh we

get six here and so on so this is how we

can use also the index if you want in

your transformation now we're going to

look at the next transformation that you

can do in our collection and that is

zipping

now it's time to look at the

transformation function called zipping

so I'm going to add a comment here

called the

zipping

and I'm going to declared to list I'm

gonna call the first one colors

no particles

I'm gonna type please stop

I'm gonna type red

Brown

and

Gray

and I'm going to create another list

called the animals

stuff

I'm gonna type here Fox

beer

quotation marks

and uh

wolf

now

what does the zipping or the zip

functions allow us to do is to create

pairs of elements with the same position

in both collections so we can create

pairs with red we can create a pair of

fox and red a pair of bear and brown and

a pair of wolf and gray

and you can do that just by typing here

print line

and we have you can do this in two ways

to type colors

and you can type Dot

dot zip and you type here animals

and

if you run this

you'll see them in pairs so you're gonna

see Fox thread

bear brown and so on so you see here red

fox brown bear gray also the zip and the

zipping is used to create Pairs and the

pairs are at the same position in both

on both collections

or if you don't want to use this Z

function with parenthesis you can type

here colors zip and animals

and type here any models

now if you run this code you're going to

see the same output

now if you want to use a transformation

with the Z function you can type here

print line

you can type colors dot zip

and I'm going to use the parenthesis

animals

and animals and now I'm going to put

curly braces so the Lambda function here

I'm going to type

animal

for a let's put for scholar

and animal I'm gonna put the arrow sign

and here I'm gonna type day I'm gonna

but I'm gonna put first

quotation marks and quite the

and I'm gonna put dollar sign

and we're going to refer our animal uh

parameter that we defined here so we

type here there

animal

Dot and we call the replace first chart

function so type replace first chart and

as you can see this also has uh I think

along the

function and inside of this we type it

dot uppercase

uppercase and this is gonna uppercase

the first letter of the animal

uh

with the animal that is defined here and

I'm going to type yes

dollar sign

color

right now if you run this

you're gonna see the force and the f is

uppercase because it's uppercase by this

expression that we have here

it replaces and this it refers to the

animal uh an animal uh variable animal

variable that is defined here and it

refers to the first letter so it says

eat that uppercase so uppercase the

first letter

then uh then uh return that to the to

the animal uh any more variable and we

have the fox is red so I have the color

and then we have the bear is brown the

wolf is gray and all the animals start

the capital letter like we Define here

in this

in this expression

so this is the zip function so this is

how you can use the Z function to create

Pairs and also you can use the unzip

function to unzip a list of pairs so

first let's create a list of pairs so

I'm going to type Val

number pairs

equals

I'm gonna type list of

and here we're going to put

one

two

one

two

two

three

two

three

four

two

four now if you want to unzip those

pairs that we created you can just type

here print line

number pairs let's first display them as

pairs

and the necklace print line and so let's

type number pairs

dot unzip

now if you run this you're gonna see

them first in pairs and then you're

gonna see them unpaired because we use

here the anzi function so as you can see

here you have first one and you have one

two two three three four four so you

have in pairs first

and then we have them separated so we

have first one two three four

and then we have the numbers one two

three four so did you we unzip them we

separated them they're not pairs now

anymore with this unzip function this so

this is how you can use the NZ function

now it's time to look at the next

transformation that we can use and that

is Association now what is association

association Transformations allows you

to build maps from The Collection

elements and certain values associated

with them

and different Association types the

elements can be either keys or values in

the association map I'm going to see

what this means and the basic

Association function is the association

associated with which creates a mob on

which the elements of the original

collection are keys and the values are

produced from them by the given

transformation function so let's see an

example with uh with this so I'm going

to delete this code because

it will make a doubt but hard to

understand we could have so many things

here

and I'm going to type Val

numbers you know put equals

I'm gonna type really stuff

nanotype here

one

two

three

and four

now I'm going to type here print line

numbers and we are going to use the

association associate with the function

so we type associated with

and this is going to produce as I said

previously it's going to produce a map

in which the elements of the original

collection are keys so the keys of this

map are going to be those elements and

the

values are going to maybe are going to

be produced by what expression we Define

here inside the parenthesis so I'm going

to Define here

it so I'm going to type it

dot length

so now if you run this you're gonna see

the keys being one and then I'm gonna

see the value being the length of that

specific element so if you run this

we get the key one the value three the

key two the value three then you get

three the value is five then you get

four and the value is four so what has

happened now is that the associate with

function created the map in which the

elements the key the key are the

elements of the collections so those are

the keys inside our map and the values

of uh those uh with the association with

the associated with those keys are

produced by the expression that we

Define here by its dot length so

basically gonna use the length of that

specific element and it's going to be

the value so this is why I have one

three two three three five and so on now

for

building maps with collection elements

as values there is a function called

associate by it takes a function that

returns a key based on an element's

value and if two elements keys are equal

only that last one remains in the map so

let's look at this one so I'm going to

type here

print line

I'm going to put here numbers

so we type numbers

dot associate by

and now here going to define the

expression which is going to build the

key because now the the associate by

function is going to build a correlation

which the elements now are not Keys like

we had previously but they are values so

here in the expression need now to

Define how the key for those respective

values that we have here is going to be

created so we type here it

DOT first

dot uppercase

so this is what what is this is gonna do

it's gonna create a key

it's gonna use one let's say that we

have element one

and it's gonna take your element one

it's gonna take the first letter of the

element one and it's gonna uppercase it

and you're gonna have o and as the key

we're gonna have o and as the value

we're gonna have one and uh for the next

ones uh it will be the same so now if I

run this quad

foreign

elements are the values here so they are

the values previously The Collection

elements what the key is and the the

keys were builded by the expression that

we defined here by this so we have o and

we have t and we have four and we we

have uh t uh only one time and we don't

have two entry because uh you cannot

have duplicated keys in a map so this is

why you only have one only one t here

so uh

this is associated so this is

Association now associate by can also be

called with a value transformation

function and to do to do that you just

type here print line

numbers

that associate by

now we need to

apply a transformation now on the key

and on the value and how gonna how gonna

do that

we can type here actually let's delete

this because we don't need curly braces

we put parentheses and we type here key

and we have a suggestion key selector so

we're gonna apply the First

Transformation on the key so we type key

selector that named argument and we put

curly braces

and here we type it so the key DOT first

dot uppercase

so now what we're saying here apply this

transformation

for every key

in in in the map that is going to be

built so apply this transformation for

every key that is in the map that is

going to be built so we're saying to

uppercase uh to take the first letter

and uppercase it then we also need to

apply transformation for the value we

put

comma here at the end of our curly brace

and we type here

value

you also have a name argument you put

again curly braces

and here we're gonna type it dot length

so this is the transformation that is

going to be applied

to all the values in the map that is

going to be built so this is the

transformation so this is how all the

keys and the the in the map that is

going to be built are gonna be uh change

so the first letter is going to take

this thing is going to take the first

letter of the keys and it's going to

uppercase it

this will uh

we'll take the length of the the all the

values in the in the the in the in the

map that is going to be built and it's

gonna display the the length of that

element so if you run this

we get 0 3 T 5 F4 so we have the first

letter uppercase and then we have the

length three then we have t we have the

letter upper case and we have the value

five the length of that uh uh element

then we have F so we have a four and we

have the the the transformation apply on

that element on this element on on four

and we have the length and we have four

so this is how we can use transformation

Transformations with the associate by

function and the next we're going to

look at the next transformation fact

function and that is flattened now it's

time to look at the next transformation

function that you can use and that is

the flattened function but first let's

see why you'll need to use this

flattened function in the first place

so I'm gonna add the comment here called

flatten

and I'm going to create a variable here

so I'm going to die Val it's going to be

called numbers

sets and this is going to be a list with

list so I'm gonna type here

list of

and inside the list I'm gonna Pro I'm

gonna type set off so we have a list

inside the inside of our list

and I'm going to provide some values to

our set of like one two three

then another set of

four five and six

then another set of

seven

eight and nine so

what we are having now is we have a list

within a list where the index of this

list is it's its own list so we have a

list of sets of ins

now if you want to Loop through this

list if you want to display the elements

in this list uh it is not that easy

because you will first need to Loop

because let's say that because what we

have here

is what is called a multi-dimensional

array so more specifically what we have

here is a two-dimensional array and at

the index 0 we have a list at the index

one we have another list and at the

index two we have another list

and to navigate to this list we need to

first get inside that list that is at

that other specific index to Loop to

that list and then we need to move to

the next index and to to look to that

list to that next list which is that

index

but now let's see first how we can

access one individual element from our

two-dimensional array so from our array

of arrays and to do that to type here

print line

gonna put numbers set

and if we we put square brackets and we

Define the index from uh which from

which list we want to access the element

and now I want to access the element

nine and the element 9 is stored at the

index two so we put two

and let's say that I want and I want to

access as I said the the element 9 now

to access the element line I need to go

within this list and use the index to

get the element I and element 9 is

stored at the index two so I'm going to

put square brackets two

but this for whatever reason doesn't

work with a list of sets and we need to

change this to array of

array of also those

also change it to array of

array of also array of an hour

everything will work fine

so array of

so if you run this so now we are

accessing the

index tool where the array is stored

where our number is stored and we get

the element two we get element nine so

now if I run this we're gonna see nine

autopilot in the console

so we get nine so this is how you can

access one individual element from our

multi-dimensional array so I'm going to

delete this I'm going to change it back

to

to a list of and to set off

so let's type a list of

set off

set off

and set off

and now let's see how we can Loop

through this type to the dimensional

array and to do that to type here four

and type numbers I type specific numbers

because this is gonna as as you can see

um gonna hold on hold the numbers the

set of numbers four numbers in number

sets

so now you we are you we are getting the

as you can see here this is of type is

the the type set of ins so this uh

basically gets the element of the number

set index let's say zero and it gets the

first set and now we need to Loop

through that set so another four

number

in numbers so now we are looping

through the list through the set which

is inside the list of the index zero I

didn't zero

and you can add the print line to add to

do output that number I'm going to type

here number and also I'm gonna add the

space here so I'm gonna add the print

line here and I'm gonna type here four

or slash

n

now if you run this

as you can see you have on the output

one two three then we have a space four

five seven six then seven eight and nine

but uh we achieved this by using a loop

and then we use another loop so we have

a loop inside the loop we have a nested

Loop in order to get to the

to this uh two dimensional array because

as I said the in the index of this array

is its own list so it contains the list

now with the flattened uh with the

flattened function what you can do is

that we can convert this two-dimensional

array in one dimensional array so you

don't need to use this nested for Loops

in order to Loop through to them it will

return the nested the nested

uh is it to private it will provide

access to the nasty collection elements

without you needing to Loop through this

two-dimensional Ray it will it will

basically

take that two-dimensional array and it

will convert it in one dimensionally and

you will not need to create to have this

nested Loop inside you have to have this

Loop inside the loop in order to to

display the number so to do that to type

here uh Val

numbers let's say flatten

equals to

number sales so our our list to it list

that

flatten

now if you look at the type here now

this is a list of fins you no longer

have a list of sets of ins so this is a

very very uh informative because it

tells us that this now is just simply a

list of ins is not a list with the set

which is which is uh which itself is

also or a list of events so

this converts that two-dimensional array

in one dimensional array and because of

that now it's much easier to get one

element from our list because now our

list is not a two-dimensional array and

it's just one dimensional array so if I

type here print line

and the

let's say that I want to get the element

nine I'm going to type here uh

numbers flatten

square brackets and I'm going to type

here eight because it's stored at the

index 8. so now if I run this

so we get 9 here and as you can see this

is much easier than uh with the the two

that dimensional array so this is what

the flattening function is doing is

converting the

the two dimensional array or if a

multi-dimensional array in into one

dimensional array so now let's look how

we can Loop through our uh one dimension

array

so let's delete this

and if you want to to Loop through this

you just type 4

number

in

numbers flatten

and we either here a print line

number

and

let's comment this code

and if you run this code

now get the number displayed one after

another so get one two three four five

six seven nine and we got this because

our function our flattened function our

transformation function is transforming

it's converting our two-dimensional

array in one dimensional array so it's

easier for us to get the elements into

loop loop through them so this is what

the flatten function is doing and next

we're going to look at the

string representation and the functions

that you can use uh what we for the for

the string representation

I'm going to close the console

now it's time to start a discussion

about string representation so if you

need to retrieve the collection content

in a readable format you can use

functions that transform the collections

to Strings and there are two functions

to do that the join to string and the

join two The Joint to string build a

single string from the Collision

elements based on the provided arguments

the join 2 does the same but appends the

result of the given appendable object

and what that means is that it's gonna

add the text to the object on which that

function The Joint function is called

you're going to see an example with this

immediately

and now I'm going to create a variable

called numbers while numbers

I'm going to put equals I'm going to

call it actually numbers strings

equal

and I'm gonna type here list off

and I'm gonna put here

the text one

two

three

and four

now if you want to retrieve the

this if you want to get the string

representation of this list

if you just type here print line

and type you put your numbers strings

and if you run this

we get one two three and four but this I

I think is calling the two string

function it's not because we're not

using the joint to string function and I

don't like those

square brackets I want them to be

separated by comma with spaces to get

the to get data just type here

print line

so let's

put here print line

and to type numbers to string

Dot

joint to string

and uh we don't we delete this and put

parenthesis

now you throw on this

you'll see now the single presentation

of our list they are going to be

separated by comma when we do is going

to have a it's going to have

spaces now let's look at the Joint 2

function and for that

I'm gonna Define another list down here

so I'm going to type here let's press

Ctrl alt for model code

so I'm going to type here uh

Val

is going to be called list string

and now I'm going to use something

called

a string buffer and this is like the

string type is a little bit different

but it's gonna allow us to do

is going to allow us to present how the

joint function over so string buffer and

we put parenthesis and here we Define

our text and we're gonna type the list

of numbers I'm going to put colon so the

type here the list

of numbers

colon

now to use the join 2 function just type

here another print line

we we type number string

number strings dot join tool

and as you can see this receives a

buffer and this is the buffer that we

have created here so I'm gonna

press enter and we're going to type I'm

gonna give us the argument the listing

that we created

and what is this this is gonna do is

gonna append this text that we have here

in front of all of our elements that we

have in our list number strings so now

if you run the squad

as you can see it added the text the

list of numbers that we have here to our

number strings in front so we have the

list of numbers one two three and four

now

if you want to build the

custom string representation you can

specify its parameters in a function

arguments which are the separator prefix

and postfix and the result will start

with the prefix and end with the with

the postfix and the separator will come

will come after each element except the

last so let's see how we can do that

so

um I'm gonna use

I'm going to use the number numbers to

string so I'm going to type here print

line

numbers to string

and we type join

join to string and we select this one

with the you select this one with the

which with the separator parameter

and the type is separator so now we

Define the what is going to be the S the

separator here and we're gonna put

quotation marks

and we're going to put

this so this is going to be our

separator between our elements next

we're going to define the prefix so we

put we put here

comma type prefix and now I'm going to

put the prefix and the prefix is going

to be the text start and colon so I put

quotation marks start

and colon

and we also need to provide the post fix

and to do that we put another comma here

so those are named the arguments which

are

defined in this files and join the

string so we type here

postfix

and we put

colon

and

now if you run this you're going to see

them separated by this separator that we

Define here is going to have the prefix

start and it's going to have at the end

this post fix which is comma and N so

now if you run this quad

as you can see we have start and we have

one then it's separated by the sign that

we defined here then we have 2 3 4 and

we have the end so we have our post fix

that had defined here now

for bigger Coalition you may want to

specify

limit so a number of elements that will

be included into the result and if the

collection exists that size with that

limit all the other elements will be

replaced with a single value called the

truncated argument so let's see what all

of this means so let's say that I have a

list called the

numbers let's type here Val numbers and

I'm going to create a range from one

to 100 so what we have here is 100

numbers

and let's say that I type here print

line

but first let's convert this to a list

so I'm going to put this in parentheses

so you put this in parenthesis

and you put at the end that to list

and this is going to convert now our

range into a list and as you can see the

the type is a list of ins and now if I

type here numbers

and if I run this code

when I get all of our numbers displayed

now with as I said with the join join

tostring function we can specify a limit

and also a truncated this is going to be

the thing which is going to be sold out

after our number limits so what I can

type here is I can type join to string

and uh which is also

one with parenthesis so hero type limit

the named argument limit and let's say

that I don't want to get all the numbers

as you can see here from to 100 and I

want to get only the first 15 elements

to do this you just type here limit 15.

then we type truncated and that is going

to be that this trunky thing is going to

be showed after our limit number so I'm

gonna put here uh quotation marks

less than um sine dot dot and uh

greater than PSI so now if you run this

look at what we have in the output

now we get we only have the first 15

elements and then for the next elements

we only have this uh

string that we typed here this is this

expression that we have here and we can

change this if you can change this let's

say to 25 so if you run this now you'll

see only the first 25 elements displays

foreign

to string with the limit name argument

and the truncated now let's say that you

want to customize the representation of

elements of the elements themselves you

need to in that case you need to provide

the transform function so what we can do

is that we can type here print line

let's say

and I'm going to type number strings

put dot join to string and choose this

one with curly braces

and here I'm gonna Define how this uh

this uh

list is going to be transform so how's

going to be changed so I'm going to type

here

quotation marks element

and I'm gonna put

colon

we're going to put the dollar sign

and put it

dot uppercase so we're gonna uppercase

all the elements

so if I run the code now

get element and get one

so you can get one uppercase an element

to get two uppercase element three

uppercase element four uppercase so this

is how you can use the transformation uh

we can this I can transform your list we

using the joint to string function

so this is our discussion about the

string representation and now I'm going

to move to the next sections so I'm

gonna end the video now now it's time to

start a discussion about filtering so

what is filtering filtering is one of

the most popular tasks in collection

processing in kotlin filtering

conditions are defined by predicates and

that is Lambda function that take a

collection element and return a Boolean

value true means that the given element

matches the predicate false means the

opposite

the standard Library contains a group of

extension functions that lets you filter

collection in a single call those

functions leave the original collection

unchanged so they are available for both

multiple and read-only collections to

operate the filtering result you should

assign it to a variable or change the

function after filtering

now the basic filtering function is

filter when called with a predicate

filter Returns the collection elements

that match it for both list and set the

resulting Collision is a list for map

it's a map as well

now let's Define a variable here call

number so let's type in val numbers I'm

going to put equals to list of

and I'm gonna type here

one

two

three

and four

now if you want to filter this list what

you need to do is let's create another

variable here called

longer

then

three

I'm going to put equals I'm going to

type numbers

Dot and we type filter

so filter

and we choose this one

and here we defined how our list so we

will Define the expression which is

gonna determine how our list is going to

be is gonna be filtered so I'm gonna

type here it so the element dot length

let's say greater than

3.

now if we I add the print line down here

print line

now this list is going to contain all

the elements from our numbers list which

are longer than three so if I put here

longer than three and if I run this

we get 3 and 4 because those are the

only string numbers which have a length

longer than three so I'm going to close

the console next let's see how we can

filter them up and for that I'm going to

create another variable called numbers

map

foreign

here the key is going to be a string key

one

the value is going to be one

comma then

key to

to

then

key three

two three

and now I'm gonna put key 101

two

101 the value

now if you want to filter now this map

I'm going to create another variable

called filtered

mop and it's going to be equal to our

numbers map but it's going to be filter

so I'm going to type DOT number snap

that filter

and now I'm going to filter filter the

map both by the key and by the value so

we type here it dot key

dot ends with so we we want to filter it

first by the key and the one that key to

ends with a specific character and that

character is going to be one

and so we put the operator and we want

the the value to be greater than 100 so

we put here it

dot value

greater than 100 and the only key which

satisfy this condition that we find here

it's only is this key so now if I add

the print line here

and if I type filter map so the map

which was filtered using the expression

that we defined here

now if you run this you're gonna see

gonna see

this in the output because this is the

only one which satisfy the condition so

you get key 101 and we get the value 101

so this is how you can filter them up

using the both the key and the value now

the predicates in filter can only check

the values of the elements if you want

to use element position in the filter

use filter indexed it takes a predicate

with two arguments the index and the

value of an element

to filter correction by a negative

condition use filter not it's written as

a list of the elements for which the

predicate yields false so let's see how

we can do this so I'm gonna declare two

variables down here

file

filtered

index

is going to be equal to numbers

or a variable that is defined above

that

filter index

and this is gonna take

two parameters index and the value so

I'm going to type here

actually you need to put curly braces

index

let's change this to value

actually let's keep it as s

Ctrl Z

and

here we're gonna Define

our expression by a by which our list is

going to be filtered so I'm going to

type here that first I want the

index

to not be equal to zero

and

I I also want

that

the value of the element so actually

let's change this to valid to make more

sense so let's type here to value

because we're talking about those ones

so I want the value of that string so

the value dot length I want I want it to

be less than a 5.

all right so this is the transformation

if you want this is not a transformation

this is how we want to filter our

numbers list that we defined above I

want to

filter both by the index so this is why

we use the filter index and also by the

value

and I want the index to not be equal to

zero this is the first condition for the

filtering and I also want so use the end

operator that the valued of the element

the length to be less than five now if I

print this if I added here a print line

but first let's Also let's also use the

uh filter not so I'm going to type here

Val

foreign

filtered nut

and I'm going to type equals numbers

dot filter not

curly braces

and we type here it dot

length

so the value of the element

is to be less than equal to 3.

now if we output those two in the

console so if I add here print line and

I'm going to Output filter

Index this should be filtered in the

solicit Factor this

so let's type here in the index

I type it already

so let's type here

edex actually

it will make more sense

but I have Elixir so why anyway so I'm

gonna output that first then I'm gonna

hear a print line

and I'm gonna type filtered not

and filter not as I said is gonna return

a list of the elements for which the

predicate yields false

now if you run those two look what

you're gonna have on the output

so this is the output for our first for

this print line and for this print line

so let's think about what we have here

first we have two and four

and we Define the filter here first the

index which should not be equal to zero

so the one is first excluded and we also

want to that to the value that length to

be less than five so the only ones which

uh for which the value is less than five

is the 2 and 4 because the length of the

three is bigger than 5 because it has a

length of 0 1 2 3

4 and

the length is 5 but I want the length to

be less than five not less than equal

than five so this is why this is not

included and you only have two and four

next

uh now use the filter nut and it's gonna

as I said it's gonna return a list of

elements for which the predicate for

each this expression is gonna yield

false so it's gonna go through all of

our elements and it's gonna return the

elements for which this is false so we

get three

N4 because those are the only ones which

are not less than equal to three they

are greater than equal to three so this

is why we have here three and four

because this returns if Returns the

elements if the uh the the length of the

element is less than equal to 3 so this

is why you get here 3 and 4.

and

there are also functions that narrow the

element type by filtering elements of a

given type so there is a function called

filter is instance which returns a

collection of filaments of a given type

being called so let's see how you can

use that but first let's create a

Val mixed

let's call it mixed

list

I'm gonna put equals another card type

list off

and I'm gonna Define some mixed elements

here so I'm going to type here let's say

one

two three then some charts like

a

B

C

now so on strings let's type here

hello

World another string

Alex and let's also type

a Boolean so let's type here let's say

false

now there is a function as I said which

which Narrows the element type by

filtering the elements of a given type

so we can use this function called

filter is instance so what you can do

here is we can we can type

mixed list

Dot

filter

is instance

so we choose I think this one not this

one

filter is instance

of Y so mixture is that

filter

is instance

and here with inside the angle brackets

we Define the generic type for which

this list is going to be filtered so

let's first choose the chart Char then

I'm going to put that for each so I'm

going to use the for each Loop

and I'm gonna add print line

and I'm going to type it here so I'm

going to type the parameter

which is the each chart so now if I run

this we will get only the charts being

displayed here because it is filtering

by using the instance of charge so you

get the charge if I change this to

to in now I'll get the integers from our

mixed list so if I run this

now get the integers one two three that

we have defined here

and if I change this to let's say a

string I will get the strings inside our

mixed list because filter is instance

it's gonna take the specific title that

you type here and then it's gonna Loop

through all of those types which are of

this type in this list so if I run this

I'm gonna see Hello World Analytics

so we get hello world and Alex also I

can filter for Boolean values if I type

here Boolean is gonna

it's gonna output in the closet or the

one Boolean value that we have here

which is false so if I run this

I get false so this is how you can use

the filter is instant function on types

to filter the mixed list and you can

also put that because this Returns the

list this entire thing a filter at least

you can put that for each to call the

the this the for this for each Lambda

function and then we output them in the

console

uh the next thing that you can look and

that we're gonna look at is called

Partition so I'm going to add a comment

here but I'm going to hide the console

first so I'm going to put here a comment

I'm going to type here partition part

T1 so partition

and this is an alter another filtering

function called Partition and it filters

a collection by a predicate and it keeps

the elements that don't match in a

separate list

so you have a pair of lists as a return

value the first list is containing the

element that match the predicate the

predicate and the second one is

containing everything else from the

original collection so I'm gonna take

again our numbers that have defined the

path so I'm going to type here

uh

file and to to get this partition we put

we put parentheses and here to type

match so this is going to be the values

which are going to be

matched by the predicate and

I'm gonna type here rest and you're

gonna be stored the rest of the value

which which don't match the expression

the predicate that's going to be defined

and I put here equals

numbers

dot partition

and we type here it

Dot

length

greater

than 3.

now if I

other print line here

I can print first the match

let's press Ctrl D and now I can print

the rest of the elements which don't

match this this condition this

expression that is defined here so if I

run this code

you will see first all the elements

which are greater than 3 and then we're

gonna see the rest of the elements which

are not greater than 3. but let's add

the space here to make things more

clearly so we're going to add a hero

print line

quotation marks for recession so let's

run this again

so we get three and four so those are

the elements which are coming from our

match list which is defined here uh

which is uh which satisfy this condition

which is greater than 3 so we have three

and four so the length is greater than

three

and then we have one and two so those

are the rest of the elements which are

not satisfying this condition that is

defined here so we have one and two so

this is how you can use the partition

function to

to get the

basically to get a pair in which the

return of value the first is the first

is the list containing the elements that

match the predicate and the same code is

the one containing everything else from

the original collection

so this is how you can use the the

partition function so

now I'm gonna end the video and see you

in the next video now it's time to look

at testing predicates and those are

functions that simply test the predicate

against the collection elements and we

have three functions any

which returns true if at least one of

the element matches the given predicate

then we have none which returns true if

none of the elements match the given

predicate and then we have all which

returns true if all of the elements

match the given predicate so let's test

all of them so I'm gonna type here print

line

numbers

that let's start with any so we put any

so here we type it dot ends

with

and let's put let's say

e

then out of another print line

numbers

dot none

it should be

curly braces

eat

Dot and sweet

let's say w

and print line

numbers

dot all

let's say it dot length

so if if the length of all the elements

is greater than one

so now if you run this

all of those gonna return either true or

false

so get true true and true this is

because first testing any and then this

returns true with at least one of the

elements much in the given predicate so

it is true we have

two elements which ends with the E we

have three and one so that's why you

return true there

then we check to see if uh none of the

elements ends with W so we're checking

to see if none of the elements uh in our

numbers ends with w n is true not only

for elements will end through double so

we have two in the output

then we check numbers.org so we check

into if all the elements the line the

length of all the elements the element

strings that we have here one two and

three are greater than one and this row

all of them are greater than one so this

one have true true intro

so this is how you can use the test

predicates uh on your collections

first time to look at the plus and minus

operators so in Catherine plus and minus

operators are defined for collections

they take a collection as the first

operand and the second operand can be

either an element or another collection

the return value is a new read-only

collection and let's look at an example

on this I'm going to type here Val

numbers

you want to put equals two so now I'm

going to type your Multiple List off

Multiple List at least with we to which

you can add and remove elements and I'm

gonna type here

one

two

three

and four now if you want to add an

element to this list you need to type

here numbers dot add

and here you need to specify let's say

five and now this new element is going

to be added to the list but with the

plus and minus operator you can do we

can do this in an another way so you can

type here

uh

but I'm gonna put this in another list

so you can type here Val

plus list

so we put equals to and type numbers

Plus

and to type five and now this element is

going to be added to our list that we

have here it's going to be added to the

the string five and it's going to be

stored in this plus list now let's look

at the minus operator here Val I'm going

to call this minus list

and put equals 2 and I'm going to type

numbers so now I'm going to use the

minus operator so I put minus

and we type minus

multiple list of

and we put here

let's say three and four so we type here

three

and four

I have an underline let's see what it

says

now anyway let's run our code to see

what to get in the output

but we need to add some print lines

together so

I'm gonna add here a print line

plus list

and another print line

minus list now if I run this again

we get one two three four and our

Five Element added to our numbers list

from our class list which is outputted

here

and then we get 1 and 2 because 3 and 4

were removed using the minus operator so

this is how you can use the minus and

plus operators with the collections

so this is our discussion about the plus

and minus operators and I'm going to

look at we're gonna

uh

move to the next section so I'm going to

close the console and I'm going to end

the video

now it's time to start a discussion

about grouping so the kotlin standard

Library provides extension functions for

grouping collection elements the basic

function Group by takes a Lambda

function and returns them up in this map

each key is the Lambda result so the

result of that expression inside the

Lambda function

and the corresponding values is the list

of filaments on which this result is

returned this function can be used for

example to group a list of strings by

the first letter

you can also call Group by with a second

Lambda argument a value transformation

function in the result map of group y

with two lambdas the key is produced by

key produced by key selector function

are mapped to the result of the value

transformation function instead of the

original elements

so let's see what all of this means by

looking at an example so if I type here

Val

numbers

and I'm going to put

list of

again one

two

three

four

and five

now we I can add here a print line

I can type numbers

and I can type here dot Group by

and I'm gonna group it by

it dot first so the first letter in in

one of our elements that

uppercase

and now we're going to look at the next

the next Group by with the key selector

and with the transformation function so

I'm going to type here print line

numbers Dot

group y

so we have this one key selector so you

type here key selector

equals

and here we put

curly braces and we type eat that first

so the first letter in our element so

first it should be here

we put a comma

and we type value so the how the value

of the the value of this list is going

to are going to be transformed and here

we also put curly braces and we put it

that uppercase so put it curly braces it

should be

it dot uppercase

let's press Ctrl alt L to format code

and now let's run our code to see what

we get in the output

so we get

for the first one number the numbers are

grouped by for uh and we have this

Lambda it that first.apper case it takes

the first letter in Upper cases so it

gets whoa whoa so then you get one

we get T we get to we get F we get four

we get uh then we use this one which on

which we we uh you we we group uh

elements with a second Lambda argument

with the value transformation function

and the result uh map of group y with

two lambdas are the keys produced by the

key selector and the function are the

function are mapped to the result of the

value transformation function so the

results are mapped to this

transformation that is defined here so

this this is what creates the keys this

is what creates the key this uh Lambda

function and this is what creates the

the the the the values so we get all

because you get in that first so the

first letter and get one because this is

the value so the value is uppercased we

get one so we get one here then we get

again eat that first for the for all men

so you get T then you get two and I get

uppercased and get F lowercase f and get

four and uh and five

so this is our discussion about grouping

and uh my suggestion for you is to play

with the code try different values for

the key selector for the value transform

for uh try to group The Elements by

different expressions and just play with

the code until you get uh get uh until

you get comfortable with the code now

it's time to start a discussion about

the driven collection parts and the

quoting standard Library contest

accession functions for retrieving parts

of a collection

these functions provide a variety of

ways to select elements for the result

collection listing their position

explicitly specifying the result size

and others I'm going to start with the

first function called slice

which Returns the list of collection

elements with given integers the indices

may be based either as a range or as a

collection of integer values so I'm

gonna create first domain function

I'm going to type Val numbers I'm going

to create a list of

and I'm going to type here one

to

three

four

five

and six

now to use the slice function we simply

add here a print line

and we'll type your numbers

dot slice

and we create here arrange so we type 1

dot dot

3

and next we're going to add another

print line and I'm going to type numbers

dot slice

and I'm going to put here now zero dot

dot

four

and

I'm gonna type step

two

foreign

list so slice here

slice

and I'm gonna add another print line

here

numbers

dot slice

and I'm gonna add now a set here so I'm

going to type set off

and I'm gonna type

three

five

and zero so let's press Ctrl R till the

format the code

now if you run this code look at the

output

so you first get the first three

elements we get to

two three and four because uh we're

starting from the index one so we get

two three and four

then we get from 0 to 4 so you get one

but you don't get the two because you

have this step two which steps the

second element and then we have three

and five because it's stepping for the

four so this is what step two is doing

here

and next we have numbers slice and get

four

six and uh one but we get them as the

string representation we don't get them

as

as a integers so

all right

now

let's look at the next functions and

those are the take and drop

and to get the specified numbers of

elements starting from the first you can

use the take function for getting the

last elements use the take last when

call when when called with a number

larger than the collection size both

functions return the whole collection to

take all the elements except a given

number of first or last elements called

the drop or the and the drop loss

function respectively

so I'm gonna

add some print lines here I'm gonna add

print line

and I'm going to type numbers

dot take

three

Another Printer line

numbers

dot take last

hmm

also three then another print line

numbers

Dot

drop

one

and another print line

numbers

dot drop last

five now if you run this code

look what you get in the output

but let's add a space here so on another

print line

quotation marks photosyn now let's run

this code again

so we get uh

for you get numbers take three and this

function basically takes only three

um

three elements from our list

then numbers take last three is gonna

take the last three elements from hourly

so I have four five and six

then we have a print print line numbers

drop one so you have two three four five

and six and the first element is dropped

so this this is what this function is

doing

next we have numbers dot drop plus five

and this function is basically dropping

all the uh file the last five elements

and you have only the one so this is

what those functions are doing

you can also use predicates to define

the number of elements for taking or

dropping and there are four functions

similar to the ones described above

and those are take while take last while

drop while and drop drop last while and

let's look at all of them but I'm gonna

put a space between uh

our code that we have here so I'm gonna

have the print line here

quotation marks photo session

print line

so I'm going to type here numbers

dot take

while

and here we Define the predicate I'm

going to type

not

eat dot starts

with

f

in quotation marks

now another print line

numbers

dot take

last

while

and we Define here the predicate it

not equal to 3. not equal

two three

another print line

we type numbers

dot drop while

and we Define the expression the

predicate which is going to be applied

for our drop last while

so it should be

drop while here

and type it

dot length

equals equals to 3.

and what another print line

numbers

Dot

drop last while

and we type here it contains

it dot

contains

quotation marks I

no

if you run this code

we first get one

two and three so because here we check

in we're uh because using the logical

not operated we want all the elements

which don't start with f so we don't

have four and a five we have only one

two and three

next we have

numbers that take last while and we

Define the expression for which is going

to be

returned the elements by it not equal to

three so you only have four

five and six and uh the the the three

the three is is missing and we don't

have one because take last value only

takes the uh last elements so this is

why we have only

and six when you don't have three so it

doesn't care about the first element or

uh about uh the second element

next we have numbers drop while eat that

length equals to 3 and here we have

we have

so it at the lines equals to three and

we have

three four five and six so

this condition is satisfied so it

basically it drops all the elements

which are equal to three so we have

three four five and six and all of those

are bigger than three and the ones which

are equal to three respectively one

uh uh two and uh let's see and six

actually six is appearing here

so

one

two

I don't know why six supreme but this

basically saying that it should drop

while if that length equals uh to three

so it basically drops all the elements

which are equal to with the length three

so we have this is what we have here um

three four

five and six six should not be but I

don't know why it's here now

the next that we have here is numbers

dropped last while so again this is uh

Focus specifically about the last

element so it contains I so get uh

uh one two three and four so because

this drop last while is basically

basically dropping all the elements all

the last Elements which contain uh which

contains I so we have

six five

so we have a

drop last while so we have it that

contains I

so it drops all the elements which

contains I so sorry for what I said

previously so this way I have here one

two three and four and uh the five and

the six are missing because it basically

drops the old Elements which contains

the letter i so this is how you can use

the take and drop the next function that

you can use is called chunked

and

if you want to break a collection

through parts of a given size then you

can use the chunked function chunks take

a single argument the size of the chunk

and Returns the list of

[Music]

list of the given size the first chunk

starts from the first element and

contains the size of the elements and

the second chunk holds the next size

elements and so on the last chunk maybe

may have a similar size so if I have

here let's change this to numbers

strings and let's type here

Val

numbers

while I was changing that I should use

refactor so

let's use a refactor

rename

numbers strings so it will be changed in

all the places we're using that

and here have numbers and I'm gonna put

equals and I'll put parentheses

zero dot dot 13. and I'm gonna put that

to list to convert this to a list

and we add a print line to see how this

chunk function is working so we got the

print right let's close the console

and we type here numbers Dot

Dot chunked

and would put three so it's gonna chunk

our elements into three into

um

basically elements of three so we have

zero one two three four five six seven

eight and they're both on on chunks of

three elements so this is what this

chunks is doing

so this is why I have here this result

and

the next function that you're going to

look at is called windowed and

and we we can also use a transformation

on our chunked element so I cannot hear

a print line

we type numbers

Dot chunked

chunked which we also choose the size

three

then you put curly braces and here I'm

gonna Define how our chunked elements

are going to be transformed so we put it

dot

sum so it's going to sum the chunked

elements so if I press Ctrl I'll tell

the format the code and now if I run

this look what we're going to see in the

output

so we get uh you get first the chunked

elements here

in entry in three elements and then we

have our chunked elements but they are

summed up so we have first three because

zero plus one plus two is three then we

have three four five because that is

twelve don't have seven eight uh seven

uh

six seven eight and they have twenty one

then we have

uh 10 11 and 9 and we have 30 and so on

so this is uh how uh you can use the

chunked function to chunk the elements

together by a given size and you can

also use the transformation function to

do a transformation on them like adding

them so as you can see we have adding

them here

next we're going to look at the next

function function that you can use and

that is called the windowed function and

we can retrieve all possible ranges of

the collection elements of a given size

with the window function the function of

getting them is called windowed it

returns a list of element ranges that

you would see if you are looking at the

collation through a slightly wind off of

the given size unlike chunked window

returns element ranges Windows starting

from each collection element all the all

the windows are returned as an element

of a single list so if I have here

the

let's create another

variable here let's call it

Val numbers

string

two

and we're gonna type here let's say

or I can assign

I can assign the number of strings here

so I'm going to type here number strings

now what I can do here I I can add a

print line

and like I can type numbers string store

Dot

windowed and we choose let's say three

and let's see what we have on the output

so now if you run this code

I should have added here a space so

let's add the print line here

backslash

photo selection

foreign

so we have one two three then we have

two three four you have three four five

and four five six so this is similar to

junk but window provides more

flexibility because uh you can

specify a step which defines a distance

between the first element and and to a

DH and windows by default the value is

one so the result contains Windows

starting from all elements if you

increase the step to two you'll receive

only Windows 30 from the other element

first third and so on and finally you

can apply transformation to the return

ranges right away to do this you provide

the transformation as a Lambda function

when calling windows so it is similar to

chunked

and um

so with this I think we end our

discussion about the thieving collection

parts and see you in the next video and

if you find this confusing don't worry

because it's so confusing for uh for me

for me and for probably other people

because it takes time to to get used to

this and my suggestion for you just play

with the code change the the parameters

play with the functions try different uh

values and so on so see you in the next

video

now we're going to look at some

functions with which you can retrieve

single elements from our Collections and

the first one is one which retrieves an

element by its position so I'm gonna

create the Val numbers again

numbers equals to

list of

and

we type here

one

two

three

and four and also five

and let's say that I want to get the

element at position three to do that

just add here a print line and we type

numbers

dot element

and here you specify the position three

so zero one two three so we're going to

get four outputted in the console

so we get in the output as expected for

now there are there are also two

functions for retrieving the first and

last element and to do that just add

another print line here

and type numbers that first

so this is gonna retrieve the first

element and Ctrl d

numbers Dot

last so

this is gonna receive the first element

one and last is gonna retrieve the last

element five so if you run this

we get first four from our

disappearance line then we get first one

and again the last five

and we can also edit three

elements by condition so I can type here

uh I can type down here or

or I will type it here

actually I'm going to type it down here

it's going to add here a print line

numbers let's say

DOT first now I'm going to retrieve by a

condition so once I first I'm going to

choose this one with curly braces and

the condition is that I want to leave

the first element for which the length

is greater than 3 so I'm going to type

here it dot length

greater than 3. and I'm gonna add I'm

gonna press Ctrl D because I'm gonna do

the same thing for the

by condition so I'm going to type here

numbers that last but I'm gonna change

the condition I'm gonna change it to

eat that starts with the it that starts

with

the letter f

so now we're retrieving the first

element which is greater than three and

next retrieving the last element which

starts with the letter f

so now if you run this

we get 3 and 5 because 3 is the first

element which is has a length greater

than three and can get five because this

is the one the last element which starts

with the letter F and if you can other

space here if you want to make things

more clear so we're gonna add the print

line quotation marks

backslashing

actually

forward selection

so if you run this

we get three and five output today in

the console

and we can also retrieve a random

element from our numbers so I can type

here print line

numbers

dot random and this is going to return a

random number from our list of numbers

so if I run this

we get one and if I run this again

get four so every time you run this

you'll get a random number

and you can also check if the list is

empty so you can type here print line

numbers

that is empty then that is going to

return true if the list is empty and if

the list is not empty it's going to

return false so if you run this

we get false so this is our discussion

about how to retrieve single elements

see in the next video now it's time to

start a discussion about aggregate

operations so kotlin collections

contains functions for commonly used

aggregate operations

and those are operations that return a

single value based on the collection

content most of them are well known and

work in the same way as they do in other

programming languages

and we have minor null and Max ornal

which return the smallest and the

largest element respectively and on

empty collections the return null then

we have average which Returns the

average value of elements in the

collection then we have sum which

Returns the sum of elements in the

collection of numbers

and have count which Returns the number

of elements in our collections so please

declare a list numbers

list of

let's put so let's put some numbers here

like let's say six

then

14

4

let's say

100

now let's press Ctrl format the code and

let's add the print line here let's put

quotation marks and let's type the sum

is

dollar sign

and the type here numbers

dot sum

numbers

that's sum

all right and uh

I'll add another print line and now this

is gonna say

the

count is

again dollar sign

numbers

dot count

another print line

the average

is

again dollar sign

numbers that average

another print line

and now I'm going to say the

max value is

again dollar sign numbers

dot marks

numbers Dot

marks

or null

and I'll print line

the mean value is

dollar sign numbers that mean

or null

now if you run this code you're gonna

see in the output the corresponding

values for those expressions that we

have here so again the sum is 134 the

count is 5 because we have five elements

here the average is 26.28 the maximum

value is 100 and the minimum value is 4.

so this is how you can use those

aggregate operations and agree and those

aggregate um

functions and beside the regular sum

there is an advantage so much summation

function called sum of that takes a

selected function and Returns the sum

sum of its application to all collection

elements and let me show you how this

works so you just type here

so we type here print line

the sum is

dollar sign

numbers

dot sum of

we select this one

and here we type it

times two

now if you run this what it's going to

do is it's gonna sum all the numbers

like the first function that we have

here so it's going to sum 134 and it's

multi is going to multiply that that

number by two so if you run this

we get 260 which is 132 times

134 times 2. so this is how you can use

the sum off with this uh

transformation which is gonna basically

multiply the list with a number with the

with the sum of the list with the number

so this is our discussion about

aggregate operations and see you in the

next video now it's time to start a

discussion about ordering and in this

video we're going to look at comparable

and add comparator interfaces

so first I'm going to create a list of

numbers

and

I'm not gonna

I'm gonna get a multiple list so

Multiple List off and I'm gonna

Define some numbers here not in a

specific order

so we type two let's say five

one

let's say

40.

20.

100 let's say

60. now what if I want to

order the numbers that I have inside our

list numbers to do that you just need

you type numbers

Dot

and there is a function called sorted so

if I type number that's sorted now our

number on our numbers are going to be

sorted so if you type here for each

put in the line

it

so now if you run this

even though we didn't type our numbers

in ascending order there will be sorted

and as you can see they are sorted we

have one two

five twenty forty six three and one

hundred so they are sorted by this

function called sorted and why this is

working

it's working because if you click on

this sorted function

what a

actually if you type here let's say

let's say that I put the type here

and

if I click on the type

this tie this type integer this class is

implementing this thing called

comparable and this comparable has a

function called compare to so when we

actually type numbers that are sorted

what what internally was happening is

that the integer class was using the

compared to function which is which is a

function which you want to you need to

implement if you implement the

comparable interface to compare if

to compare our numbers and to based on

that to to sub them to to sort them

and now let's say that I have or let's

say that I have a data class

called laptop

and it's gonna have a

variable called brand it's going to be

of type string

another variable called here this could

be the the year one one this specific

version was released on int

also need

and the wall

let's say um

price also an integer

right now if I create here a list of

laptops laptops

equals to Multiple List of

and I type here laptop

let's say that I type here Dell

and I put the year let's say 2021

the ram let's say that is four gigabytes

RAM and the price will say that is

600 okay

comma I'm going to create another laptop

here

so another object I'm gonna type here

let's call it

Acer

is going to be let's say the year um

let's put 20

20.

the para is let's say the ram let's say

that is eight gigabytes Ram

and the prices say that it's 100 dollars

comma and let's create another laptop

object so we type laptop

and create an Apple laptop which is

which you're gonna for which you're

gonna choose the

the year let's say

um

Tony

2020

2022

and

for the ram let's say they're choose 16

gigabytes RAM and 1 000 for the price

now if I type here laptops

that sorted let's say that I want to

sort the laptops as if you can see while

on time pink the IntelliJ is not giving

me any hints to have this function

sorted

as a true fill in that function because

that function is not working on our

laptops list and why why is happening

that

that is happening because the IntelliJ

doesn't know

how we want to sort this laptop list

that we have here because you want you

can sort it in many ways you can sort it

by the name you can sort it by the year

you can sort it by Dynamic and sort it

by the price so when you say laptops are

sorted the IntelliJ is not it's not

annoying

what do you mean because previously it

was working with our integers or and

without integer class because the

integer class if you type if you put the

type here

back integer

the integer class or the int class what

I was doing actually it was inheriting

from this comparable interface and it

was implementing the compared to

function which is used when you call the

sorted function and inside this

comparative function it has defined its

own Logic for comparing integers for

comparing whole numbers and what we need

to do in order to be able to

sort our list of laptops is we need to

implement the comparable interface and

you need to Define our own logic which

is going to determine

how our list is going to be awarded by

by which uh property by the Year by the

ram by the price so this is what you

need to do and to do that just put here

a colon and we'll implement the

interface comparable so we type it

comparable and we have a suggestion you

should choose this one

we put angle brackets and here we type

laptop because you want to implement

this we want to compare laptops so

laptops so this swipe water level there

then you put curly braces and now we

need to implement the function so click

on that it says Implement members you

have the compare to function now

you don't need to Define our logic which

is gonna determine what makes uh uh zero

the final logic which is going to

determine how our elements is going to

be watered by what criteria and I'm

gonna order the the laptops first by the

price and to do that to do that just

type if and to type this and we type in

this because this is going to be called

on an inst an instance of our laptop

left of class is going to call the

compared to function and it's going to

compare with another argument so if this

dot price we say here

is greater than other

that price

we we return as positive number so we

type return one and a positive Value

Plus simply shows that the receiver

object so the objects

this object is greater than the the uh

the object which was passed so this is

what this means this one else lcf is the

receiver object is not so if this dot

price is less than other that price

let's say then gonna return

-1 a negative number so I'm going to put

here minus one and what this basically

logic is I'm just going to swap them so

it's going to swap the elements based on

this logic else I'm going to return 0

and that is going to mean that they are

equal so if you type here return 0. and

if you click here it says that you can

write this in a different way so if you

click on this

and let's add the print line to see that

our if if uh are called so if I type

here print line

I'm gonna say

in if statement

dollar sign

actually let's type swapping

dollar sign

this dot brand name

with

dollar sign

other

dot brand

so let's add this print line also in our

lcf

and I'm not gonna add in inside the ears

because our we don't have Elements which

are equal but

you can add that if you want now if I

type laptops that's sorted now I have a

suggestion and if I type for each

print line it so print the element

now if you run this look you're gonna

see the elements sorted and uh ascending

order by their uh their price so you see

that we have first the brand the brand

name Dell and we have price 600 then we

have

the price uh you have laptop Acer and we

have eight hundred dollars and have left

to apoll the brand and you have the

price one thousand dollars so the

nearest so now they are sorted using our

own logic which was defined inside this

comparable interface and which was

defined inside the which was defined by

this comparable interface and which was

defined inside this function compared to

that we've overrided inside our client

and as you can see

if uh you look you see here that he says

if an if statement swapping Acer window

so the elements are swapped based on our

road in if statements it says swapping

apple with Acer so they are they are

swapped based on our logic

so this is how you can use the

comparable uh interface but what if I

want now to compare by the

let's say uh RAM what can I do I can't

implement the comparable interface again

here and I can't uh

I I can I can add the that property here

but that is gonna conflict with my logic

with for the price so what can we do and

here comes into action comparator

with comparator you can compare your uh

your list you can you can uh com you can

order actually I should say you can sort

your list using multiple properties so

you can order it by the uh brand name

you can order by the year we can already

buy the ram you can already buy the

price and with compare because with the

comparable interface

we can do that just only by one property

and that is pretty uh inflexible and

it's not what you want and how how to

use this comparator to use the

comparator we just type here

class

I'm gonna call it

comparator let's say Ram

and it's gonna extend

from comparator so type comparator and

you have the type and for the type here

we want to compare laptop so you type

laptop here

angle brackets curly braces and press

enter

and now we need to override

the

compare function inside our class so you

override that function and here again

let's change those to laptop one

so no now we have laptop one and laptop

two

and we Define

the same logic so we type here uh

return

if because you need to return an integer

if

this dot Ram

actually not this that ram laptop

laptop1.com

is greater than laptop 2.

Ram

and we need to delete this nullables

because you need to

have some errors there

and put curly braces return one else if

else if laptop

one dot Ram

is less than laptop 2.

laptop two dot Ram then gonna return I'm

gonna put curly braces I'm gonna type

return

minus one else

we're gonna return

zero now if I want to now to compare our

let's add the space here now I want to

sort our elements based on the RAM and

another print line here to add a space

so I'm gonna have the biggest photos in

also here one

so go quickly space it here

should be print

and now what we can do is we type

laptops Dot

and we type here

sorted with and which is we choose this

one which has the parameter Define a

comparator and we're going to pass our

comparator that we defined here with the

comparative function so I'm gonna pause

I'm going to type sorted with and we

type comparator

foreign

our class

and

create an instance and within the type

dot for each now this one

for each print line

it

so now if you run this you're gonna see

them sorted by their border Ram

so if you run this

as you can see uh if you look at the

if you look at the first there are they

are First Source did by their price we

have 68 800 and 1000 dollars and then we

have them sorted by the ram so we have

four for the four which is the dell and

then we have the Acer which is uh eight

gigabytes around and you have the Apple

which is 16 and uh if you look uh

actually they are in this order in the

list so let's change this to to let's

say to 16 and these two to wait

to

to make it more clear so let's run this

again

so as you can see they are still started

so we have first the Dell which has four

gigabytes of RAM then we have the Apple

which is eight then we have the Acer

which is 16. so this is why so now it's

sorting our list this sorted width using

comparator Ram that we defined we

created and then we Loop through all our

all elements because that Returns the

list so this is how you can use

comparator and if you want to uh to to

sort of let's say now the list by the

year you just create another comparator

so you type class

comparator let's say here

and it's also going to extend comparator

comparator

is going to have for the type laptops

because it's gonna Source laptops curly

braces we implement the function compare

and to define the logic for comparing

elements using uh using the ear so let's

delete this nullables

let's type here laptop one

and here laptop too

and I'm gonna copy this chord

I'm gonna paste it here

but I'm going to change this from Ram to

here

so here

here

also here

again

and we have an error here

missing so let's add the curly brace

here

all right so now if I want to to have

the element sorted by the year we just

type here

let's put a space Also print line

quotation marks for us this run and

found the laptops to be sorted by the by

the by the year I just like laptops that

sorted with

and this expects a comparator so I'm

gonna call our comparator that we

created comparator here comparator

here

an instance of that then for each

now this one again for each

print line

it

so if you run this

now we're gonna see the last element

sorted by their ear I have as you can

see

you have uh we have them sorted by the

ram so this is from this one

and now it's using comparator here so

you're sorting the embodies we have 20

20 21 and 2022 so this is just not like

the order that they are defined in uh in

the list so this is how you can use

comparable and comparable but if you

think about there is a lot of code here

to just have to have our

our list so that you have to we found

to

you have to you can use the compare the

comparable and with the comparable you

can use it with just one property and if

you want to use it with another property

you have to create those comparators

which gives you some flexibility but

still you have a lot of code and we can

remove all of this code

and use Lambda

functions

so I can delete all of this

all of this comparable that we have here

and instead of using those comparators

and all of this stuff what I can do is I

can delete all of this

I can little or this also

now what I can do is I can add here

laptops dot sorted

with and here

so we choose this one sorted with which

has this parameter comparator press

enter and here we type

compare

by

and here we type and this line of code

that we have here we it what it actually

is doing is creating a comparator but is

creating a comparator using a Lambda

expression so here you can type it dot

it's dot let's say uh price

right and then for each print line

it so this is equivalent to creating

that comparator here and inheriting from

the comparator interface and defining

the logic for uh uh or ordering for

sorting them so now if you if you run

this this will have the same effect like

the comparator price that we have

implemented previous as you can see they

are ordered now by the price and I can

do

now on another line of code Source dot

laptop dot sorted weight

and we type here compare by we type it

dot let's say uh

Ram

dot for each

print line

and it

now if you run this I'm gonna have the

same output let's add the space between

them print line

so let's add the space to see them to

see the code more clearly

and now as the as you can see the first

they are sorted by their price and then

they are sorted by their uh their RAM

memory so this code is equivalent to

creating what we had previously with the

comparator and implementing the dotting

interface and the function and actually

there is a shorter way even to do this

we can delete this let's say so the

sorted with and compare bear compare by

and we can just type here sort by

so this this will create internally the

same thing that we had previously so if

I type it dot ROM

and I can do the same thing here I can

delete all of this and I just

could type here selector the sort by

eat that price now this will have the

same effect like all the code that we

had previously but now it's extremely

concise and easier to read you just say

laptop sorted by sorted them by their

price so the elliptos by the ram so if

you run this code

you'll see the exact output in the

console because what this function is

doing uh actually we should Loop through

them so let's add the print line here

print line it

so let's put here for each

put into line

it

so now if you run this you will see the

exact same output we just this simple

line of code

so as you can see you have the same

output we have them sorted divide their

price first and then they are sorted by

their RAM memory and if you hold Ctrl on

this sorted by function

as you can see this actually internally

calls the sorted width which receives

the comparator and is calling the

compound the compare buy so this is like

a syntactical sugar for us which makes

the code more concise but internally

it's using using that sorted weight and

uh if you want to use the salt to do it

one advance for using a sorted with

let's say that I type here another print

line to add a space

let's put photos lesson

should be in quotation marks

forward slash n one

good thing about the sorted width so

let's type laptops

Dot

sorted weight

and here we type

compare by

let's say it dot year one thing that you

can do with the sorted with if you call

specific restorative function you can

put that and then buy so it will sort by

year and then by I know let's say uh it

dot price so this is what you would get

what you can do with the sorted with you

can sorted by year first and then by uh

by the price so if you run this

as you can see they are sorted first by

the year so

and then buy their uh their price so

this way we get this in the output

now I'm gonna end this video and uh you

should you should use this uh this one

always because it's

concise monitable yeah very easy to to

to read and to and to type is

in contrast with what we had previously

with the or creating that comparators

and implementing the comparator

interface or the comparable

this is a lot a lot more concise and

easier to read so this is our discussion

about the comparable and comparator

interface so this line of code I repeat

this line of code that we have here

this sorted by it does RAM is creating

internally that comparator that we

talked about

the node we're just using this Lambda

function

so I'm going to end the video now and

see you in the next video so there is

one mistake that I did previously so if

I press Ctrl Z to have this code uh when

I run this code you you didn't sound

output then the list sorted by the year

and then by the price and that is

because I forgot to add the four each

to Loop through our elements so if I add

the four each and now if I run this now

we're gonna see them

sorted first by the year and then buy

the price because previously it wasn't

so this is why it wasn't now if you look

they are sorted by the first by the year

so we have 2020 2021 and 22 and then buy

uh by their price so this is why

previously you saw this one this output

and also I saw this output that but uh

some way I uh I didn't uh didn't saw

that I forgot to add the foliage here so

this is how you can also sort the sorted

width you can um sort it by more

elements you can put here I think

another

then buy

not here

we can put another so

dot then by you can put another then by

here if you want so eat that so let's

say your RAM

so you can add more more than buy if you

want so

now I'm gonna end with the video see you

and see you in the next video now it's

time to start a discussion about binding

the search but before you look at binary

search you're going to first look at

linear search to see why a binary search

works better so I'm gonna type a

function here I'm going to type private

fun it's going to be called search

element

and you're going to type search elements

so this parameter is going to be the

element that we are searching for

and it's gonna then another parameter is

going to be a list

of numbers so it's going to be a

multiple list

of ins

and it's going to return a number the

search element or minus one if you don't

find the search element

and here we're going to Loop through our

release so I'm going to type for number

in our list of numbers

if

at a particular iteration or number

it's equal to the search

element that means that we found the

element that we found the number so

we're going to return number

else if you come down here and after we

Loop and we did our if checks we didn't

return the number that means we didn't

find a number we're going to return

minus on here

now I'm gonna add the print line here

and I'm gonna type search

element because this is going to return

an element and then you're gonna output

with the print line dot in the console

and I'm going to type here 27

and for the next parameter I'm going to

define a list of numbers one time

Multiple List off

and here I'm going to define a number

from 1 to 30 so I'm going to type here

the numbers one

and our last element 30.

now if you run this code you're gonna

see in the output 27

so we get as expected 27 but let's

declare a variable here I

to number the number of iteration it

took it took to find the elements I'm

going to assign 0 to it

I'm going to increment it every time you

Loop so I'm going to type Val I'm going

to type I sorry plus plus

and I'm gonna print line here

and I'm gonna it's gonna say search

number

colon

dollar sign I

so now if you run this we're gonna see

how many iteration it took to found our

element

and if you look at the console as you

can see it took 27

27 iteration before we found our element

because the way the linear search is

working is it's going through all of the

elements is checking out each iteration

if the number of that particular

iteration is equal to the search element

if not it's going to loop again and

again again until it reaches 27 then it

found that element and it Returns the

element and you get that element

outputted here in the console

so this is what we call a linear search

and this is not particularly good

because imagine if you have 1 billion

elements or 10 billion elements and you

need to find I don't know

maybe the 7th billion element in the in

that array that is going to be very hard

and it's going to take a

time and memory and because of that we

have another

data structure for a searching element

and that is called binary search and I'm

going to type the code for binary search

here and also you're going to see the

number of iteration it tooks to

find element and also I'm going to

explain how binary search works so I'm

going to delete this

I'm going to type here VAR low I'm going

to put equals to zero

VAR

High equals to

numbers that size minus one I'm going to

explain the code immediately

here I'm going to type while

low is less than equal

to high

here I'm gonna type Val mid so now we're

going to get the middle element so I'm

going to put parenthesis slow

plus High divided by 2 and that's going

to give us the middle element 15.

then

I'm going to type Val comparison so I'm

going to type CMP

equals to numbers

dot get the middle element compared to

we're going to compare the middle

element with the search element

then I'm going to type here if

now I'm going to type if

comparison is less than zero

then I'm going to type here low

equals to

mid

plus one

lcf

if comparison is not less than zero

so it's greater than zero

I'm going to type here that

low

is equal to

High

sorry

is equal to my mid

-1

else

when I type here return

numbers

dot get meet and that is going to give

us the middle element

now if I declare here a variable

VAR

I and if I assign 0 to 8

and if I increment that I at each

iteration

and if I add a print line here

search

number

dollar sign I

now if you run this you're gonna see uh

you're gonna actually I should return

minus on here

because uh if at the end of our looping

and after we checked our all of our list

and we didn't find the number then we

determine this one meaning that we

didn't find the element

so now if you run this again

we get

search number one two three and then we

get 27 and as you can see this is

significantly different from our linear

search because now we only took three

searched and then I found the he found

our element previously we had to search

through all of our elements

through our elements until uh the number

27 to to get the number seven but now we

only get the

three iteration and we get the number 27

and how is that possible this is because

now we're using what is called the

binary search and how this binary search

works I'm going to explain it now so now

let's see how the code works

so first we have a variable declare code

load which assign a value of 0. then we

have another variable called height

which assign number the PSI minus one so

we assign the size of the array to our

high

then we have a variable called I to

which you assign 0. then we have a while

loop and we start looping by this

condition while low is less than equal

to high

then we increment our I to keep track of

how many iteration it takes before we

find the element and without any console

search element and the number at that

iteration

then we have a variable called mid and

this is going to give us the middle

element and to get the middle element we

add the lower lowest element plus the

highest element so yeah we'll add here 0

plus 30 divided by 2 which is 15. so

this is the

middle element

then we declare another variable called

comparison and we type here numbers that

get the middle element which is 15

compared to search element and if the

middle element is less than the search

element that means that the search

element is on the right of the numbers

which come after the 15 and because of

that this is gonna return because our

middle element is less than the search

element is going to return a negative

number so this is going to be true

comparison is going to be less than zero

and then I'm gonna assign low equals to

Mid Plus on so now

the low variable has the value 16 all

right now it will loop again it will uh

it will uh do it will have the I

incrementally it will

do the while loop it will increment the

I again and it will output into I in the

console now here now it will calculate

again the middle element and it will it

will add now 16.

Plus

30 divided by 2 and that is going to

give give us 23.

all right

now that is the middle element now and

what we do now here is we again uh I

have our variable comparison and we get

numbers that get 23 so we get the 23

element and we compile with our search

element which is 27.

and again because our middle element

is less than the search element this is

going to return on any negative number

so this shift check is going to be true

again now what is gonna

happen is that now low

is going to be equal to Mid

plus 1 and the low previously was equal

to 23 so now I'm going to have 23 plus 1

which is 24 all right

now this will loop again

or loop again so

as you can see this basically breaks the

the list in half at each iteration and

now this will loop again it will

increment the eye to Output I and now it

will calculate again the middle element

now the middle element is 24.

and the high element is 30 and we have

24 plus 30

divided by 2. and that is 27 the exact

number that we are searching for so this

is how binary search works it will

compare the middle element with the

search element if the middle element is

less than the search element that means

that your element is on the right side

of the element which come after the

middle element so all the elements from

the left are going to be excluded and

it's going to search only on the on the

right side

then it will split the list again in

half if the middle element again is less

than the search element it will go to

the right again it will speed the least

in half I need to sooner or later it

will find the element to to to the right

or to the left

so this is how one research version is

very powerful because for a very big uh

list you can use binary search and it's

more effective that uh only the linear

search

and uh this is the code that I type but

it doesn't you don't need to use this

code because uh I only type this code to

show you how the binary search works but

usually you'll use the binary search

function provided by The Collection so

you will just type here return

numbers

we're gonna put square brackets and I'm

gonna type binary I'm going to type here

numbers again binary dot binary search

because the binary search function

provided by The Collection only return

the returns the index so if I

search for the 27 element

that is going to return the index 26th

and the index 26 then it's gonna be the

number 27 from our numbers list so now

if you run this you'll see 27 output in

the console

so get 27 and we did this one with one

line of code you can even remove this

and just have a

single expression function so we can put

here

equals

so just by that it will do the same

thing that we did previously with all

the code that I typed

so this is our discussion about binary

search and see you in the next video but

before I end the video I should say that

binary search only works with the order

collection of elements so your elements

need to be sorted in order to for the

binary search to work and in our case we

look we used integer numbers and it was

easy because we typed elements in a in a

sorted order

but uh if the elements are not sorted by

by research is not going to work also if

you have your own class so you have your

own objects and you search with using

one research uh through a list for a

specific element in that case your class

has to implement the comparable

interface or it has to to to to use the

comparator interface and I'm going to

show you how to do that in the next

video so see you in the next video

so now it's time to start a discussion

about genetics so what are genetics

genetics allows us to give to the

compiler some hints about what kind of

objects you are dealing with and this

helps the compiler to figure out some

errors at compile time and it also helps

us to write more consistent and better

code and we already use genetics when we

look at collections so every time you

use an array and you you specified in

the angle brackets an INT or a double or

a class that you created an object what

you actually did there is that you use

generic you set the compiler that that

is the kind of data that specific

collection is gonna store so if you have

something like this while let's say

numbers

and

we put equals is equals here and type

array of

here you can put angle brackets

and you can specify a type here let's

say in because

I'm going to put numbers here and now

this helps the compiler to figure out if

let's say we want to add here let's say

a string so if I type here numbers

equals let's say your

some text let's put in quotation marks

some text as you can see you have those

underlines red which says if you hover

over here

first it says that Val cannot be resent

so let's change this to VAR to get the

error that I want so let's put VAR here

so we get here type mismatch required

and found string so here we are using a

generic type and the generic type is an

integer and because we said explicitly

here that this array is only going to

store integers we cannot go down here

and assign some string but

as I said at the beginning

genetics are only a compile timing

feature they don't make it through the

runtime and again we're going to come

back to this later because this has some

implications

now let's uh delete this and let's

create our own generic class to see how

it works because here we're passing in

but if you click on this array of

you see that you don't have here any an

intest the type that we SP we

specifically decided to pass to this

array but if you look here inside this

library.kt and if you go to array here

you see you see that we have VAR Arc

elements and we have this t

and T is a genetic type and the specific

type that is going to be passed here is

the type that we pass when we create an

arrays when we want to store some

numbers or some strings and now now

let's see how we can create a similar

thing that we have here with our with

our own code so I'm gonna delete this so

again generics are there to

to to help the compiler to figure out

errors and it also they are there for

helping us to write better and

consistent calls so that you you don't

add let's say uh strings to to an array

which is meant to store only numbers so

first I'm going to create a class and

it's going to be an Open Class

and it's going to be called player

and it's going to have a property called

name so it's going to be a Val name

we're going to Define this in the

primary Constructor it's going to be a

string

now I'm gonna create

open class here

and down here I'm going to create two

classes first is going to be called

football player so football

player

and this is gonna have a name and it's

gonna be a property because you're gonna

gonna extend from our player class we're

gonna derive from our operator class I'm

gonna pass this property to the dot

parameter name to our property name from

the superclass so if we type here player

because we were in inheriting from it

and here first name

so you pass your name

next I'm going to create another class

called baseball player so we type here

baseball

player

and this is also going to have a name

also only a parameter because the

property is going to be ultimately

defined by the

superclass by the class from which we

are inheriting from so here we're gonna

inherit put the colon to inherit from

our player player class and here we pass

our name parameter that we defined here

we pass it here to the primary

Constructor of the base class from which

we are inheriting from

all right so now I'm gonna go up here

and now I'm gonna create a class called

team

and this class is going to represent a

team and this thing can be a football

team or a baseball team and here we're

going to create the class called team so

we type the keyword class I'm gonna call

it team

and now here we put angle brackets and

inside the angle brackets we're gonna

Define the generic type t

and you can use a different letter here

if you want but usually and generally

use the T because it stands for the for

type parameter

next to put parenthesis to define the

primary Constructor and for the primary

Constructor I'm going to define

a Val name and this is going to

represent the name of the theme and it's

going to be a string and next I'm going

to Define also valuable this is going to

be called players and it's going to be a

multiple list

of also of type T so we type here

Multiple List

and we put here t

right

now I'm going to put curly braces

because I'm going to define a function

inside our class team so here we type

fun and I'm going to call this function

add

players so this function is going to add

players to the team

we put parentheses and here we Define a

parameter called called player and it's

going to be of type T so it's going to

be

should put

a colon here

and we put curly braces

and this is going to receive a parameter

of type T and this type T that we Define

here and here and here is going to be

the type the specific type that we gonna

pass when we create an instance with our

class team so

first here we're gonna check if

the

players

so our list of players contains this

player that is passed our ad player

function because if or you already have

that player in our list We're Not Gonna

add it to our list so if player and to

check if that player is in the list we

call the contains function on our uh on

on our uh

Multiple List so here we we type players

that contains player the argument

and if that is true I'm gonna put curly

braces

and here we're gonna output something

because to the console so we type here

print line

quotation marks and we type player

colon and then we put dollar sign curly

braces

and here we need to do something which

is called casting and casting casting is

used when you want a variable to be

treated as a certain type of variable so

when you want a variable to be treated

by the component by the compiler as a

certain type of object or or a class so

we type here player

because let's let's let me show you why

we need to do that because if I type

here player now I want to get the name

and if I put here dot name as you can

see we don't have any name here because

because this type because this T and

this T that we have here is generic the

compiler has no idea if this T that is

going to be passed here so this piece so

if you pass here

a baseball player or football player it

has no idea if at this moment now if

that is gonna have uh is gonna have a

name property on it this is why when you

type here that's name we don't get

nothing and to solve that we can say to

the compiler for them to the compiler to

treat this parameter here so this is

going to be a value pass to this

parameter as a player and in that sense

in that in that way you can get access

to the name property so we type here

player and

to cast to cast a variable to a certain

type we put as so we put

the keyword as and the type player

and we put this in parentheses so we

surround our expression in parentheses

inside the curly braces

and now here at the end of of the of the

end of the enclosing parentheses you put

that and now we have access to name

because we settle the compiler hey treat

this player variable I don't know

because I don't know if this is going to

be it's going to be a player

a player object passed here or a

baseball player or or a football player

treat this player variable that is

passed here as because here we have

teeth so we don't know what it is three

display to display a variable as a

player so you can use the name of a

variable and we're gonna say is already

in the TM and we can put here

the team and we can type here dollar

sign this Dot name so the name of the TM

else if if our player is not in the team

we put else

we're going to press enter to add the

right curly brace we're gonna add this

player to the list because if the if

fails that means that the player is not

present in our list so we type here

players

our

Multiple List dot add this player that

is passed player and we're also going to

output something to go to the console so

you add here print line

and uh

I'm gonna copy this because I'm just

gonna change the text a little bit

so let's delete this

player and so here is we're gonna say

instead of it's already

I'm gonna say it was

added

in the team

now let's create some objects with those

classes that we have here so first I'm

going to create some some players so I'm

going to create first the football

player so type here

Val for the ball player equals football

player

and

for the name I'm going to use a generic

name I'm gonna type just football

player

one

and I'm gonna press Ctrl D to create

another football player so football

player two is going to be called

and I'm gonna create two baseball

players so we type here Val

baseball player equals

baseball player

and we type here

baseball

player one and I'm gonna call Press Ctrl

D here also I'm gonna

call this baseball player 2 and I'm

gonna change this to 2.

now I'm going to create an instance of

our team class so we cut we type here

Vault team and this is going to be a

team of football players so we type Val

team

equals

and hero type team

and we put now because as you can see

it's expecting a type here and the type

is going to be

football player so it's gonna be a team

with what it's gonna be a team with the

football players and you're gonna Define

a name

also generic name football

team is going to be called

and they also need to pass an array

there

and for this array I'm gonna type just

uh

mod multiple array of

we can't

foreign

list of

and here I'm gonna pass our

football player I'm gonna pass only our

first football player here

now I'm going to create another team Val

team uh let's actually call this first

team football team so let's type here

food bowl

football team

now if you run this code

we don't get it on any output because

our ad player's function wasn't called

but if I

type here a football team

foreign

actually here football team

that

add players and I'm gonna add football

player too

now if I run this code

we get football player two was actually

here should be added

football player 2 was added in the team

football team so our code works well and

we use generic types for our classes

here we specified only the T and this T

that we have here was replaced with

football player that we Define here and

um

uh we're gonna do the same thing we're

gonna create another theme for the

baseball player and we're also going to

look at

how we can restrict the types that are

going to be passed to our generic type

here

so now I'm going to create a baseball

team so I'm going to type here Val

baseball team

put equals then you put Here theme

and here we type for the generic type T

baseball player because this is gonna

store baseball players and we Define a

name here the name is going to be

baseball team or generic name

and uh also you need to type to pass a

multiple list so type Multiple List

and we pass here a baseball player

but as you can see

here for the type

the type here is great oh that is

because

here we pass the football player

uh object that we create then here we

pass the baseball player

because kotlin has Type inference it can

infer the type as being a team of

football players so we don't need to put

here for the player we can delete this

you can delete all of this

and now our code still works and you can

see we have this type here which says

team football player so it's a team of

football players and it's uh inferred

it's inferred automatically by quoting

for us and you can do the same thing if

here for a baseball player so I'm gonna

delete this

as you can see now we also have the hint

team baseball player now let's add to

the baseball player

let's call the baseball player function

let's put here a

so we type here base bullet

team let's call the function dot add

players and hero type baseball player 2

because first one was added there

so now if you run the squad

not debug so run this code

let's increase this

we get let's actually decrease this so

let's put here we get player football

player 2 was added to the team football

team so we have this code here

which is called and it says that the

the football player was added to the

team and the name of the football team

and then we have player and we have

baseball player player two

so this

this one

that we have here

that we pass here

baseball team that was this to the

baseball team as you can see it says

player baseball player 2 was added to

the baseball team

and what generics helps out to do

because I said they can help the

compiler to figure out errors if you try

to

pass here baseball TM Dot

add football player so we type here

football player

now you get an error because

the generic type enforces the type on

the the

on the class because here we said

explicitly that this is because this was

inferred to be as a baseball player if

you cannot pass here a football player

in our baseball team because this is in

first because you said Siri here

explicitly explicitly because first

because this was inferred from here so

we have team baseball player so we

cannot add here a football brain inside

our team football player and this is

very very

powerful for us because in this way we

don't do things which doesn't make sense

and the the same is true about our

football team so if on our football team

so if enough of working with a football

team

dot add

and here to put baseball player

we get an error because we specified we

specified that because the generic type

was

in fair to be a football player so we

cannot put a baseball player in our team

of football players so this is what

generics is doing is figure out those

errors for us at compile time and this

is very powerful because we cannot we

can run our code and we don't get in

those some

scenarios where we have a bay of

baseball player added to a baseball team

or maybe other examples there so I'm

gonna delete this

next we're gonna look at upper bounds

because

our upper bounds basically allows us to

restrict what kind of type is passed to

our generic type here so

and particularly gonna see how this is

gonna affect this code that we use here

to cast so to to add an upper bound so

to restrict what kind of type is passed

to this type we can put

colon so extends and here you can we can

type player

so what we are saying here is that

every

type that is going to be passed here

when you create an instance like

we did here so but only that this was in

Fair here this type that is going to be

passed here needs needs to inherit from

the player class otherwise this type is

not going to work so because of that

because the compiler now knows that and

enforces this that this type that is

going to be passed here is is going to

be is going to inherit from the player

class we we can't delete this cast

because now it knows that that type is

gonna have uh it's gonna be it's gonna

inherit from the player class so it's

gonna have the property name so if I

delete this

and if I delete this so let's put name

here

now

so let's delete these two

should be player here

player

so now we have player the name because

it knows that this type so this type

that is going to be passed series is

going to inherit from the player cards

from the player class so it's gonna have

the property name so you don't need to

do this the weird this word cast is

overcasting so I'm gonna delete this

and now as you can see we don't have uh

any arrows so now we have the name here

because now it knows that whatever type

you pass here it has to inherit from the

player class so if I go down here and I

create another class called gamer

player so

we type here class

let's call it games player

and it's gonna have a Val

is going to be

actually let's define a parameter only

name

is going to be a string

and we will not inherit from our player

class let's see what happens so

if I um

if I uh create here another theme let's

call it Val

let's call it the game Steam

so I'm gonna

type here team and we're gonna specify

here

games player

now if if I put now the primary

Constructor here here we need to define

a name

let's call it

games

team

and

multiple list of

now

here we have an error and if you look it

says

expected player found games player and

this happens because our games player

that we have here is not inheriting from

the player class and because we said

here that the upper bound is this T that

is passed here so this T is this here

has to inherit from the player class

otherwise if you if you don't uh inherit

from the player class it will not work

because you said here that it it must

inherit from the player class now if I

change this to

if I change this down here to inherit

from the player class so it I hear

player

we type here name

now as you can see the error disappears

because now it's respecting the upper

bound that we set we set up here which

is T

meaning games player has to inherit from

the player class and down here it's

inheriting so this is our discussion

about how we can use generics and how we

can use how you can Define upper bounds

and in the next videos you're gonna look

at

um

type erasures and you're going to lose

what reified

a keyword and how you can inline our

function with because you need to inline

your function when the use refine the

keyword so see you in the next video

well actually before I end the video I

should say why this uh genetics is

important why

because if you think about because here

you can type we can define a generic

type we don't need to create

um a separate class for for every team

that that we want to use so we don't

need to create a football team we don't

need to create a separate baseball team

we don't need to create a

separate games team we we just create a

team we Define the generic type T and

here we pass our we pass our

our type and in this way we can use only

one class

and we can we can have all of those uh

those uh baseball team we can have the

football team we can have just by having

the the type here which is generic

otherwise we'll have to create separate

classes for each of those

teams so

see you in the next video and next video

as I said we're gonna look at type

erasers and refine the verified the

keyword and the inline functions and in

the next videos we're going to look also

at covariance and counter variance so

see in the next video

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