Code Explanation:
Importing Required Modules
from abc import ABC, abstractmethod
ABC (Abstract Base Class) is imported from the abc module.
@abstractmethod is used to define an abstract method.
Defining an Abstract Class (A)
class A(ABC):
@abstractmethod
def show(self):
pass
A is an abstract class because it inherits from ABC.
show(self) is an abstract method, meaning any subclass must override it before being instantiated.
Since show() has no implementation (pass), A cannot be instantiated directly.
Creating a Subclass (B)
class B(A):
pass
B is a subclass of A, but it does not implement the show() method.
Because show() is still missing, B remains an abstract class.
Python does not allow instantiating an abstract class, so obj = B() causes an error.
Attempting to Instantiate B
obj = B()
Final Output:
Implement
show()
in B
0 Comments:
Post a Comment