Welcome back.
In this video we are going to see why this whole thing matters.
So why it is so important to use properties and an important aspect of encapsulation.
So the thing is, I just said we had like all of these lambda expressions and all of that stuff.
Don't worry about that for now, because what we're going to do is we're going to simplify this part
here.
How are we going to simplify it?
Let me show you.
So what we are going to do is we're going to use set and then curly brackets.
And here we will define how the value for our brand will be set.
So let me do that again.
Like like this.
So this is the default code here.
So let me go like this okay.
So this is here the entire property right.
So it starts with brand.
And this is the part of the property.
This first part is if you want to get it I'm going to give you the private string up here.
And if you want to set it then you need to go through the logic that I'm going to define inside of these
curly brackets here.
Okay.
So let's define some logic.
So here I could for example check I could say if string is null or empty and here it will be value.
Then I'm going to write onto the console c w you entered nothing okay.
And if somebody does that then we cannot store the brand of the car.
So we say you entered nothing.
Whenever we are passing a value to our brand that is empty, it says you entered nothing.
So what I'm going to do here is I'm going to say, if that's the case, then let me set the brand with
a default value of default value, okay.
And otherwise.
So in this else block, that's where I'm going to set the brand to be whatever the user has entered
okay.
So what's going on here.
Let's look at this.
So whenever we assign a value to brand we're going to say are you empty if you are empty.
So if the value that we are assigning, if it's empty or if it's null, which is like an empty string
is not null.
But here we're saying if you're either null or an empty string, then I'm going to say you entered nothing
via the console.
I'm going to set the value of the brand member variable here.
I'm going to set that to default value.
And then I'm going to go out of this.
So I'm going to go down here and keep going with my code.
But if you didn't enter an empty string or NULL.
So if you entered an actual string in actual text, then I'm going to say, okay, whatever you entered,
I'm going to store that in that private string brand.
So in this member variable okay.
Now let's go ahead and actually use our property here and follow its instructions.
So here I'm going to say Audi Dot brand is going to be console dot read line.
So what I want is I want the user to enter the brand name.
So I'm going to say please enter the brand name.
And then I'm going to say what the user has entered, you entered and then Audi brand okay.
So what we are effectively doing now is here we are setting brand and here we are getting brand.
So what does that mean.
That means that in this line here, that's where we are going to go through this part here and in this
line here.
We're going to go through this part here where we're just saying, okay, just get me the brand member
variable up here.
Okay.
So now let's run our code and see this in action.
So it says please enter the brand name.
So let's say I enter Audi.
It's going to say you entered Audi.
Good job.
Now let's go ahead and enter nothing.
So an empty string and it says you entered nothing.
And then it says you entered default value.
Or maybe we should change that brand name brand is default value.
So it's a little more clear.
So let's run this again I enter nothing.
So it says you entered nothing.
Brand is default value.
And that is part of encapsulation where you define specifically what the condition should be when somebody
tries to access your variables, in this case to access your property.
Because we're saying that if you want to set the property, follow my rules.
My rules are don't give me an empty string.
And if the user then gives us an empty string, or if whenever we're setting an empty string, it's
just going to go to default value, because that's what we define.
And here.
Now we can do the same thing with the Get'er.
Here we can say whenever you're trying to get the variable, we are going to set specific rules as well.
So we can do that.
And I'd say let's do that in the next video.
But just quickly I wanted to make sure that you really understand this value keyword as well, because
I haven't explained it in too much depth.
But what this value is, is in fact whatever we entered here.
So if I entered here car for example, then car is the value that I am passing to my brand so that I'm
setting in my brand.
And that is what this value does.
Okay.
So just so you're aware.
So here it is checking what value is.
Let's run this actually with the breakpoint so that you really see it in action okay.
So it checks straight away in the first line pretty much because that's where we are creating an object
of car.
It checks whether brand is not empty.
And brand is not empty because it's Audi.
Right.
So we can just go into the next step.
It checks.
It's not empty.
Cool.
So it goes here and then it says whatever the value was.
And you see here you can hover over value and it says Audi.
Why is value Audi.
That's because here at this line we are passing Audi to our car constructor.
Now let's look at our car constructor.
What does it do.
Our car constructor.
When it when an object of car is created it says whatever brand you give me I'm going to store that
or I'm going to pass that brand that you give me into the brand property.
This brand property then says, hey, follow my instructions, follow my rules.
My rules are if you want to set a value, you need it to be non-null and non-empty.
And if it is neither null nor empty, then whatever you entered is fine and I'm going to store it inside
of the brand member variable, okay.
And that is why it is directly executed.
And then don't even get to see anything.
So I can go deeper.
Okay.
So then it does it once again for the second time.
So this time it will do it for the BMW.
Okay so new car A3 Audi.
And then it goes to the BMW.
It checks again.
Is BMW empty.
No it's not.
So it's going to set the value.
Then it goes into the constructor back here and says okay.
Yeah.
Well actually you gave me a good value and now it's accessing the brand again.
So here it's saying okay brand of the model has been created and then we are getting out of it and then
it just goes here.
Please enter the brand name.
We're saying the brand name is car.
It checks again.
Whatever is the value.
And you see this time it's car.
That's because we just overrode it here.
We just said car okay.
And then it tries to access it.
And here's our program.
It says audio of the mobile model A3 has been created BMW has been created.
Enter the brand name brand is car okay.
So now you should understand the flow of things.
And in the next video we're going to look at how we can override the Get'er as well.
So see you there.