1. Encapsulation:
Encapsulation is one of the four fundamental Object-Oriented Programming (OOP) concepts and involves bundling the data (attributes) and methods (functions) that operate on the data within a single unit, i.e., the class. It helps in hiding the internal details of the class and only exposing what is necessary.
1.1 Access Modifiers:
In Python, encapsulation is often achieved through access modifiers. Although Python doesn’t have strict access modifiers like some other languages (e.g., private, protected, public), it uses conventions to indicate the intended visibility.
- A single underscore
_
before an attribute or method name implies that it is intended to be protected. - A double underscore
__
before an attribute or method name implies that it is intended to be private.
1.2 Example:
class Circle:
def __init__(self, radius):
self._radius = radius # Encapsulation using a single underscore
def get_radius(self):
return self._radius
def set_radius(self, new_radius):
if new_radius > 0:
self._radius = new_radius
else:
print("Invalid radius value.")
# Creating an instance of the Circle class
circle = Circle(5)
# Accessing and modifying the radius using encapsulation
print(circle.get_radius()) # Output: 5
circle.set_radius(7)
print(circle.get_radius()) # Output: 7
circle.set_radius(-2) # Output: Invalid radius value.
2. Abstraction:
Abstraction involves hiding the complex implementation details of an object and exposing only the necessary features to the outside world. It helps in simplifying the interface and reducing the impact of changes in the implementation.
2.1 Abstract Base Classes (ABC):
In Python, abstraction is often achieved using Abstract Base Classes (ABC). An abstract base class defines abstract methods that must be implemented by its subclasses.
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
2.2 Example:
# Using abstraction to work with shapes without worrying about their specific implementations
def print_area(shape):
print(f"The area of the shape is: {shape.area()}")
# Creating instances of the Circle and Rectangle classes
circle = Circle(5)
rectangle = Rectangle(4, 6)
# Using the print_area function with different shapes
print_area(circle) # Output: The area of the shape is: 78.5
print_area(rectangle) # Output: The area of the shape is: 24
3. Conclusion:
Encapsulation and abstraction are crucial principles in Object-Oriented Programming that contribute to code organization, modularity, and maintainability. Encapsulation helps in hiding the internal details of a class, and abstraction allows you to work with objects at a higher level without worrying about their specific implementations.
In the next sections, we’ll explore more advanced topics and practical applications of encapsulation and abstraction in Python.