What is Abstract Base Class:
If Parent classes are concrete classes, then there are chances the it may get instantiated.
So if we don't want parent classes to be instantiated, then we need to make our parent class as Abstract class.
The abstract class will serve as template for the child class;
How to Create Abstract Base Class
Here abc is module and ABC is class.
We have derived our Employee Class from Abstract Base Class name ABC.
Below is the sample code construct to create the ABC
In this code construct, we cannot instantiate the employee class. We can only instantiate manager class.
When trying to instantiate employee class we get below error.
>>> o=employee()
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
o=employee()
TypeError: Can't instantiate abstract class employee with abstract methods m1
We are able to instantiate manager class.
>>> o=manager()
>>> o.m1()
I am m1 from manager
We can also call Parent class's abstract method from child class using employee.m1(self)
>>> o=manager()
In this case we still be able to instantiate employee class and also manager class.
This is as good as simple single inheritance concept.
1) Parent class derived from ABC and has abstractmethod in it.
In this case we cannot instantiate employee class.
>>> o=employee()
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
o=employee()
TypeError: Can't instantiate abstract class employee with abstract methods m1
2) Parent class derived from ABC and has abstractmethod in it. Child class dont have m1 implemented in it.
>>> o=manager()
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
o=manager()
TypeError: Can't instantiate abstract class manager with abstract methods m1
Here you cannot create object of manager.
This means that in the child class, if we don't implement the override method , then we cannot instantiate the child class.
If Parent classes are concrete classes, then there are chances the it may get instantiated.
So if we don't want parent classes to be instantiated, then we need to make our parent class as Abstract class.
The abstract class will serve as template for the child class;
How to Create Abstract Base Class
Here abc is module and ABC is class.
We have derived our Employee Class from Abstract Base Class name ABC.
Below is the sample code construct to create the ABC
In this code construct, we cannot instantiate the employee class. We can only instantiate manager class.
When trying to instantiate employee class we get below error.
>>> o=employee()
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
o=employee()
TypeError: Can't instantiate abstract class employee with abstract methods m1
We are able to instantiate manager class.
>>> o=manager()
>>> o.m1()
I am m1 from manager
We can also call Parent class's abstract method from child class using employee.m1(self)
>>> o=manager()
>>> o.m1()
I am M1 from employee
I am m1 from manager
Test Cases:
1) Parent Class is derived from ABC, but parent class doesn't have abstractmethod.
In this case we still be able to instantiate employee class and also manager class.
This is as good as simple single inheritance concept.
1) Parent class derived from ABC and has abstractmethod in it.
In this case we cannot instantiate employee class.
>>> o=employee()
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
o=employee()
TypeError: Can't instantiate abstract class employee with abstract methods m1
2) Parent class derived from ABC and has abstractmethod in it. Child class dont have m1 implemented in it.
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
o=manager()
TypeError: Can't instantiate abstract class manager with abstract methods m1
Here you cannot create object of manager.
This means that in the child class, if we don't implement the override method , then we cannot instantiate the child class.
No comments:
Post a Comment