Abstract Class and Method in Python

In this class, we discuss Abstract Class and Method in Python.

For Complete YouTube Video: Click Here

Abstract Class and method

The reader should have prior knowledge of decorators in python. Click here.

First, we take an example and understand when abstract classes and methods are needed.

Assume we have to develop an application for two universities. University1 and university2.

We go to both the universities and collect the functionalities required for the universities.

The functionalities required are given below.

F1) Employee details: The logic to maintain employee details is identical for both universities.

F2) Employee Tax Details: The logic to maintain employee tax details is identical for both universities.

F3) Student Details: The logic to maintain student details is identical for both universities.

F4) Student marks and grades: University1 is using percentages to calculate grades. And university2 is using GPA to calculate grades.

Both universities using different logic to calculate student grades.

The remaining all functionalities are identical for both the universities. So we can use the same code.

With the above example, we will understand how object-oriented concepts help in reducing the code.

Implementation:

Here we consider class university. In the class, we have four methods.

M1) Employee_details

M2) Employee_tax_details

M3) Student_details

M4) Student_grade_calculation

The code for the first three methods is written in the class university.

The name of the last method, student_grade_calculation, is written. The body is not written in the class university.

The last method, student_grade_calculation, is considered an abstract method.

Note:

The method student_grade_calculation is the required method in any university.

The situation where compulsory required methods and functionality are different. We define these methods as abstract methods.

Abstract Class: Class that consists of at least one abstract method we call abstract class.

We can not create objects for abstract class because the method functionality is not written in abstract methods.

To complete the application, we define two more classes.

Class university1(university). The university1 class is inheriting the university class.

The same way university2 class will inherit the university class.

Note: The class that inherits the abstract class. That should compulsorily implement the abstract method or should make it an abstract class.

Abstract class and method Implementation

from abc import *
class abtest(ABC):
    @abstractmethod
    def m1(self):
        pass
class abtest1(abtest):
    def m1(self):
        print("subclass test")
s=abtest1()
s.m1()

Output:
subclass test

In the above program, we defined a class abtest.

We defined a method m1 and made the method m1 abstract.

We used a decorator to impose the abstract condition on method m1.

@abstractmethod decorator is used. This decorator method is provided in class ABC.

ABC Class is provided in module abc.

We import the module, and the class abtest inherit the class ABC.

ABC full name Abstract Base Class.

We defined one more class abtest1. the class abtest1 is inheriting class abtest.

Class abtest1 should implement the method m1.

What happens if we do not implement the method m1 in abtest1 class?

The example is shown below.

from abc import *
class abtest(ABC):
    @abstractmethod
    def m1(self):
        pass
class abtest1(abtest):
    def m2(self):
        print("subclass test")
s=abtest1()
s.m2()

Output:
error
Can't instantiate abstract class abtest1 with abstract methods m1

Suppose a situation where the implementation of m1 should not be done in the class abtest.

We have to make the class abtest1 abstract.

The example is shown below.

from abc import *
class abtest(ABC):
    @abstractmethod
    def m1(self):
        pass
class abtest1(abtest):
    @abstractmethod
    def m2(self):
        print("subclass test")
class abtest2(abtest1):
    def m1(self):
        print("m1 in test2 class")
    def m2(self):
        print("m2 in test2 class")
s=abtest2()
s.m2()

Output:
m2 in test2 class

Class abtest2 should implement both the methods m1 and m2. in the above example.

What happens if we do not implement the method m1 and m2 in abtest2?

The example is shown below.

from abc import *
class abtest(ABC):
    @abstractmethod
    def m1(self):
        pass
class abtest1(abtest):
    @abstractmethod
    def m2(self):
        print("subclass test")
class abtest2(abtest1):
    def m1(self):
        print("m1 in test2 class")
s=abtest2()
s.m2()

Output:
error
Can't instantiate abstract class abtest2 with abstract methods m2

What happens if we do not inherit the class ABC?

The example is given below.

from abc import *
class abtest():
    def __init__(self,l):
        self.instvar=l
    @abstractmethod
    def m1(self):
        print("parent class")
        
class abtest1(abtest):
    def m2(self):
        print("subclass test")
s=abtest1(10)
s.m2()
print(s.instvar)

Output:
sub class test
10

The logic of the abstract method does not apply.