Back to: Python Tutorials For Beginners and Professionals
Abstract Classes in Python
In this article, I am going to discuss Abstract classes in Python with Examples. Please read our previous article where we discussed Polymorphism in Python. As part of this article, we are going to discuss the following pointers which are related to Abstract classes in Python.
- What is an Abstract Class in Python?
- Types of Methods in Python based on the Implementation
- How to declare an abstract method in Python
- Abstract Classes in Python
- Can abstract classes contain more sub classes?
- Different cases for Abstract class object creation
What is an Abstract Class in Python?
An abstract class is the class which contains one or more abstract methods. An abstract method is the one which is just defined but not implemented.
The above statements may seem unclear with the new terms. Let’s discuss them in detail in this article.
Types of Methods in Python:
Based on the implementation the methods can be divided into two types:
- Implemented methods.
- Un-implemented method.
Implemented methods:
A method which has a both method name and method body, that method is called an implemented method. They are also called concrete methods or non-abstract methods. The methods which we have seen in the previous chapters are implemented i.e having method name and body as well.
Un-implemented methods:
A method which has only method name and no method body, that method is called an unimplemented method. They are also called as non-concrete or abstract methods.
How to declare an abstract method in Python:
Abstract methods, in python, are declared by using @abstractmethod decorator. For better understanding, please have a look at the bellow image.
Method ‘one’ is abstract method. Method ‘two’ is non-abstract method. Since one abstract method is present in class ‘Demo’, it is called Abstract class
Abstract Method in Python
From the above example, we can understand that an abstract class is the one which does not have implementation. Few points about it:
- By using @abstractmethod decorator we can declare a method as an abstract method.
- @abstractmethod decorator presents in abc module. We should import the abc module in order to use the decorator.
- Since abstract method is an unimplemented method, we need to put a pass statement, else it will result in error.
- Class which contains abstract methods is called an abstract class.
- For abstract methods, implementation must be provided in the subclass of abstract class.
Program: Abstract methods (demo1.py)
from abc import * class Demo1(ABC): @abstractmethod def m1(self): pass @abstractmethod def m2(self): pass def m3(self): print("Implemented method")
Output: No Output because no object is created. Just a demo to show abstract methods
Abstract Classes in Python:
- Every abstract class in Python should be derived from the ABC class which is present in the abc module. Abstract class can contain Constructors, Variables, abstract methods, non-abstract methods, and Subclass.
- Abstract methods should be implemented in the subclass or child class of the abstract class. (demo2.py)
- If in subclass the implementation of the abstract method is not provided, then that subclass, automatically, will become an abstract class. (demo3.py)
- Then, if any class is inheriting this subclass, then that subclass should provide the implementation for abstract methods. (demo4.py)
- Object creation is not possible for abstract class. (demo4.py or demo8.py)
- We can create objects for child classes of abstract classes to access implemented methods.
Program: Abstract Class (demo2.py)
from abc import ABC, abstractmethod #Abstract Class class Bank(ABC): def bank_info(self): print("Welcome to bank") @abstractmethod def interest(self): "Abstarct Method" pass #Sub class/ child class of abstract class class SBI(Bank): def interest(self): "Implementation of abstract method" print("In sbi bank 5 rupees interest") s= SBI() s.bank_info () s.interest()
Output:
In the above example, Bank is an abstract class which having intertest() abstract method. SBI class is a child class to Bank class, so SBI class should provide implementation for abstract method which is available in Bank abstract class. An object is created to subclass, which is SBI, and the implemented method interest() is called.
If child class misses to provide abstract methods’ implementation
If a child class inherits an abstract class, doesn’t provide the implementation for abstract methods, then that child class also will be considered as an abstract class. Object creation for such child classes is also not possible. Even, if we try to create an object an error will be thrown.
Program: Abstract Class (demo3.py)
from abc import ABC, abstractmethod #Abstract Class class Bank(ABC): def bank_info(self): print("Welcome to bank") @abstractmethod def interest(self): "Abstarct Method" pass #Sub class/ child class of abstract class class SBI(Bank): def balance(self): print("Balance is 100") s= SBI() s.bank_info () s.balance()
Output:
In the above example, Bank is an abstract class, which is having an abstract method intertest(). SBI is a child class to Bank class, so SBI should provide implementation for abstract method interest() of Bank class, but it is not providing. So, the class SBI will also be considered as an abstract class.
Hence, if we try to create an object to it, then we will get TypeError. Now, if we define a class which is inheriting SBI class, there we should provide the implementation for interest method.
Program: abstract class and subclass of abstract class (demo4.py)
from abc import ABC, abstractmethod #Abstract Class class Bank(ABC): def bank_info(self): print("Welcome to bank") @abstractmethod def interest(self): "Abstarct Method" pass #Sub class/ child class of abstract class class SBI(Bank): def balance(self): print("Balance is 100") class Sub1(SBI): def interest(self): print("In sbi bank interest is 5 rupees") s= Sub1() s.bank_info () s.balance() s.interest()
Output:
Abstract class can also contain concrete methods
As discussed abstract class can contains implemented methods also along with abstract methods. In the above example, bank_info() method is a concrete method which has implementation along with name in the abstract class.
Program: Abstract class can also contain concrete methods (demo5.py)
from abc import ABC, abstractmethod #Abstract Class class Bank(ABC): def bank_info(self): print("Welcome to bank") @abstractmethod def interest(self): "Abstarct Method" pass def offers(self): print("Providing offers") #Sub class/ child class of abstract class class SBI(Bank): def interest(self): print("In SBI bank 5 rupees interest") s= SBI() s.bank_info () s.interest()
Output:
Can abstract classes contain more subclasses?
Yes, an abstract class can contain more than one subclass. If different child classes require different kinds of implementations, in that case an abstract class can contain more than one subclass.
Program: abstract classes contain more subclasses (demo6.py)
from abc import ABC, abstractmethod #Abstract Class class Bank(ABC): def bank_info(self): print("Welcome to bank") @abstractmethod def interest(self): "Abstarct Method" pass def offers(self): print("Providing offers") #Sub class/ child class of abstract class class SBI(Bank): def interest(self): print("In SBI bank 5 rupees interest") class HDFC(Bank): def interest(self): print("In HDFC 7 rupees interest") s= SBI() h=HDFC() s.bank_info() s.interest() h.bank_info() h.interest()
Output:
Program: abstract classes in python (demo7.py)
from abc import ABC, abstractmethod class One(ABC): @abstractmethod def calculate(self, a): pass def m1(self): print("implemented method") class Square(One): def calculate(self, a): print("square: ", (a*a)) class Cube(One): def calculate(self, a): print("cube: ", (a*a*a)) s = Square() c = Cube() s.calculate(2) c.calculate(2)
Output:
Different cases for Abstract class object creation
Case1: If any class is inheriting ABC class, and that class doesn’t contain an abstract method, then happily we can create an object to that class.
Program: Different cases for Abstract class object creation (demo8.py)
from abc import * class Demo(ABC): def m(self): print("calling") d=Demo() d.m()
Output:
Case2: If any class is inheriting ABC class, then that class will become an abstract class. If that class contains an abstract method, then the object creation is not possible for abstract class.
Program: Different cases for Abstract class object creation (demo9.py)
from abc import * class Demo(ABC): @abstractmethod def m(self): pass def n(self): print("Implemented method") t=Demo()
Output:
Case3: If any class is not inheriting ABC class, then we can create an object for that class even though it contains an abstract method. As per below example, class not inheriting ABC class, but it contains abstract methods.
Program: Different cases for Abstract class object creation (demo10.py)
from abc import * class Demo: @abstractmethod def m(self): pass def n(self): print("Implemented method") t=Demo() t.m() t.n()
Output:
In the next article, I am going to discuss Interfaces in Python. Here, in this article, I try to explain Abstract classes in Python with Examples. I hope you enjoy this Abstract classes in Python with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.