Here’s the fix for can’t instantiate abstract class error. Also explained the reason for it.
A class that is derived from the ABC class belonging to the ABC module is called an abstract class. ABC class a.k.a metaclass. Precisely, it is a class that defines the behavior of other classes. So it can be said that the class which is defined from the ABC class becomes a metaclass.
When you supply ‘ABC’ to any class then, that class becomes an abstract class. The other condition is it should have an ‘abstract method’. This is a mandatory requirement. It can have both abstract methods and concrete methods. The abstract method means it has just a template. But the concrete method has a definition.
Rules for Abstract class
- The objects of an abstract class cannot be created by PVM (python virtual machine).
- It is not mandatory to declare all the methods as abstract in an abstract class.
- The class must be abstract if there is any abstract method in a class.
- Defining the abstract methods of an abstract class must be in its child class/subclass.
- If any abstract class is inherited which have an abstract method, then either implementation of the method or making this class abstract must be provided.
- An abstract class can have an abstract method and a concrete method.
Abstract class example threw an error
from abc import ABC,abstractmethod class one(ABC): @abstractmethod def move(self): pass obj_one=one()
When you run this code, it throws an error. The error is “TypeError: Can’t instantiate abstract class one with abstract methods move“
Reasons for error
The abstract method is not implemented (that should be implemented in the child class). That’s why we got this error. Here’s the code after fixing the error.
Code is fixed and the error ‘Can’t Instantiate Abstract Class’ is not displayed now.