WEBVTT

00:00.000 --> 00:05.000
encapsulation and data-hiding.

00:05.000 --> 00:10.000
In this lesson, we will learn what is encapsulation and Python,

00:10.000 --> 00:13.000
how to use public, protected, and private attributes,

00:13.000 --> 00:17.000
how, getters, setters, and property decorators

00:17.000 --> 00:22.000
control data access, concept of encapsulation and data-hiding.

00:22.000 --> 00:25.000
In encapsulation simply means keeping data

00:25.000 --> 00:27.000
and the methods that work on that data together

00:27.000 --> 00:30.000
in one unit, usually a class.

00:30.000 --> 00:33.000
It's like packing your clothes and shoes neatly in a suitcase

00:33.000 --> 00:36.000
so everything stays organized.

00:36.000 --> 00:39.000
In Python, we use access modifiers to control

00:39.000 --> 00:41.000
who can see or change the data.

00:41.000 --> 00:44.000
Public data can be accessed by anyone.

00:44.000 --> 00:47.000
Protected data, written with one underscore attribute,

00:47.000 --> 00:52.000
should only be used inside the class or its child classes.

00:52.000 --> 00:55.000
Private data, written with two underscores attribute,

00:55.000 --> 00:59.000
is hidden from outside the class.

00:59.000 --> 01:02.000
Controllers to protect data.

01:02.000 --> 01:06.000
To control and protect our data, we use getters and setters.

01:06.000 --> 01:09.000
Getters allow us to safely read data.

01:09.000 --> 01:12.000
Setters allow us to safely change data,

01:12.000 --> 01:16.000
often with some checks to make sure the new value is valid.

01:16.000 --> 01:19.000
Python also gives us a shortcut, property decorators,

01:19.000 --> 01:24.000
so we can manage getters and setters in a cleaner way.

01:24.000 --> 01:28.000
Let's build a program to understand encapsulation in Python.

01:28.000 --> 01:30.000
We start with a class student,

01:30.000 --> 01:33.000
our blueprint for keeping data and methods together.

01:33.000 --> 01:37.000
Self.name is public, so anyone can access it.

01:37.000 --> 01:42.000
underscore grade is protected, meant for use inside the class or subclasses.

01:42.000 --> 01:48.000
Double underscore marks is private, hidden from outside.

01:48.000 --> 01:52.000
To read private marks, we use a getter method.

01:52.000 --> 01:55.000
To update them safely, we use a setter with a check,

01:55.000 --> 02:02.000
so values stay between 0 and 100.

02:02.000 --> 02:05.000
For grade, we use a property decorator,

02:05.000 --> 02:08.000
a cleaner way to control access with validation.

02:08.000 --> 02:11.000
Finally, we create a student, test the attributes,

02:11.000 --> 02:17.000
and see how encapsulation protects our data while keeping it organized.

02:17.000 --> 02:23.000
In encapsulation keeps data safe and organized by controlling how it's accessed or changed.

02:23.000 --> 02:26.000
With getters, setters, and property decorators,

02:26.000 --> 02:31.000
Python makes data protection both simple and powerful.

