Sitemap

#PYTHON(DAY25)

1 min readMay 21, 2023

--

Data Abstraction

It hides the unnecessary code details from the user. Also, when we do not want to give out sensitive parts of our code implementation and this is where data abstraction came.

Data Abstraction in Python can be achieved through creating abstract classes.

Press enter or click to view image in full size
abstraction

Why Abstraction is Important?

In Python, an abstraction is used to hide the irrelevant data/class in order to reduce the complexity. It also enhances the application efficiency. Next, we will learn how we can achieve abstraction using python programing.

Get Bhavitha Cherukuri’s stories in your inbox

Join Medium for free to get updates from this writer.

We use the @abstractmethod decorator to define an abstract method or if we don’t provide the definition to the method, it automatically becomes the abstract method.

Syntax:

from abc import ABC  
class ClassName(ABC):

Example:

# abstract base class work   
from abc import ABC, abstractmethod
class Car(ABC):
def mileage(self):
pass

class Tesla(Car):
def mileage(self):
print("The mileage is 30kmph")
class Suzuki(Car):
def mileage(self):
print("The mileage is 25kmph ")
class Duster(Car):
def mileage(self):
print("The mileage is 24kmph ")

class Renault(Car):
def mileage(self):
print("The mileage is 27kmph ")

# Driver code
t= Tesla ()
t.mileage()

r = Renault()
r.mileage()

s = Suzuki()
s.mileage()
d = Duster()
d.mileage()

And the output is:

The mileage is 30kmph
The mileage is 27kmph
The mileage is 25kmph
The mileage is 24kmph

--

--

Bhavitha Cherukuri
Bhavitha Cherukuri

Written by Bhavitha Cherukuri

My passion for writing motivated me to start writing blogs and the content within is written in a simple and easy format. so that readers can understand easily.