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 (X)
class X(ABC):
@abstractmethod
def display(self):
pass
X is an abstract class because it inherits from ABC.
display(self) is marked as an abstract method using @abstractmethod.
Since display() has no implementation (pass), any subclass of X must override it to be instantiated.
Creating a Subclass (Y)
class Y(X):
pass
Y is a subclass of X, but it does not implement the display() method.
Because display() is still missing, Y remains abstract and cannot be instantiated.
Attempting to Instantiate Y
obj = Y()
Since Y does not provide an implementation for display(), it remains an abstract class.
Python does not allow instantiating an abstract class, so this line raises a TypeError.
Final Output:
TypeError
0 Comments:
Post a Comment