What is Abstraction in Python with an Example?

First thing first, we know before reading this article you might have gone through other articles as well and it may have been confusing or you might not have the correct answer, but let me assure you after reading this article it won’t be the case. In this article you will get almost all the concepts and doubts cleared about abstraction once and for all. So, let’s begin.

Just one last thing: I request you to read the complete article once and then decide.

Abstract or abstraction.

The simple meaning of abstraction is existing in thought or an idea which has no physical existence. This itself is the full idea behind abstraction in python.

Abstraction is a concept in which we declare a class as abstract and inside that class we define some methods. Now, methods in this class are only declared not defined meaning we have only declared methods in abstract class we haven’t passed any data to these methods. This is what I mean by abstract method.

abstract method

Now the question arises: what is an abstract class?

An abstract class is a class in which we inherit the ABC class from the abc module and whose methods are only declared. This is what I meant by this.

abstract _class_python

Here abc is a module and ABC is a class inside module abc, which is used to make any class as an abstract class. In this case it is Shape class.

abstactraction_in_python_with_code

You Can Copy Code Here Below.

from abc import ABC

class Shape(ABC):

def show(self):
pass

class Rectangle(Shape):
def __init__(self, len, bred):
self.len = len
self.bred = bred

def show(self):
return self.len*self.bred

class Square(Shape):
def __init__(self, side):
self.side = side

def show(self):
return self.side*self.side

r = Rectangle(4, 5)
print(r.show())
s = Square(4)
print(s.show())

After defining an abstract class, we define other classes as well and in these classes, we inherit this abstract (Shape) class and do all the coding, data handling, data passing etc tasks here in this base class. This is what I mean.

Here we have created a shape class as an abstract class with the help of the abc module. After that we have created a Rectangle class which is inheriting our abstract class which is shape, and since it is inheriting it it is also using its show method and passing some data to this show method and utilising it as per needs. 

This full concept till here is known as abstraction.

Some concepts about abstraction.

  • Abstraction hides the code complexity.
  • Abstraction acts as a blueprint for other classes.
  • We cannot create objects of abstract class. 
  • To make any class as an abstract class we have to import the ABC class from the abc module.

Related Post:

Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

Your email address will not be published.