Encapsulation and Abstraction
Encapsulation and Abstraction in Python
Encapsulation and Abstraction are two foundational pillars of Object-Oriented software design. Together, they protect internal object integrity, hide internal implementation complexity, and expose clean, intuitive interfaces to external consumers.
1. What is Encapsulation?
Encapsulation is the bundling of data (attributes) and the methods that operate on that data into a single unit (a class), while restricting direct access to internal components to prevent unintended tampering.
Python Naming Conventions for Access Control
Unlike languages with hard public, protected, and private keywords, Python uses naming conventions and name mangling:
| Type | Convention | Meaning | Access Behavior |
|---|---|---|---|
| Public | attribute | Part of the public API | Fully accessible from anywhere |
| Protected | _attribute | Internal use only; intended for class & subclasses | Accessible, but signals "do not touch outside class" |
| Private | __attribute | Strictly private to this class | Triggers name mangling (_ClassName__attribute) |
2. Pythonic Properties: @property and Setters
In traditional OOP (like Java or C++), developers write verbose getBalance() and setBalance() methods. Python replaces this with the elegant @property decorator, enabling getter/setter validation while preserving clean attribute access syntax (acc.balance = 500 instead of acc.set_balance(500)).
3. What is Abstraction?
Abstraction focuses on hiding background details and displaying only essential features to the user. An end-user interacting with a smartphone screen doesn't need to understand RF transmission circuits or silicon microcode—they only interact with high-level buttons and icons.
In Python, abstraction is enforced using the abc (Abstract Base Classes) module:
ABC: Base class from which abstract classes inherit.@abstractmethod: Decorator indicating methods that must be implemented by any concrete subclass.
4. Implementing Concrete Subclasses
Any subclass that fails to implement all abstract methods cannot be instantiated:
Multiple Choice Questions
1. What happens when an attribute name begins with two leading underscores (e.g. __secret)?
A. Python makes the attribute read-only in memory B. Python automatically performs name mangling, changing its internal name to _ClassName__secret C. Python encrypts the value using SHA-256 D. The attribute can only be accessed via an external C extension Answer: B Explanation: Double-underscore prefixes trigger name mangling, prepending _ClassName to the attribute to protect against accidental overrides in subclasses.
2. Which decorator is used to turn a method into a read-only getter attribute?
A. @getter B. @classmethod C. @property D. @abstractmethod Answer: C Explanation: The @property decorator exposes a method as a readable attribute without needing parentheses when called.
3. What error is raised when attempting to instantiate an abstract class that has unimplemented @abstractmethods?
A. NotImplementedError B. AttributeError C. TypeError D. InstantiationError Answer: C Explanation: Python raises a TypeError: Can't instantiate abstract class ... with abstract method ... when trying to instantiate an incomplete abstract class.
4. What is the primary convention of a single leading underscore (e.g. _internal_var)?
A. It indicates private variables enforced by the Python bytecode compiler B. It is an advisory naming convention indicating internal use, signaling other programmers not to access it directly C. It denotes a global variable D. It deletes the variable automatically after the function returns Answer: B Explanation: A single leading underscore is a conventional hint to programmers that the variable is intended for internal implementation, but Python does not technically restrict access.
5. From which standard library module are ABC and @abstractmethod imported?
A. abstract B. typing C. abc D. sys Answer: C Explanation: The standard Python module for abstract base classes is abc (Abstract Base Classes).
Project: Bank Account Class System
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Polymorphism Basics | Project: Bank Account Class System |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.