Hello everyone.
So in the last video we started discussing about whoops we learned what is whoops.
Object Oriented Programming Baghdad object and how we can create glasses.
So in this video we will see how to create objects and how to access its properties so in the last video
we created this class.
And we know how to create glasses.
So now let's see how to create object of this class
so when we write so when we write in a is the name of the variable and a is of type in digit.
Now we have a class student
this student has two properties age and William.
But now I want to create object S1.
Now I want to create a student or I can say I want to create a variable S1 of type student.
How can we say that because glasses nothing but a user defined data type
that another name of glasses user defined data type for example and double floored.
These all are built into the types building types whereas class is a user defined data type okay.
So when we write in a is the name of the variable but when we write student S1 S1 is the name of the
object here.
We will call it a variable.
And here we will call it the object a is of type in digit S1 is of type student.
Okay.
So a student.
Class glasses nothing but the user defined data type.
So let's see when we write in into what will happen we will get four blocks of memory space and the
name of this block is a.
Okay.
And there will be random values random garbage value so when we write student S1 we will get a memory
block the name of this block is S1 and virtual boot size so student contain two integers.
Rule number and age both our 44 bytes.
So in total eight bytes so the size of this block will be approximately eight bytes not exactly.
It byte because there are many other things also we do not need to understand them yet.
Okay so this is nearly eight bytes approximately eight bytes but not exactly it bytes.
So S1 will contain H and it will have rule number and garbage values.
Okay.
Similarly when we write in day there was garbage values.
So similarly when we create an object there will be garbage values.
Similarly we can create an understood end student as to again we will get approximately eight bites
of memory space name is as to age and a number.
And similarly garbage value Okay so till now we have created objects statically below we are creating
objects strategically but we can also create objects dynamically
so in the static how we create objects practically in.
Okay.
Student S1 student S2
So what will happen.
Like student s when is it s containing age and rule number garbage values so why garbage values.
Because we are not setting the properties ourself below that's why garbage values.
Similarly for Sudan to age rule number garbage values
Okay now let's see how we can create objects dynamically so for creating an integer dynamically the
same indexes into start a new and
the.
So this is this index for creating an integer.
Benny Michaeli.
So what will happen in the heap memory we will get for bytes of memory space and in the static memory.
This is the name of the variable e and efforts.
This is handed down and it will be here.
Okay so this is static memory
and this one is he similarly if we want to create an object dynamically the syntax will be similar to
this one we will write student
then start as t is new keyword and then the data type which is student So what will happen in the heap
memory.
We will get nearly eight bytes and this will have each rule number garbage value and in the state in
the static memory we will get eight bytes of memory space because as TS a point.
Similarly the size of A's eared bytes because it's a pointer and let's say for there this is 300.
So this will contain 300.
Okay this will contently entered
okay.
So what we used to do with the variables we can also do with objects.
Now let's sing called
so let's create object statically
so we can create many object student as to similarly we can create many objects in one line like we
used to do with variables so as tree as food as 5 and so on.
Now let's create object dynamically so creating objects dynamically
so this index is quite similar.
So student start as six as new keyword and then later type which is student
now let's see how we can set the property of an object.
Okay so we have an object S1 S1 contain H.
And rule number
and garbage of ideas.
Similarly we have object as to age rule number garbage value.
And let's take one dynamic object.
So this is s.t..
Now this is our heat memory and here we have each and William but which contain garbage values.
Okay so when we write and a word will happen we will get four bytes of memory space.
Now if we want to put something inside this box what we will do we will write a equals five so five
will come here.
Now similarly if I want to write something here what I can do is one equals.
24 now is it right or wrong it is wrong because S1 one contain multiple properties if I want to set
each this index will be as one dot each equals 24.
Now this is right.
So 24 will come here.
This value will become 24.
Similarly I can central number s one dot rule number is let's say a hundred.
So this value will become a hundred.
Okay so syntax is very simple so what we have to right this is object name dot property name.
Okay.
Object name dot property name.
Now let's see in code
as one dot age let's say 24 S1 DOT rule number let's say a hundred and similarly we can also print c
out as rendered age
C out as one dot.
Rule number.
Okay so this is a very simple index to access the properties of an object.
Now let us talk about how to access properties of our dynamically created object.
So we have object S6 which is dynamically created sorry.
Wait a second
so if we have an integer variable which is dynamically created.
So what will be the situation and star.
A is new and so this is it bytes pointer and this is our 4 bytes memory space so we cannot write simply
equals 5.
This is wrong.
The right way to do is first we have to reach in this box now for reaching this box.
We will use R to start R D defense operator.
So start is five now five will come here.
Similarly in case of dynamic created object we will do something similar to this.
So we have object S6 which is dynamically created.
So S6 is new student Okay so this is a pointer of eight bytes and now this box contain H and lumber
which are garbage initially.
Okay so we cannot write a six dot age.
This is wrong.
What it will do is it will go to a six and inside a six.
It will try to find each but it is present in this box.
Age is present in this box.
So we have to reach this box for it in this box.
We will use data fence operator.
So start S6 by doing statistics.
We have reached this at this box dot age let's say 5 now 5 will come here.
Similarly star as 6 Dato number let's add 200 so two hundred will come here.
Okay so in case of four dynamically created objects we have to use data fence operator to reach this
box okay so let's good
we have to use star operator so start A6
last age twenty three
is six Natali numbers
let's say 30 okay.
Now this is.
Now we have all.
Now we also have a shortcut to do this.
We will write a six and a each is 23 A6 and orderly number is 30.
Okay so these two lines and these two lines that exactly seem uh exactly same means uh if we will right
start Essex dot age and we will write a six note age ethics arrow age.
So these two are exactly same
this arrow is a shortcut.
Okay this is a shortcut they will exactly Wuxi.
But this is a shortcut.
Many people use it.
Instead of using stored and then dart and then brackets and all that stuff it is much simpler to use
ETL okay.
Now
if we will run this file we will get a lot of errors.
Let's see why
okay.
So I am telling you if we will run this file we will get a lot of errors.
So let's see so we are getting a completion edit and let's see that ID.
So this is dated ages private roll number is private.
Age is private.
Again age is private.
So we are getting something.
So we are getting an error which is telling us that age.
Roland but these are private.
Now let's see what is private.
So there is a concept in Opes called access modifiers excess modifiers so excess modifiers is who all
can access the properties and functions of a class.
Okay.
So there are two types of excess modifier public private and the third one is predicted
so we will not discuss about protected yet.
We will discuss about public and private.
So what private uses private will say you can access properties of a class inside the class only.
Okay.
So I would say as you can access the properties only inside the class what we are doing here is C H.
Andrew Lambert can only be accessed within student class letters is between this bracket and this record
private will say this to fields.
Rule number and age can only be accessed within the student class that is between this bracket and this
bracket.
Okay
what public says is if the access modified is public then you can use the properties of the class anywhere
anywhere in your program.
You can use the properties of the class.
Whereas if the access modified is private you can use the properties inside the class only.
So here we are when we created these two variables these two properties we have not defined any access
modifier so by default the access modifier is private.
Okay so default access modifier is private if you will not specify the access modify it.
Default will be private.
So here rule number n h by default they are private.
And what I'm doing here is C I am using excess I am using each and lumber outside the class in the main
function I am using each and number.
That's why we are getting a compilation error which is saying that rule number n h are private and you
cannot use them outside the class.
Similarly we are using a general number here.
Okay.
So what we have to do if we will if I want to use the properties of the class outside the class we have
to make them public.
So for public work we will do.
There is a very simple index.
We just have to write the keyword public so we will use.
We will write the keyword public and then column.
So after writing public column what will happen
so after writing public everything everything below public will become public.
So now rule number and age they are public and now this line is correct this line is correct.
Basically we will not get any uh any compulsion at it.
Okay.
Now let's see how come by lament program so we are getting ahead.
So what is the idea.
Okay.
This Beckett it's right now
see how program is running
okay.
Now what if
I make in Deja each private.
So private.
Now what will happen now
so when you write public everything inside public will become public.
So this rule number is public.
But now we have defined and another access modifier which is private.
So now age is private.
So it can only be used between this and this blanket that is each can only be used inside.
Class and the but can be used any weird
so as one dot age is 24.
This line is correct because.
Sorry.
This line is wrong because age is private but this line is correct.
Why.
Because it is public.
Rule number is public.
Age is private.
So we will get compilation error wherever we have used each and outside the class.
Let's come bilateral program.
Now this time if you will see that ad that it will be only due to age ages private ages private ages
private there will be no added regarding the roll number.
Okay.
So this way we can use like we can define multiple access modifiers.
Okay.
Now I want to show you one more thing.
Let's remove this called okay.
Uh let's remove everything else
okay.
So what I did it is I uh I'm creating a lot I care to file student do you start CPB.
And this is I want all the class which is containing the class only.
Now why I am doing this is
see these are concept in software development which is called isolation of logic so overt isolation
of logic seizes whenever you want to create a class create a class in a separate file.
So we will have a file.
Now this file will create the class and we will have another file in this file.
We will use this class use class here okay.
So it's like best practice in the software in the software industry.
Because the logic is different.
Here we are here we will just define the class.
And here we will use class.
So I did exactly the same.
Okay.
So I have a class which is student and I have entered the class and I have another file student.
You start TPB here.
We are using the student class.
Now if I will run this program it will to me and added model with data.
It will say I don't know what is student why.
Because student is defined in a separate file.
So if I want to use student in this file right what I have to use I have to include that file in this
in this current file.
So hashing glued downloaded commas name of the file student art CPB okay.
Now this is correct.
Now let's start.
Now let's turn our program
okay.
So this is training me at it.
Uh what is the error.
Let me see.
And funds in danger.
Private.
Oh I have to run this class also.
Now this.
Now this will be correct see it is running now.
Okay.
So always this is a good practice.
Make glass and separate file and use data class in your current file.
Now.
Now when we included I use gym class I used this ad was for I use string but then I used but when I
included student file I used the downloaded data commas because for Ford built in files the files which
are already present we have to use these arrows.
But if we create a fly file on our own that is user file the file that we have created we have to use
devil records.
Okay.
This is just a syntax.
Nothing else.
Okay.
So thank you.