Lesson 7: Encapsulation
OOP
Encapsulation
Starter activity
2
Now, let's relate this to programming:
Starter activity
3
Now, let's relate this to programming:
Starter activity
This code simulates a locked treasure chest containing valuable items. We try to open the chest with a key, and if successful, we can access the items inside. Encapsulation ensures that only specific methods can interact with the chest and its contents, keeping them secure.
Explain how this code works
What will this code output?
4
Lesson 8: Encapsulation
Objectives
Lesson objectives:
5
Literacy – Key Words | |
Instance | an occurrence of an object // a specific object based on the class // an instantiation of a class. |
Inheritance | the capability of defining a new class of objects that has all the attributes and methods from a parent class. |
Containment | a relationship in which one class has a component that is of another class type |
Encapsulation | combining data and subroutines into a class and restricting external access to the data |
Now, let's relate this to programming:
Starter activity
Private Attributes:
In both the TreasureChest and ValuableItems classes, attributes are prefixed with double underscores (__), making them private.
These private attributes cannot be accessed directly from outside the class.
Getter Methods:
Both classes have methods (get_gold_coins() and get_precious_gems()) that allow indirect access to the private attributes.
These methods encapsulate access to the private attributes and provide controlled access from outside the class.
6
Now, let's relate this to programming:
Starter activity
Setter Method:
Although not explicitly shown in the provided code, encapsulation could also involve setter methods to modify the private attributes, ensuring data integrity and validation.
Example (hypothetical setter method):
This setter method would encapsulate the modification of the __gold_coins attribute, allowing validation before changing its value.
Overall, encapsulation in this program ensures that the internal state of the TreasureChest and ValuableItems classes is protected and can only be accessed and modified through controlled interfaces (getter and setter methods), promoting data integrity and code maintainability.
7
Knowledge Check
What is encapsulation in Python?
A) The process of breaking down a problem into smaller subproblems
B) The bundling of data and methods into a single unit
C) The process of converting code from one programming language to another
D) The process of optimizing code for better performance
8
Question 1: Easy
Which of the following best describes containment in Python?
A) One class inherits from another class
B) One class contains an instance of another class
C) One class has access to methods of another class
D) One class overrides methods of another class
9
Question 2: Easy
Which of the following is true about encapsulation?
A) It allows any part of the program to access and modify data freely�B) It hides the internal details of a class from the outside world�C) It only allows methods to be encapsulated, not data�D) It is only used in procedural programming, not in object-oriented programming
10
Question 3: Medium
What is the purpose of encapsulation in Python?
A) To make code run faster�B) To make code easier to write�C) To hide the internal details of a class and protect its data�D) To enforce strict typing in Python
11
Question 4: Medium
In Python, what prefix is commonly used to indicate a private attribute or method?
A) Single underscore ()�B) Double underscore ()�C) Triple underscore ()�D) No prefix is used
12
Question 4: Difficult
Which of the following statements about encapsulation is false?
A) Encapsulation allows for data hiding and abstraction�B) Private attributes and methods can be accessed from outside the class�C) Encapsulation helps in maintaining code integrity and security�D) Encapsulation promotes modularity and code reusability
13
Question 5: Difficult
Which access modifier in Python allows access to attributes and methods only within the class and its subclasses?
A) Public�B) Private�C) Protected�D) Hidden
14
Task 1: Understanding encapsulation
Activity 1
Create a Python class called Person with a private attribute __name. Implement a method get_name() to retrieve the name of the person.
15
Task 2: Implementing encapsulation
Activity 2
Create a Python class called BankAccount with a private attribute __balance. Implement methods get_balance() to retrieve the balance and deposit(amount) to add funds to the account. Ensure that the balance cannot be accessed directly from outside the class.
16
Task 3: Advanced Encapsulation
Activity 3
Extend the BankAccount class from Task 2 to include a method withdraw(amount) to withdraw funds from the account. Ensure that the balance cannot become negative after a withdrawal.
17