0% completed
Encapsulation is a key concept in object-oriented programming that involves bundling the data (attributes) and the methods that operate on this data into a single unit, or class. This concept also restricts direct access to some of an object’s components, which can prevent the accidental modification of data.
In Python, encapsulation is typically achieved by making certain class attributes private and providing public getter and setter methods to modify these attributes safely. This approach is also known as "using accessor methods".
In this example, we'll create a simple Temperature
class to demonstrate the use of getters and setters for a private attribute.
Explanation:
class Temperature:
- Declares a class named Temperature
.def __init__(self, celsius):
- Defines the constructor with one parameter, celsius
.self.__celsius = celsius
- Initializes a private attribute __celsius
with the value provided during instantiation. This attribute is not accessible directly from outside the class.def get_celsius(self):
- Defines a getter method that allows reading the value of the private attribute __celsius
.def set_celsius(self, value):
- Defines a setter method that allows setting the value of __celsius
after performing a check.if value < -273.15:
- Checks if the provided value
is below absolute zero, which is not physically possible.self.__celsius = value
- Sets the __celsius
attribute to the new value if the check passes.
-. temp = Temperature(25)
- Creates an instance of Temperature
, setting the initial temperature to 25 degrees Celsius.temp.set_celsius(-300)
and temp.set_celsius(10)
- Attempts to set new temperatures, one of which is invalid.In this example, we use encapsulation in a BankAccount
class to manage account balance safely.
Explanation:
class BankAccount:
- Declares a class named BankAccount
.def __init__(self, initial_balance):
- Constructor that initializes the BankAccount
with an initial_balance
.self.__balance = initial_balance
- Sets the __balance
as a private attribute to store the balance of the account securely.def deposit(self, amount):
- Defines a method to deposit money into the account.if amount > 0:
- Checks if the deposit amount is positive.self.__balance += amount
- Adds the deposit amount to the current balance.def withdraw(self, amount):
- Defines a method to withdraw money from the account.if 0 < amount <= self.__balance:
- Checks if the withdrawal amount is positive and does not exceed the current balance.self.__balance -= amount
- Deducts the amount from the current balance.def get_balance(self):
- Getter method that provides read-only access to the account balance.Through these examples, you can see how encapsulation not only protects data but also bundles the operations that manipulate this data into methods, ensuring that the internal representation of an object is hidden from the outside. This approach helps maintain the integrity and consistency of the data throughout the lifetime of the objects.
.....
.....
.....