9 6 Encapsulation Explained
Key Concepts
Encapsulation in Python involves several key concepts:
- Data Hiding
- Access Modifiers
- Getter and Setter Methods
- Practical Applications
1. Data Hiding
Data hiding is the practice of restricting access to certain attributes or methods of a class. This prevents direct modification of the internal state of an object.
Example:
class BankAccount: def __init__(self, balance): self.__balance = balance def deposit(self, amount): self.__balance += amount def withdraw(self, amount): if amount <= self.__balance: self.__balance -= amount else: print("Insufficient funds") account = BankAccount(1000) account.deposit(500) account.withdraw(200)
Analogy: Think of data hiding as a vault that stores valuable items, accessible only through specific methods.
2. Access Modifiers
Access modifiers control the visibility of attributes and methods. In Python, there are no strict access modifiers like in some other languages, but conventions like using single or double underscores are used to indicate private or protected members.
Example:
class Car: def __init__(self, make, model): self.__make = make # Private attribute self.__model = model # Private attribute def get_make(self): return self.__make def get_model(self): return self.__model car = Car("Toyota", "Camry") print(car.get_make()) # Output: Toyota print(car.get_model()) # Output: Camry
Analogy: Think of access modifiers as security levels that determine who can access certain information.
3. Getter and Setter Methods
Getter and setter methods are used to access and modify private attributes. They provide a controlled way to interact with the internal state of an object.
Example:
class Person: def __init__(self, name, age): self.__name = name self.__age = age def get_name(self): return self.__name def set_name(self, name): self.__name = name def get_age(self): return self.__age def set_age(self, age): if age > 0: self.__age = age else: print("Age must be a positive number") person = Person("Alice", 30) print(person.get_name()) # Output: Alice person.set_age(35) print(person.get_age()) # Output: 35
Analogy: Think of getter and setter methods as controlled access points to a safe, where only authorized personnel can retrieve or deposit items.
4. Practical Applications
Encapsulation is crucial for maintaining the integrity of an object's state and ensuring that it behaves predictably. It is commonly used in scenarios involving data validation, security, and maintaining invariants.
Example:
class Employee: def __init__(self, name, salary): self.__name = name self.__salary = salary def get_name(self): return self.__name def get_salary(self): return self.__salary def set_salary(self, salary): if salary > 0: self.__salary = salary else: print("Salary must be a positive number") employee = Employee("Bob", 50000) print(employee.get_name()) # Output: Bob employee.set_salary(55000) print(employee.get_salary()) # Output: 55000
Analogy: Think of encapsulation as a protective shield around an object, ensuring that its internal workings are safe from external interference.
Putting It All Together
By understanding and using encapsulation effectively, you can create robust and maintainable code that protects the integrity of your objects.
Example:
class Product: def __init__(self, name, price): self.__name = name self.__price = price def get_name(self): return self.__name def get_price(self): return self.__price def set_price(self, price): if price > 0: self.__price = price else: print("Price must be a positive number") product = Product("Laptop", 1000) print(product.get_name()) # Output: Laptop product.set_price(1200) print(product.get_price()) # Output: 1200