×

Python Abstract Class

An Introduction to Abstract Classes

An Abstract Class is one of the most significant concepts of Object-Oriented Programming (OOP). An Abstract class can be deliberated as a blueprint or design for other classes. It is a sound practice of the Don't Repeat Yourself (DRY) principle as duplicating codes in a large project is approximately similar to reusing bugs. A developer can't remember all details of the classes. Thus, the use of an abstract class becomes efficient while defining a common interface for distinct implementations. It also allows the developers to design a set of methods created necessarily within any subclasses constructed from the abstract class.

When a class contains one or more abstract methods is known as Abstract Class. The abstract methods are the methods that only have declarations but do not have any detailed implementation.

Some features of an Abstract class are as follows:

  • We cannot instantiate an abstract class. It only helps as an interface for child classes to avoid any code duplication. It does not make any sense to instantiate an abstract class.
  • We can implement the abstract methods using a derived child class to create a concrete class that fits the abstract class's interface. Thus, we cannot instantiate it until and unless all of its abstract methods get overridden.

In short, we can also say that an abstract class is used to define a common interface for a set of child classes or subclasses. It delivers common properties and methods for all child classes to reduce duplication of code. It also helps in enforcing the child classes to implement an abstract method avoiding odd contradictions.

What is the need for Abstract Base Classes?

We can define a common API (Application Program Interface) for a set of child classes by defining an Abstract Base Class. This Abstract Base Class capability becomes specifically useful in conditions where a third-party provides implementations like in the case of plugins. However, it can also help us while working in a big team or with a large codebase where it is difficult to remember all the classes.

Working of Abstract Base Classes

Python does not provide Abstract Classes by default. However, it does come with a module providing the base to define Abstract Base Classes (abbreviated as ABC). This Python module is known as ABC. The ABC module works by adorning base class methods as abstract and then registering concrete classes as abstract base implementations. We can create a method abstract by decorating using the keyword, namely @abstractmethod.

Let us consider the following example to understand the working of an Abstract Base Class.

Example:

from abc import ABC, abstractmethod 
 class Shapes(ABC): 
     # defining abstract method 
     def sides(self): 
         pass
 class Circle(Shapes): 
     # overriding an abstract method 
     def sides(self): 
         print("Circle has ONE curved side.") 
 class Triangle(Shapes):
     # overriding an abstract method 
     def sides(self): 
         print("Triangle has THREE sides.") 
 class Square(Shapes):
     # overriding an abstract method 
     def sides(self): 
         print("Square has FOUR sides.") 
 class Pentagon(Shapes):
     # overriding an abstract method 
     def sides(self): 
         print("Pentagon has FIVE sides.") 
 shape_1 = Circle()
 shape_1.sides()
 shape_2 = Triangle()
 shape_2.sides()
 shape_1 = Square()
 shape_1.sides()
 shape_2 = Pentagon()
 shape_2.sides() 

Output:

 Circle has ONE curved side.
 Triangle has THREE sides.
 Square has FOUR sides.
 Pentagon has FIVE sides. 

Explanation –

In the above example, we have imported the ABC module and defined a class called Shapes. We have then defined an abstract method called sides. Later, we have defined some subclasses such as Circle, Triangle, Square, and Pentagon that inherit the attributes and methods of the superclass Shapes and override the abstract method. At last, we have created some objects and print the output to the user.

Using Subclassing for Abstract Class Implementation

The need for explicitly registering the class can be avoided by subclassing from the base directly. In such cases, the Python class management helps in recognizing PluginImplementation with the abstract PluginBase implementation.

Let us look at the following example to understand the concept of implementing abstract class via subclassing.

Example:

import abc 
 class superClass:    
     def myMethod(self):
         pass
 class subClass(superClass): 
     def myMethod(self):
         print("This a Subclass.") 
 print( issubclass(subClass, superClass)) 
 print( isinstance(subClass(), superClass)) 

Output:

 True
 True 

In the above example, we have imported the abc module. We have defined two classes: the first one is the superClass, where the other is its subClass and defined an abstract method. Finally, we have used two functions, namely issubclass() and isinstance(), respectively.

The issubclass() function allows the user to check if the first argument is a child class of the second argument. The isinstance() function allows the user to check if the object (i.e., first argument) is an instance or the second argument's child class.

However, there are some side-effects of utilizing direct subclassing. We can find all the user's plugin implementations just by asking the parent class for the list of acknowledged classes inherited from it.

Concrete Methods in Abstract Base Classes

Concrete Classes consist of only normal (concrete) methods, whereas the abstract classes may consist of the abstract methods as well as the concrete methods. The concrete class delivers an implementation of the abstract methods, whereas the abstract base class can also deliver an implementation by invoking the methods using the super() function.

Let us consider the following example illustrating the functioning of the super() function in invoking a method.

Example:

import abc 
 from abc import ABC, abstractmethod 
 class myClass(ABC): 
     def myMethod(self): 
         print("Abstract Base Class") 
 class mySubClass(myClass): 
     def myMethod(self):
         super().myMethod() 
         print("Child Class") 
 first_class = mySubClass() 
 first_class.myMethod() 

Output:

 Abstract Base Class
 Child Class 

Explanation –

