WEBVTT

00:00.000 --> 00:09.480
In capsulation and access modifiers, in this video we will understand the concept of encapsulation

00:09.480 --> 00:15.680
and why it is used in object-oriented programming, identify and apply different access modifiers,

00:15.680 --> 00:21.400
private, public, and protected, implement getters and setters to provide controlled access

00:21.400 --> 00:28.480
to class fields, apply data validation and setters to protect objects from invalid states,

00:28.480 --> 00:34.240
understanding encapsulation. In capsulation is a core concept in Java that helps us hide

00:34.240 --> 00:40.160
the internal state of an object. Instead of allowing direct access to fields, encapsulation

00:40.160 --> 00:45.800
gives controlled access through specific methods. This prevents accidental or unauthorized

00:45.800 --> 00:50.980
changes to the objects' data and protects it from misuse.

00:50.980 --> 00:54.960
Access modifiers To implement encapsulation, Java provides

00:54.960 --> 01:01.500
access modifiers. Public allows access from anywhere in the program.

01:01.500 --> 01:07.440
Private restricts access only within the same class, which is commonly used to protect fields.

01:07.440 --> 01:12.280
Protect it allows access within the same package or subclasses.

01:12.280 --> 01:16.840
Choosing the right modifier helps determine how safely and securely each part of the object

01:16.840 --> 01:19.880
can be accessed.

01:19.880 --> 01:24.840
Getters and Setters When fields are private, we use getters

01:24.840 --> 01:30.760
and setters to read and update those values. For example, a private string name field

01:30.760 --> 01:35.720
can be accessed only through get name and set name string name.

01:35.720 --> 01:42.000
This gives us full control. We decide what happens before data is stored or returned.

01:42.000 --> 01:49.240
Setters can also include validation, allowing only acceptable data to enter the object.

01:49.240 --> 01:54.520
High encapsulation matters Consider a person class.

01:54.520 --> 01:59.640
If age were public, someone could set it to a negative number, which makes no sense.

01:59.640 --> 02:04.800
But if age is private and updated only through a setter, we can add a rule that age cannot

02:04.800 --> 02:11.680
be negative. This maintains data integrity and makes the program easier to maintain.

02:11.680 --> 02:15.760
Internal implementation can change later without affecting the rest of the code because

02:15.760 --> 02:20.840
other classes communicate only through getters and setters.

02:20.840 --> 02:25.400
In encapsulation safeguards the internal data of an object while still allowing access through

02:25.400 --> 02:31.080
controlled methods. With proper access modifiers and validation, Java classes become more

02:31.080 --> 02:34.080
secure, reliable, and easier to maintain.

