HOW TO CREATE ABSTRACT CLASSES IN PYTHON?

CREATE ABSTRACT CLASSES IN PYTHON WITH EXAMPLES

Abstract classes in python are a part of object oriented programming. Object oriented programming help the programming for save their time. Create custom classes in their application with more functionality.

Clink here to download PyCharm IDE in your compute or laptop.

In previous article we have discuss about how to create composition and aggregation in python. But now in this tutorial we will learn how to create abstract classes in python with easy and simple example.

Python doesn’t allow creating abstract classes but doesn’t worry we will use the built in module which is allow creating abstract classes.

This tutorial will be very informative for you because in this will learn the following

  1. Inheritance in python
  2. How to create abstract classes in python
  3. How to import module in python

Abstract method in python is that method which must use to implement in the sub class. Abstract method is defined in the supper class. Following are the simple and easy steps which help your how to import and Inheritance in python.

Step 01. Import the abstract module at the top in your python file.

from abc import ABC, abstractmethod

Step 02. Create two classes in your python file first is supper class and second is sub class and supper class is inheritance in sub class as look like this.

CREATE ABSTRACT CLASSES IN PYTHON
CREATE ABSTRACT CLASSES IN PYTHON
class Shape(ABC):
@abstractmethod
def area(self): pass

def perimetter (self):pass
class Sequare(Shape):
def __init__(self, side):
self.__side = side

Step 03. Add the decorator on top of your method here in this example we have two methods so we add two times decorator one is area and 2nd is perimeter.

class Shape(ABC):
@abstractmethod
def area(self): pass

def perimetter (self):pass

If your accesses the method with decorator outside the sub class it will give you’re an error which will says you can’t initiate the class Square which have the abstract method.

So before access the class method outside the sub class you must need to declare the above super class method which have abstract in the inside of supper class.

Below example will more clear your concept about how to create abstract classes in python?

# how to create abstract classes in python
#how to import module in python

from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self): pass

@abstractmethod
def perimetter (self):pass
class Sequare(Shape):
def __init__(self, side):
self.__side = side
def area(self):
return self.__side * self.__side
def perimetter(self):
return 4 * self.__side

sequare = Sequare(5)

once run the code  it will be fine. Now we access the both method in outside the sub class.

Add the follow code in outside the sub class to access the area and perimeter of square

print(sequare.area())
print(sequare.perimetter())

run the  code again you will this line in your output.

Output:
25
20

Conclusion:

In python you can define the abstract classes by importing ABC module and inherit this ABC to that class which you want to become abstract. You need to implement all abstract method in the sub class which have the inheriting the abstract class.

This Post Has One Comment

Leave a Reply