In this article, you will learn about Python abstract class and Python abstract method along with examples. In the previous topic, we have seen the Python class method.
Headings of Contents
What is Python abstract class?
A class that contains one or more abstract methods is called a Python abstract class. An abstract class in Python is a blueprint for another class. Abstract class in Python allows us to create a set of methods that must be created by any child class using an abstract class.
Note:- A class is not a real abstract class even if it contains abstract methods but is not inherited from the ABC module.
What is the Python abstract method?
A Python abstract method is a method that has only a declaration but not any implementation.
Python does not provide any built-in method for abstract methods and classes. If we want to use the abstract method and abstract class in our program then we need to use abc module.
To create an abstract method we will use the @abstractmethod decorator which is written in the abc Python built-in module.
Why use the abstract method?
The abstract class allows users to define functionality for the subclass. Sometimes we want to define some special method in the subclass then we can declare abstract methods in the abstract class and implement those methods in the subclass.
If we define any abstract methods in an abstract class then it’s mandatory to create an inside subclass or child class built from abstract methods.
Example:
from abc import ABC, abstractmethod
class Person(ABC):
@abstractmethod
def printDetails(self):
pass
class Myname(Person):
def printDetails(self):
print(f"My name is Vishvajit Rao and I am a Programmer.")
class Computer(Person):
def printDetails(self):
print(f"My name is Lenovo")
obj1 = Myname()
obj1.printDetails()
obj2 = Computer()
obj2.printDetails()
Output
My name is Vishvajit Rao and I am a Programmer.
My name is Lenovo
Python Abstract class Object
You have to always remember you can’t create an object of an abstract class because an abstract class is incomplete because it has only methods not implementation.
If you try to create an object of abstract class then you will get an error.
Example:
from abc import ABC, abstractmethod
class Person(ABC):
@abstractmethod
def printDetails(self):
pass
class Vishvajit(Person):
def printDetails(self):
print(f"My name is Vishvajit Rao and i am a Programmer")
obj = Person()
obj.printDetails()
Output
Traceback (most recent call last):
File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py", line 12, in <module>
obj = Person()
TypeError: Can't instantiate abstract class Person with abstract method printDetails
If you define any abstract method inside the abstract class and don’t create that method inside the subclass, then you will also get an error.
Example:
from abc import ABC, abstractmethod
class Person(ABC):
@abstractmethod
def printDetails(self):
pass
class Vishvajit(Person):
def printName(self):
print(f"My name is Vishvajit Rao and i am a Programmer")
obj = Person()
obj.printName()
Output
Traceback (most recent call last):
File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py", line 13, in <module>
obj = Person()
TypeError: Can't instantiate abstract class Person with abstract method printDetails
So it’s mandatory to create an abstract method inside a subclass or child class.
Conclusion
In this tutorial, You have learned the Python abstract method and Python abstract method along with examples. Python does not provide any built-in method to create a Python abstract class and an abstract method in Python.
To create a Python abstract class and Python abstract method in Python, you have to use the abc module.
If you like this article, please share it and keep visiting for further Python tutorials.
Python OOPs
For More Information:- Click Here