Python Tutorial

Introduction Python Features Python Applications System requirements for Python Python Installation Python Data Types Python IDE Python Keywords Python Operators Python Comments Python Pass Statement

Python Conditional Statements

Python if Statement Python elif Statement Python Switch Case

Python Loops

Python for loop Python while loop Python Break Statement Python Continue Statement Python Goto Statement

Python Arrays

Python Array Python Matrix

Python Strings

Python Strings Python Regex

Python Built-in Data Structure

Python Lists Python Tuples Python Dictionary Python Sets

Python Functions

Python Function Python min() function Python max() function Python User-define Functions Python Built-in Functions Anonymous/Lambda Function in Python

Python File Handling

Python File Handling Python Read CSV Python Write CSV Python Read Excel Python Write Excel Python Read Text File Python Write Text File Read JSON File in Python

Python Exception Handling

Python Exception Handling Python Errors and exceptions Python Assert

Python OOPs Concept

OOPs Concepts in Python Inheritance in Python Polymorphism in Python Python Encapsulation Python Constructor

Python Iterators

Iterators in Python Yield Statement In Python

Python Generators

Python Generator

Python Decorators

Python Decorator

Python Functions and Methods

Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods

Python Modules

Python Modules Python Datetime Module Python Calendar Module  

Python MySQL

Python MySQL Python MySQL Update Operation Python MySQL Delete Operation

Python MongoDB

Python MongoDB

Python Data Structure Implementation

Python Stack Python Queue Python Hash Table Python Graph

Python Advance Topics

Speech Recognition in Python Face Recognition in Python Python Rest API Python Command Line Arguments Python JSON Python Virtual Environment Type Casting in Python Collections in python Python Enumerate Python Debugger Python DefaultDict

Misc

Python PPTX Python Pickle Python Seaborn Python Coroutine Python EOL Python Infinity Python math.cos and math.acos function Python Project Ideas Based On Django Reverse a String in Python Reverse a Number in Python Python Word Tokenizer Python Trigonometric Functions Python try catch exception GUI Calculator in Python Implementing geometric shapes into the game in python Installing Packages in Python Python Try Except Python Sending Email Socket Programming in Python Python CGI Programming Python Data Structures Python abstract class Python Compiler Python K-Means Clustering List Comprehension in Python3 NSE Tools In Python Operator Module In Python Palindrome In Python Permutations in Python Pillow Python introduction and setup Python Functionalities of Pillow Module Python Argmin Python whois Python JSON Schema Python lock Return Statement In Python Reverse a sentence In Python tell() function in Python Why learn Python? Write Dictionary to CSV in Python Write a String in Python Binary Search Visualization using Pygame in Python Latest Project Ideas using Python 2022 Closest Pair of Points in Python ComboBox in Python Python vs R Python Ternary Operators Self in Python Python vs Java Python Modulo Python Packages Python Syntax Python Uses Python Logical Operators Python Multiprocessing Python History Difference between Input() and raw_input() functions in Python Conditional Statements in python Confusion Matrix Visualization Python Python Algorithms Python Modules List Difference between Python 2 and Python 3 Is Python Case Sensitive Method Overloading in Python Python Arithmetic Operators Design patterns in python Assignment Operators in Python Is Python Object Oriented Programming language Division in Python Python exit commands Continue And Pass Statements In Python Colors In Python Convert String Into Int In Python Convert String To Binary In Python Convert Uppercase To Lowercase In Python Convert XML To JSON In Python Converting Set To List In Python Covariance In Python CSV Module In Python Decision Tree In Python Difference Between Yield And Return In Python Dynamic Typing In Python Abstract design pattern in python Builder design pattern in python

How to

How to convert integer to float in Python How to reverse a string in Python How to take input in Python How to install Python in Windows How to install Python in Ubuntu How to install PIP in Python How to call a function in Python How to download Python How to comment multiple lines in Python How to create a file in Python How to create a list in Python How to declare array in Python How to clear screen in Python How to convert string to list in Python How to take multiple inputs in Python How to write a program in Python How to compare two strings in Python How to create a dictionary in Python How to create an array in Python How to update Python How to compare two lists in Python How to concatenate two strings in Python How to print pattern in Python How to check data type in python How to slice a list in python How to implement classifiers in Python How To Print Colored Text in Python How to develop a game in python How to print in same line in python How to create a class in python How to find square root in python How to import numy in python How to import pandas in python How to uninstall python How to upgrade PIP in python How to append a string in python How to open a file in python

Sorting

Python Sort List Sort Dictionary in Python Python sort() function Python Bubble Sort

Programs

Factorial Program in Python Prime Number Program in Python Fibonacci Series Program in Python Leap Year Program in Python Palindrome Program in Python Check Palindrome In Python Calculator Program in Python Armstrong Number Program in Python Python Program to add two numbers Anagram Program in Python Even Odd Program in Python GCD Program in Python Python Exit Program Python Program to check Leap Year Operator Overloading in Python Pointers in Python Python Not Equal Operator Raise Exception in Python Salary of Python Developers in India What is a Script in Python Singleton design pattern in python

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 



ADVERTISEMENT
ADVERTISEMENT