‫Welcome back.
‫In this video, I would like to show you properties and teach you how properties work.
‫And because one video would be too long and I created a video in the past about properties and it was
‫a little too confusing as it seemed.
‫I decided to break it down and rerecord it because it just wasn't good enough as it seems.
‫So if you find another video which is totally confusing, it doesn't help you at all.
‫Then please let me know because I want this course to be the best course out there.
‫All right.
‫So what we need to do is, first of all, create a new class.
‫So let's go to project and add a new class, because I want to teach you the fundamentals of properties
‫because they are an important aspect or an important concept that we're going to use a lot.
‫That's why it's really important that you understand the whole breadth of it.
‫So we start by creating a new class and I'm going to call this class box.
‫All right?
‫So just be very simple class.
‫So this box class is going to just be a class that has a certain amount of variables.
‫All right.
‫So, for example, just want to calculate the volume of the box based on its length, its height and
‫its width.
‫All right.
‫So let's just create those variables, and I'm going to make them public for now.
‫All right?
‫I'm going to make this one public length, then another, and this one will be the height, and then
‫we need the width.
‫And finally, I want to also have the volume.
‫So the volume will be the end result of this box.
‫All right.
‫So now I have a bunch of variables.
‫Now, those variables are not simple variables.
‫They are, in fact, member variables, which means that they are the variables on the level of the
‫class, in this case of the class box.
‫So they have a direct impact on the whole appearance of the box or the whole structure of a box, so
‫to say.
‫All right.
‫And we're going to see how we can use member variables in a different way or even a different structure,
‫which is even safer.
‫Because the problem here by is that they are public, so they're not safe.
‫We will see how we can fix this.
‫So first of all, let's go ahead and create a new method here.
‫And I'm just going to call this one display info, because the idea behind it is that it will just display
‫the length, the width, the height, and then it will calculate the volume and tell us how big the
‫volume is.
‫All right.
‫I'm just going to write a short right line statement.
‫So I enter CW double tab, which creates this console right line statement here for me.
‫And in here I'm just going to say something like length is going to be the first entry and height is
‫going to be the second entry.
‫So I use it like this and with is the third entry.
‫So the volume is and then I have the fourth entry.
‫All right, so this is my whole right line statement and then the variables that I want to use is length,
‫height.
‫With.
‫And volume.
‫Now, volume is something that I need to calculate based on length, height and width.
‫So I'm just going to do that.
‫I'm just going to say the volume is the length multiplied with the height multiplied with the width.
‫Doing so creates this volume or calculates the volume because the volume is the result of this calculation
‫here.
‫Length, time, side times, width, it's going to be the volume.
‫All right.
‫So now let me split it up into lines so it's a little more readable.
‫All right.
‫So now we have this method display info.
‫Now let's go ahead and save this file and go to our program keys.
‫Here we are going to use this class.
‫We're going to create an object of this class.
‫How do we create objects?
‫Well.
‫So I create a new object of it.
‫I'm going to call this one box and it's going to be a new box.
‫So this is how you create an object of a class or an instance of a class.
‫So I'm creating an instance of this class box.
‫So pretty much a variable of type box and box is what we created ourselves.
‫Now what I can do is I can assign the values to it so I can go ahead and say, okay, the length is
‫going to be three, then the height is going to be four and the width is going to be five, something
‫like that.
‫Of course, I need an equal sign here as well in order for this to be correct code.
‫Now, what I can do is I can just go ahead and call box dot.
‫Display info.
‫So that's pretty cool.
‫I don't need this bright line statement here anymore.
‫I can now save this file and execute it.
‫So let's run this file and it will display us the info in the console.
‫So that we are length is free and height is four and width is five, so the volume is 60.
‫All right.
‫So that worked out.
‫That's great.
‫So this display info worked out and it just displaying the details that we wanted.
‫Now what we could do is we could change the values or what we can change the values of the member variables
‫of our box class.
‫And we can do so because they are public.
‫Right?
‫So this one is public.
‫This one is public.
‫This one is also public.
‫So all of them are public.
‫Now, this is very insecure code because at this point, if you write member labels which are totally
‫public, everyone can change them at any point.
‫So everyone who can create an object or every class that can create an object of box, we'll be able
‫to change all of those values of that instance of the box.
‫So everyone was access to this box can change it now.
‫And that is something that can cause a lot of problems.
‫Go further down the road, right when you program and a team and stuff, then it can really become cumbersome
‫to do it like this.
‫So there are better ways.
‫There are cleaner ways to implement it because maybe you don't want the length to be adjustable, maybe
‫you want the length to have a certain value.
‫No one can adjust it.
‫Then what you could do is you could make this.
‫Verbal privates.
‫So my member variable length is going to be private and I'm just going to assign a default value of
‫three to it.
‫So now every box that I create will have the value for the length of three.
‫So every box will have a length of three inches or meters or whatever the metrics are that you're using.
‫So now I cannot assign length anymore.
‫So you see, I get an error here.
‫Box length is inaccessible due to its protection level.
‫So we added the protection level by making it private.
‫So now we cannot adjust it anymore.
‫So how can we still change it?
‫Well, we can't.
‫Not that we're this way.
‫So we need to implement something.
‫We need to implement an alternative way to change the length of the box.
‫All right.
‫In order to do so, what we can do is we can implement something called setters and getters.
‫So in this case, we cannot even access the length of the box anymore.
‫So let's say I want to write the length.
‫So I want to console right line box length is and then just going to access box dot length.
‫As you can see, my ID doesn't even show me the variable.
‫I can't access box dot length so I cannot change it.
‫But I can also not read it.
‫All right.
‫So I can neither get it nor set it.
‫So this is setting and this is getting the variable.
‫These are very important terms setting and getting, because now what we can do is we can create methods
‫in our box class which will allow us to set or get the variable length.
‫So let's just do that.
‫Let's create a.
‫Sutter so public.
‫Voit set length.
‫So this variable will set the length.
‫Now, what will it set it to?
‫Well, it needs a variable or a parameter here.
‫It needs a value here that it can set it to.
‫So it's just going to.
‫Need an integer here, and I'm just going to call this one length.
‫So it will set the length.
‫Now I will set length, equal length.
‫Now, this is confusing and it's also incorrect.
‫So I get a warning here.
‫Don't even get an error here, because as you can see, the colors are different, right?
‫So this color and that color are slightly different.
‫This one is this very light color and this one is a darker blue color.
‫So what is going on here?
‫Because we are using the same word here.
‫We're using length here.
‫We're using length here and here, here as well.
‫So this length here is actually this or should be this length here at the top.
‫So if I want to do it correctly, I can use this this keyword to it.
‫By doing so, I say, okay, I want to change the length, which is.
‫Parts.
‫Of this class called Box.
‫So I want to change the length of this object.
‫So in this case, of the object that is created of the class box.
‫All right, so whatever length I'm getting assigned.
‫So whatever length is passed when set length is called.
‫This method will be.
‫This variable here, and it will be set to the length, which is my private member variable of my box.
‫This is very important because what I can do now is I can now go ahead and change the length of the
‫box by doing so.
‫Set length.
‫I can also set the length to, for example, three and now run the code again and it will work perfectly
‫fine.
‫And let's see.
‫All right.
‫There we are.
‫Length is free.
‫Height is four and so forth.
‫All right.
‫So now let's say I don't want to have three because it was the default value.
‫Now, let's say I want to have the length as four, and now we can see it's the working and the volume
‫also has adjusted.
‫So it even worked for the calculation.
‫So that's great.
‫So now we can set a variable which is a member variable of a class.
‫So that's important.
‫We're talking about the different class.
‫It's not in the same class.
‫Now what we can do is we can try to access this length because it's private, right?
‫So how could I access it?
‫Because maybe I really need this variable here somewhere.
‫I want to access the length, but I can't do it.
‫So what I can implement is something called a getter.
‫So a get length public.
‫And in this case the type has to be the same type as the variable that I want to return.
‫And in this case, I want to return the value of this length or I want to return the length itself,
‫right?
‫So it's of type int.
‫So I need to return an int type for this method.
‫So this one will be get length and this is also the convention to use the name, get with the capital
‫G and then the name of the variable that you want to get.
‫And in this case I want to get the variable length and now you just return the length.
‫Now, in order to be more precise, to return this dot length, of course you could just write the length,
‫but when you add this, then it's perfectly clear that we're talking about the member variable here.
‫So we're talking about the variable that is on the highest level of this class.
‫So it's not within any method or so you couldn't even access it in this way.
‫But here it's this particular length that we're talking about.
‫So if I click on it, you can see my ID automatically marks it.
‫And it's also marking this length here at the bottom.
‫So now I can get the length by using get length by using this public method.
‫And it's important that it's public because if it wasn't public, then I couldn't access it.
‫So in this case, I'm saving the box again, and now I'm getting the length by calling this method get
‫length, and now it suddenly works again.
‫So now I can go ahead and of course write it correctly here as well length and let's run it again.
‫And it's going to say box length is.
‫And in this case, it's four.
‫All right.
‫So this is just the preparation for properties, but it's an important concept.
‫Setters and getters, this is something that you are going to see quite a bit as well.
‫So it's a totally fine approach, but there are a lot easier and faster approaches.
‫So this is just one way to do it.
‫But I'm going to show you two or even three other ways to make it even quicker and to have the same
‫type of protection.
‫Because what we're doing here is called encapsulation.
‫So we are encapsulating the axis of our length.
‫The definition of Wikipedia is that encapsulation refers to the building of data with the methods that
‫operate on that data, or the restricting of direct access to some of an object's components.
‫So that's exactly what we're doing.
‫We are restricting the access to this component called length, which is part of an object or our class
‫box of which we can create objects.
‫All right.
‫And we create these setters and getters in order to still adjust them.
‫Now, the beauty is that you can define how you want to encapsulate.
‫In this case, I made both of them public.
‫But what if I don't want them to be public?
‫What if I want this to be?
‫Not suitable.
‫So let's say I don't want anyone to be able to set the length of my box.
‫So what I could do is I could make this one private.
‫So now only.
‫Classes that inherit from our box or the box itself will be able to set the length from outside.
‫We will not be able to set the length anymore.
‫So setting length here, this line of code is not working anymore because I decided, okay, if anyone
‫wants to set the length, I wanted to only be the class itself or inheriting classes, but not classes
‫outside of me.
‫This is something that you can directly modify from here.
‫You could also say, okay, even getting the length would not be allowed outside of here.
‫All right.
‫But now the cool thing is you can go ahead and adjust those.
‫So let's say somebody wants to get the volume.
‫And now at this point, we haven't set the volume yet.
‫So what we can do instead, we can go ahead and say public int, get volume.
‫And now if anyone wants to get the volume, we're not just going to return volume, but what we will
‫do is we will return.
‫This dot length multiplied with this dot height multiplied with this dot width.
‫So if anyone wants to get the volume, they will not get the volume directly.
‫So not get access to this variable directly, this volume variable, but they will actually get the
‫result of the calculation.
‫So now if I save this and instead go ahead and here I use get volume, then boxes volume is and now
‫it will display the volume of the box.
‫So let's say boxes, volume is 60.
‫So now generally what we do with these setters and getters is that we don't give access directly to
‫the variable to the member variable, but we only give access to a method which then can adjust the
‫variable and we can ourselves decide how we pass this value, so how we hand out the value in this case
‫with get volume, we decided, okay, we don't want to give you the volume itself, but we want to give
‫you the result of a calculation.
‫Or we could say, okay, if anyone wants to get the volume, we are going to say it's going to be volume
‫times two or something like that.
‫Or if anyone wants to set the length and it's something negative.
‫So let's say we make this public again, public void, set length, and now we double check if length.
‫Is less than zero.
‫We're going to throw an error, throw new exception size or this one, a length should be higher than
‫zero.
‫All right.
‫So if length is less than zero, then we throw an exception and this will just crash our program.
‫So this line will never be executed.
‫So now let's try this.
‫Let's go to our program and set our box length to a negative value.
‫Let's set it to minus four.
‫Now we run it again and we will see that we get an exception.
‫So our program crashes and we get an exception here saying.
‫System exception length should be higher than zero.
‫So you see, with throwing the exception, we can throw our own exceptions.
‫We can even say what the exceptions should say and it should be meaningful.
‫It should be something that is useful and makes clear what the error is.
‫In this case, we created an error because we set the length to something that is less than zero.
‫All right.
‫So that's the power of these setters and getters, because you might wonder, why would I need those
‫setters and get us?
‫Why can I not just simply access the length directly if I just have setters and getters?
‫Well, now you see it, you can actively decide what will be set and what will be forgotten.
‫If somebody tries to access this variable and change it, or even just retrieve it.
‫All right.
‫So this was a lot longer than I had anticipated, and I was planning on doing this video.
‫But I really wanted to make everything clear because this is going to be an important stepping stone
‫for the next concept, which is going to be properties.
‫So you're going to see how we can sum this all up or make it a lot shorter by using properties.
‫So I'd say let's go to the next video where we're going to check out how to use properties and how they
‫relate to what we have learned in this video.
‫I hope it made sense.
‫If you have any questions, please let me know.
‫And yeah, just write a comment in the course discussions.
‫And by the way, thanks a lot for everyone who gives feedback because it really helps me to make the
‫course better.
‫If you just passively watching this course, it's totally fine.
‫But I really appreciate if you have feedback and it's constructive and really helps me to know at which
‫point something isn't clear.
‫Because if you just say, okay, this video is complete garbage, then it will not be as useful as if
‫you say, okay, I didn't understand concept A at minute five or something, then I understand a lot
‫better what the problem is.
‫So thanks again and see you in the next video.