What is Encapsulation and Abstraction in Python OOP ?

Encapsulation and Abstraction are concepts of Object-Oriented Programming that can help us create good programs in Python by promoting modularity, security, and maintainability.

 

Encapsulation: The Python language can use this fundamental concept of OOP programming.

In Python, encapsulation is the practice of bundling data and methods that operate on that data into a single unit, typically a class. It restricts direct access to certain components of an object, thereby enhancing data security and integrity. This is a work as a class in Python.

For Example, we can observe that water bottles are filled with water. ​The bottle’s cap closes the bottle, and its body covers the water, ensuring the water inside remains secure and safe. Same as encapsulation can work in programming. Now we can understand encapsulation by the image below to make it clear up the doubts.

It is used in Object Orientation programming

Example:
class Temperature:       #Creating the encapsulation using the class keyword
     def __init__(self, celsius):
          self.__celsius = celsius

     def to_fahrenheit(self):
          return (self._celsius *9/5) + 32

     def set_celsius(self, value):
          if value < -273.15:
              print("Temperature cannot be below to zero(0)")
    Else   else:
             self,__celsius = value

     def get_celsius(self):
          return self.__celsius

tem = Temperature(25)
print(tem.to_fahrenheit())
tem.set_celsius(-300)
print(tem.get_celsius())
tem.set_celsius(30)
Output:

77.0
Temperature cannot be below to zero(0)
25

Bellow link is helping to find the real code:

https://github.com/virpatidar/python-2-encp

Abstraction: Abstraction is a fundamental concept in object-oriented programming (OOP) that enhances security and simplifies complexity.

Abstraction in Python involves hiding the complex implementation details of a system and exposing only the essential features to the user. In this, it makes good, secure data and details of the code. It can only show the external work of the program and hide the inner work.

For Example, A projector’s external casing hides its internal components, enabling users to operate it without understanding its inner workings.This demonstrates abstraction in Python, where programmers hide complex implementation details to expose only the essential features.

 

Example:
from abc import ABC, abstractmethod   #import abstraction
import math                           #import math for pi value
pi = math.pi
class Shape(ABC):                     # Create class
    @abstractmethod                   # define abstraction
    def area(self):
        pass

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return pi * (self.radius ** 2)


shapes = [Rectangle(5, 3), Circle(4)]


for shape in shapes:
    print(f"The area is {shape.area()}")

 

Output:

The area is 15
The area is 50.26548245743669

Bellow link is helping to find the real code:

https://github.com/virpatidar/python-3-abc

We have provided the some more links that are helping you to study further things below:

How to Perform HTTP Requests in Python Using the requests Linbary
Submit form data in Post method in Django

In summary, encapsulation is about bundling data and methods that operate on that data into a single unit and restricting access to some of the object’s components. Abstraction is about hiding the complex implementation details and showing only the essential features of an object. Both concepts work together to create a clear and maintainable code structure

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top