Abstract Class and Methods in Python😊
ABSTRACT CLASS
Abstract classes in Python are empty or Partially implemented classes .
An abstract class can be considered as a blueprint for other classes. It allows you to create a set of methods that must be created within any child classes built from the abstract class.
A class which contains one or more abstract methods is called an abstract class.
An abstract method is a method that has a declaration but does not have an implementation.
While we are designing large functional units we use an abstract class. When we want to provide a common interface for different implementations of a component, we use an abstract class.
Note: Every abstract class in python should be a child of ABC class, which is present in the abc module.
#abstract class concept
from abc import *
class Student(ABC):
pass
stud1 = Student()
When an incomplete method(Abstract Method) is written in above code and when
we try to create an instance of the class Student.We may face some errors.
from abc import *
class Student(ABC):
@abstractmethod
def m1(self):
pass
stud1 = Student()
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-28-57b67a37b54d> in <module>() 5 pass 6 ----> 7 stud1 = Student()
TypeError:
Can't instantiate abstract class Student with
abstract methods m1
These classes are called concrete classes. We cannot create an object to the abstract class, but we can create an object to the child's class.
To avoid the above error we need to define abstract method of parent class in child class and instantiate object for child class.
from abc import *
class Student(ABC):
@abstractmethod
def m1(self):
pass
class student1(Student):
def m1(self):
print("Student from A Section")
stud1 = student1()
stud1.m1()
Output
Student from A Section
ABSTRACT METHODS
An Abstract method is a method which is declared but does not have implementation such type of methods are called as abstract methods. In Python, we can declare an abstract method by using @abstractmethod decorator. This abstract method is present in the abc module in python, and hence, while declaring the abstract method, importing the abc module is compulsory.
The child classes are responsible to provide implementation to parent class abstract methods.
from abc import *
class Nwinterface(ABC):
@abstractmethod
def connect(self):
pass
@abstractmethod
def disconnect(self):
pass
class Videocard(Nwinterface):
def connect(self):
print('connecting videocard')
def disconnect(self):
print('disconnecting videocard')
class TPU(Nwinterface):
def connect(self):
print('connecting TPU')
def disconnect(self):
print('disconnecting TPU')
device = input()
Enter the deviceVideocard
classname = globals()[device] #globals()[str]
a = classname()
a.connect()
a.disconnect()
>>connecting videocard
disconnecting videocard
In the above code globals()[str] inbuilt method is used. It is used to convert any input
string data into an existing class name and operate over it.
string data into an existing class name and operate over it.
SUMMARY:
Key Points
1. If a class containing one abstract method and if we are extending ABC class then instantiation is not possible; for Abstract class with an abstract method instantiation(creating an object) is not possible.
2. If we are creating a child class for abstract class, then for every abstract method of parent class compulsory we should provide an implementation in the child class; otherwise child class also becomes an abstract class, and we cannot create an object for the child class.
Comments
Post a Comment