In the above example, we have imported the abc module and defined a superclass, named myClass. We have then defined a method, named myMethod() for myClass. Later, we have defined another class, named mySubClass, that inherits the attributes and methods from myClass. We have also used the super() function to invoke the methods in abstract classes. Finally, we have created an object called first_class of mySubClass to print the Output to the user.

Understanding Abstract Properties

Abstract Classes involve methods as well as attributes. The attributes can be required in the concrete classes by defining them with the keyword known as @abstractproperty.

Let us consider the following example illustrating abstract properties.

Example:

import abc 
 from abc import ABC, abstractmethod 
 class parent_class(ABC): 
     @abc.abstractproperty 
     def myMethod(self): 
         return "The Superclass"
 class child_class(parent_class):
     @property
     def myMethod(self): 
         return "The Subclass"
 try: 
     first = parent_class() 
     print( first.myMethod())
 except Exception as err:
     print (err)
 first = child_class() 
 print( first.myMethod) 

Output:

 Can't instantiate abstract class parent_class with abstract method myMethod
 The Subclass 

Explanation –

In the above snippet of code, we have imported the abc module. We have defined two classes: the parent_class class as a base class and the child_class class that inherits the base class's methods and attributes.

We have used the return with a try/except statement to return some statements. At last, we have inserted an object, namely first and print the output.

Moreover, in the output, it is visible that the Parent class cannot be instantiated. The program returned this error because of having only an abstract version of the property getter method.

Instantiating an Abstract Class

Abstract Classes are incomplete as they contain methods with nobody. Suppose Python provides access to create an object for abstract classes then utilizing it if anyone calls the abstract method. However, there are no such actual implementations for invoking. Thus, we utilize an abstract class like a template, and we extend it as per the requirement and build on it before utilization. Hence, an abstract class is not a concrete class, and we cannot instantiate them. It will return an error while creating an object for an abstract class.

Let us look at the following example, illustrating that we cannot instantiate an abstract class.

Example:

from abc import ABC, abstractmethod 
 class Cars(ABC):
     @abstractmethod
     def intro(self): 
         pass
 class BMW(Cars): 
     def intro(self): 
         print("This is a BMW car.") 
 class Volkswagen(Cars):
     def intro(self): 
         print("This is a Volkswagen car.") 
 class Ford(Cars):
     def intro(self): 
         print("This is a Ford truck.") 
 class Jaguar(Cars):
     def intro(self): 
         print("This is a Jaguar car.") 
 car = Cars() 

Output:

 Traceback (most recent call last):
   File "D:\Python\abstract.py", line 23, in <module>
     car = Cars()
 TypeError: Can't instantiate abstract class Cars with abstract method intro 

Related Topics

Python Operator Precedence

Before knowing about the operator precedence in Python, we have to know about the operators in Python. So let's have a look at it. According to one definition, the operator is...

3 minutes read.

Import Module in Python

In this article, you will learn everything about “ Import ” in Python. Modules in python that are already created can be accessed and used in another code by importing the...

6 minutes read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.

Create First GUI Application using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Best Python AI Projects

Artificial consciousness is advancing quickly, from Chabot's to self-driving vehicles. Because of the various advantages and development presented by AI, numerous enterprises have begun searching for AI-fueled applications. Thus, there...

7 minutes read.

Sentence to python vector

Conversion of a Sentence to Vector in Python Before starting the tutorial, let’s just recap about the vector and the respective package that has to be imported in Python. Python Vector: Putting simply,...

3 minutes read.

Python TCP Server

The TCP server library is also known as the socket library. Socket programming is the method of connecting two nodes through a network to form communication between two nodes. In...

3 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

2 minutes read.

Looping through Data Frame in Python

Iterate over Rows and Columns in Pandas Dataframe This tutorial aims to make us understand what Pandas in Python are, what Data Frame in Python is and its significance, what are...

4 minutes read.

How to change the names of Columns in Python

Introduction To play with huge amounts of data, in Python we require a tool. The tool which is available in Python is Pandas. Pandas is an open-source library. It is used...

3 minutes read.

One Liner If-Else Statements in Python

Before learning one liner if else Python, let us first know what Python is. Python is a broadly helpful, object-oriented and dependable language having a large number of utilizations from...

4 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

Python Redis Example

Introduction: Redis, representing Distant Word reference Server, is a kind of key-esteem NoSQL server. Redis is intended to help circle capacity for determination, holding information after power is stopped, and stores information...

5 minutes read.

Python | Read csv using pandas.read_csv()

Python is an excellent language for performing information analysis, owing to the fantastic biological system of information-driven python packages. Pandas is one of those packages that make taking in and...

4 minutes read.

Python String Negative Indexing

This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing. What is an Index? An index is a numeric value, which is assigned...

3 minutes read.

What is the re.sub() function in Python

There. sub () function is used to return a string by replacing the occurrences of a specific character or pattern with a replacement string. To use this function, import the...

3 minutes read.

How to Iterate a List in Python

As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and...

3 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

Python Dictionary get() method

Python Dictionary get() method The dictionary.get() method returns the value of the item with the specified key. Syntax dictionary.get(keyname, value) Parameter keyname- This parameter represents the key to be searched in the dictionary. value- The parameter...

1 minute read.

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand...

3 minutes read.