Abstraction In Python | FACE Prep

Abstraction in Python is the process of hiding the real implementation of an application from the user and emphasizing only on usage of it. For example, consider you have bought a new electronic gadget. Along with the gadget, you get a user guide, instructing how to use the application, but this user guide has no info regarding the internal working of the gadget. 

Another example is, when you use TV remote, you do not know how pressing a key in the remote changes the channel internally on the TV. You just know that pressing ‘+ volume’ key will increase the volume.

Before learning about abstraction in detail, let us look at its importance.

Why Do We Need Abstraction?

Through the process of abstraction in Python, a programmer can hide all the irrelevant data/process of an application in order to reduce complexity and increase efficiency. 

Next, let us see how to achieve abstraction in Python programs.

Understanding Abstraction In Python

In Python, abstraction can be achieved by using abstract classes and methods in our programs. 

Abstract Classes In Python

A class containing one or more abstract methods is called an abstract class.

Abstract methods do not contain any implementation. Instead, all the implementations can be defined in the methods of sub-classes that inherit the abstract class. An abstract class is created by importing a class named ‘ABC’ from the ‘abc’ module and inheriting the ‘ABC’ class. Below is the syntax for creating the abstract class.

Syntax
from abc import ABC
Class ClassName(ABC):

Example
from abc import ABC
Class Shape(ABC):  #abstract class

Here, the class ‘ABC’ is imported to and inherited by the class ‘shape’. The class ‘Shape’ becomes an abstract class when we define abstract methods in it. As we already discussed, abstract methods should not contain any implementation. So, we can define abstract methods using the ‘pass’ statement’ as shown below.

from abc import ABC
class Shape(ABC):
    def calculate_area(self):  #abstract method
        pass

Here, calculate_area is the abstract method of the abstract class ‘Shape’. The implementation of this abstract class can be defined in the sub-classes that inherit the class ‘Shape’. In the below example, ‘Rectangle’ and ‘Circle’ are the two sub-classes that inherit the abstract class ‘Shape’.

from abc import ABC
class Shape(ABC):  #abstract class
    def calculate_area(self):  #abstract method
        pass

class Rectangle(Shape):
    length = 5 
    breadth = 3
    def calculate_area(self):
        return self.length * self.breadth

class Circle(Shape):
    radius = 4
    def calculate_area(self):
        return 3.14 * self.radius * self.radius

rec = Rectangle()  #object created for the class 'Rectangle'
cir = Circle()  #object created for the class 'Circle'
print("Area of a rectangle:", rec.calculate_area())  #call to 'calculate_area' method defined inside the class 'Rectangle'
print("Area of a circle:", cir.calculate_area())  #call to 'calculate_area' method defined inside the class 'Circle'.
Output:
Area of a rectangle: 15
Area of a circle: 50.24

If the implementation of the abstract method is not defined in the derived classes, then the Python interpreter throws an error.

Other Important Points Regarding Abstract Class

Below mentioned points have to be noted while defining an abstract class in Python programs.

  1. An abstract class can have both a normal method and an abstract method
  2. An abstract class cannot be instantiated, ie., we cannot create objects for the abstract class
An abstract class having a normal method and an abstract method

In the below example, ‘print’ is the normal method defined inside the abstract class ‘Shape’. 

from abc import ABC
class Shape(ABC):
    def print(self):
        print("I am a normal method defined inside the abstract class 'Shape'")
    def calculate_are(self):
        pass

class Rectangle(Shape):
    length = 5 
    breadth = 3
    def calculate_area(self):
        return self.length * self.breadth

rec = Rectangle() #object created for the class 'Rectangle'
rec.print()
print("Area of a rectangle:", rec.calculate_area()) #call to 'calculate_area' method defined inside the class 'Rectangle'
Output:
I am a normal method defined inside the abstract class 'Shape'
Area of a rectangle: 15


Here, the normal method is called from the main() method using an object created for the child class ‘Rectangle’.

Abstraction In Python FAQs

Abstraction in Python is the process of hiding the real implementation of an application from the user and emphasizing only on how to use the application.

In Python, abstraction can be achieved using abstract classes and methods. 

No, the abstract class cannot be instantiated, i.e., we cannot create objects for the abstract class.

‘abc’ is the module to be imported when we define an abstract class in Python programs. ‘abc’ stands for ‘abstract base class’.