WEBVTT

00:00.170 --> 00:03.800
So in previous examples, everything was public.

00:03.830 --> 00:10.310
Now in this video we're going to learn about encapsulation in object oriented design.

00:10.310 --> 00:17.060
So let's create a class that would be called Bankaccount because that's a good example.

00:17.180 --> 00:21.980
And first start with a private field that would be float.

00:22.400 --> 00:26.720
And it would be balance initialized with zero.

00:28.040 --> 00:31.880
So we haven't used the private visibility modifier yet.

00:31.880 --> 00:33.500
So I need to explain that.

00:33.830 --> 00:42.530
So this means that I can only access this field from within this bank account class, its methods,

00:42.530 --> 00:45.710
and I can't access it from outside.

00:46.400 --> 00:52.850
So if I'm going to create this object now also keep in mind that you don't need a constructor.

00:53.240 --> 00:54.710
It's optional.

00:54.770 --> 01:00.800
And I'd like to echo the account balance.

01:00.800 --> 01:04.790
This would This would throw an error because it is private.

01:04.940 --> 01:06.620
So let me run this.

01:09.200 --> 01:10.730
And we've got a fatal error.

01:10.760 --> 01:14.180
Cannot access a private property.

01:15.200 --> 01:17.270
So without encapsulation.

01:17.270 --> 01:23.990
And if the balance would be public, I could do all kind of weird stuff.

01:23.990 --> 01:25.730
So this is a bank account.

01:25.730 --> 01:32.090
And yet I would be able to set the balance at will at -400 maybe.

01:32.120 --> 01:36.050
So this is not how bank account class should work.

01:36.080 --> 01:40.700
It should give us functionality that is safe to use.

01:40.700 --> 01:48.110
So we should either get the balance, we should deposit money and withdraw the money.

01:48.110 --> 01:53.630
But when withdrawing you should not be able to go below the balance.

01:53.660 --> 02:02.390
So that's why to do anything with a bank account, we're gonna use encapsulation by providing public

02:02.390 --> 02:03.500
methods.

02:05.720 --> 02:10.460
Okay, so let's create a get balance function.

02:10.460 --> 02:13.160
This would be a so-called getter.

02:15.710 --> 02:18.890
And it simply returns the balance.

02:18.890 --> 02:21.470
So I do this balance.

02:21.470 --> 02:26.840
That's the way I can access the current balance and echo it.

02:26.840 --> 02:37.700
So maybe I'm just going to do var dump account get balance and I should get zero in this case.

02:39.470 --> 02:40.310
There it is.

02:40.310 --> 02:45.530
So now let's add some methods that will let us modify the balance.

02:46.070 --> 02:55.880
Let's start with deposit where there is one argument called amount and it is void.

02:55.880 --> 03:00.590
So it doesn't return any value.

03:00.590 --> 03:09.790
Now here we can check if the amount is more than zero because we don't want to deposit negative values.

03:10.540 --> 03:15.490
And if that's the case we can add this to the balance.

03:15.490 --> 03:21.160
So this plus equals operator will just add the amount to balance.

03:21.370 --> 03:29.830
So again if we wouldn't check if the amount is non-negative we could accidentally subtract money from

03:29.830 --> 03:33.070
the balance when we try to deposit money.

03:33.700 --> 03:33.970
Okay.

03:34.000 --> 03:36.160
So now this is safe to use.

03:36.160 --> 03:44.110
But something a little bit more complex is to withdraw the money from the account.

03:44.140 --> 03:49.930
So this also accepts an amount and it can return a boolean.

03:49.930 --> 03:55.030
So this would just mean whether it was successful or not.

03:56.350 --> 03:56.650
Okay.

03:56.650 --> 04:02.890
So first we need to make sure that the amount is More than zero.

04:04.300 --> 04:12.370
Another thing is that we need to make sure that the actual balance is more or equal the amount to be

04:12.370 --> 04:13.450
withdrawn.

04:14.710 --> 04:16.780
If that's the case.

04:19.090 --> 04:25.660
We just subtract the amount from the balance and we return true.

04:26.470 --> 04:32.650
Otherwise we just return false which means the operation was not successful.

04:34.870 --> 04:35.110
Okay.

04:35.110 --> 04:38.890
So now let's do a couple of operations in here.

04:38.890 --> 04:41.680
First on the account.

04:43.450 --> 04:45.340
I'm going to deposit something.

04:45.880 --> 04:51.190
Then on the account or from the account I'm going to withdraw 500.

04:51.190 --> 04:56.200
And finally we're going to see what's the remaining deposit.

04:56.710 --> 04:58.750
I expect 500.

04:58.750 --> 05:01.930
And that's the case.
