Encapsulation

  • Learn how encapsulation secures data in Python using access control and class design.

  • Definition of Encapsulation

    Encapsulation is an Object-Oriented Programming (OOP) concept that binds data (variables) and methods (functions) together into a single unit called a class and restricts direct access to some of the object’s data.

    In simple words:
    Encapsulation = Data Hiding + Controlled Access


    Why Do We Use Encapsulation?

    • Protect data from unauthorized access

    • Improve security of the program

    • Prevent accidental data modification

    • Make code easier to maintain

    • Control how data is read or updated

    Real-Life Example

    ATM Machine

    • You cannot directly access your bank balance

    • You must use PIN and ATM options

    • Data is hidden and accessed in a controlled way

    This is Encapsulation

    Key Concepts of Encapsulation

    1. Class

    2. Access Modifiers

    3. Getter & Setter Methods

    4. Data Hiding

    Syntax of Encapsulation

    class ClassName:

        def __init__(self):

            self.variable = value

        def method(self):

            pass

    Access Modifiers in Python

    Python has 3 types of access modifiers:

    Modifier

    Symbol

    Access Level

    Public

    no underscore

    Accessible everywhere

    Protected

    _ (single underscore)

    Accessible within class & child class

    Private

    __ (double underscore)

    Accessible only inside class


    1.Public Attributes & Methods

    Public members are accessible from anywhere (inside or outside the class).

Public Attribute Example

This code shows a public variable that can be accessed directly using the object.

class Student:
    def __init__(self, name):
        self.name = name   # Public attribute

    def show_name(self):
        print("Student Name:", self.name)
s = Student("Aarav")
s.show_name()
print(s.name)   # Accessing public attribute
  • 2.Protected Attributes & Methods

    Protected members are meant to be accessed inside the class and its child classes.
    Python uses _variable to indicate protected access.

Protected Attribute Example

This example shows a protected variable accessed in a child class.

class Employee:
    def __init__(self, salary):
        self._salary = salary   # Protected attribute

class Manager(Employee):
    def show_salary(self):
        print("Salary:", self._salary)

m = Manager(50000)
m.show_salary()
  • 3.Private Attributes & Methods

    Private members are accessible only inside the class.
    Python uses __variable (double underscore) for private access.

    This feature is called Name Mangling.

Private Attribute Example

This code demonstrates how private data cannot be accessed directly outside the class.

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance   # Private attribute

    def show_balance(self):
        print("Balance:", self.__balance)

acc = BankAccount(10000)
acc.show_balance()
# print(acc.__balance)  ❌ Error: Cannot access private attribute
  • Accessing Private Data Using Getter & Setter

    Getter : Used to read private data

    Setter : Used to update private data safely

Encapsulation Using Getter and Setter

This example shows controlled access to private data using methods.

class User:
    def __init__(self):
        self.__password = "abc123"

    def get_password(self):
        return self.__password

    def set_password(self, new_password):
        self.__password = new_password

u = User()
print("Old Password:", u.get_password())

u.set_password("newpass456")
print("New Password:", u.get_password())
  • Encapsulation vs Data Hiding

    Encapsulation

    Data Hiding

    Wraps data & methods

    Hides data from outside

    OOP concept

    Security concept

    Uses classes

    Uses access modifiers