![]() |
VOOZH | about |
Encapsulation is one of the core concepts of Object Oriented Programming (OOP).
This example shows encapsulation by keeping __salary variable private inside Employee class. It cannot be accessed directly from outside the class.
Output
Fedrick
ERROR!
Traceback (most recent call last):
File "<main.py>", line 8, in <module>
AttributeError: 'Employee' object has no attribute '__salary'
Explanation:
Access specifiers define how class members (variables and methods) can be accessed from outside the class. They help in implementing encapsulation by controlling the visibility of data. There are three types of access specifiers:
Let's discuss it one by one.
Public members are variables or methods that can be accessed from anywhere inside the class, outside the class or from other modules. By default, all members in Python are public. They are defined without any underscore prefix (e.g., self.name).
Example: This example shows how a public attribute (name) and a public method (display_name) can be accessed from outside the class using an object.
John John
Explanation:
Note:__init__ method is a constructor and runs as soon as an object of a class is instantiated.
Protected members are variables or methods that are intended to be accessed only within the class and its subclasses. They are not strictly private but should be treated as internal. In Python, protected members are defined with a single underscore prefix (e.g., self._name).
Example: This example shows how a protected attribute (_age) can be accessed within a subclass, demonstrating that protected members are meant for use within the class and its subclasses.
Ross Age: 30
Explanation:
Private members are variables or methods that cannot be accessed directly from outside the class. They are used to restrict access and protect internal data. In Python, private members are defined with a double underscore prefix (e.g., self.__salary).
Python uses name mangling, where the interpreter internally renames the variable (for eg, __salary becomes _ClassName__salary). This discourages direct access from outside the class, although it does not create strict privacy like other languages.
Example: This example shows how a private attribute (__salary) is accessed within the class using a public method, demonstrating that private members cannot be accessed directly from outside the class.
Robert Salary: 60000
Explanation:
In Python, you can control method access levels using naming conventions:
Note: Unlike other programming languages, Python does not enforce access modifiers like public, private or protected at the language level. However, it follows naming conventions and uses a technique called name mangling to support encapsulation.
Example: This example demonstrates how a protected method (_show_balance) and a private method (__update_balance) are used to control access. The private method updates balance internally, while protected method displays it. Both are accessed via a public method (deposit), showing how Python uses naming conventions for encapsulation.
Balance: ₹1000 Balance: ₹1500
Explanation:
In Python, getter and setter methods are used to access and modify private attributes safely. Instead of accessing private data directly, these methods provide controlled access, allowing you to:
Example: This example shows how to use a getter and a setter method to safely access and update a private attribute (__salary).
50000 60000
Explanation: