0% found this document useful (0 votes)
119 views

Data Abstraction in Python

Uploaded by

tudurajanikanta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

Data Abstraction in Python

Uploaded by

tudurajanikanta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Programming

Topperworld.in

Data Abstraction
• The process by which data and functions are defined in such a way that only
essential details can be seen and unnecessary implementations are hidden
is called Data Abstraction.
• Abstraction is really powerful for making complex tasks and codes simpler
when used in Object-Oriented Programming.
• It reduces the complexity for the user by making the relevant part
accessible and usable leaving the unnecessary code hidden.
• There are times when we do not want to give out sensitive parts of our
code implementation and this is where data abstraction can also prove to
be very functional.
• Data Abstraction in Python can be achieved through creating abstract
classes

❖ Abstraction classes in Python


➢ In Python, abstraction can be achieved by using abstract classes and
interfaces.
➢ A class that consists of one or more abstract method is called the abstract
class.
➢ Abstract methods do not contain their implementation. Abstract class
can be inherited by the subclass and abstract method gets its definition
in the subclass.
➢ Abstraction classes are meant to be the blueprint of the other class. An
abstract class can be useful when we are designing large functions. An
abstract class is also helpful to provide the standard interface for
different implementations of components.
➢ Python provides the abc module to use the abstraction in the Python
program.

Let's see the following syntax.

Syntax
1. from abc import ABC
2. class ClassName(ABC):

©Topperworld
Python Programming

❖ Working of the Abstract Classes


• Unlike the other high-level language, Python doesn't provide the abstract
class itself. We need to import the abc module, which provides the base
for defining Abstract Base classes (ABC).
• The ABC works by decorating methods of the base class as abstract. It
registers concrete classes as the implementation of the abstract base.
• 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.

Example:

from abc import ABC, abstractmethod

class AbstractClassExample(ABC):
@abstractmethod
def do_something(self):
pass

class ConcreteClassExample(AbstractClassExample):
def do_something(self):
return "Concrete class doing something!"

# Trying to create an instance of AbstractClassExample


directly will raise an error
# obj = AbstractClassExample()

obj = ConcreteClassExample()
print(obj.do_something())

©Topperworld
Python Programming

Output:

Concrete class doing something!

❖ Points to Remember
Below are the points which we should remember about the abstract base class
in Python.

• An Abstract class can contain the both method normal and abstract
method.
• An Abstract cannot be instantiated; we cannot create objects for the
abstract class.

Abstraction is essential to hide the core functionality from the users.

©Topperworld

You might also like