Menu Close

Python Abstract Class

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 but here we will discuss Python abstract method and an abstract class.

What is Python abstract class?

A class which contains one or more abstract method 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 created using abstract class.

Note:- A class is not a real abstract class if it contains an abstract method but not inherit from abc module.ABC module.

What is Python abstract method?

A Python abstract method is a method that has the only 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 @abstractmethod decorator which written in abc Python built-in module.

Why use abstract method?

The abstract class allows users to define functionality for the subclass. Sometimes we want to define some special method in 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 abstract class 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 defined any abstract method inside abstract class and don’t create those method inside 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 its mandatory to create abstract method inside 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 Python abstract class and Python abstract method in Python, you have to use abc module.

If you like this article, please share it and keep visiting for further Python tutorials.

Python OOPs

For More Information:- Click Here

Previous Article
Next Article

Was this post helpful?

Related Posts