Code Explanation:
Importing ABC and abstractmethod
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 Base Class (A)
class A(ABC):
@abstractmethod
def show(self):
pass
A is an abstract class because it inherits from ABC.
show(self) is defined as an abstract method using @abstractmethod.
Since show() has no implementation (pass), any subclass of A must override it before it can be instantiated.
Creating a Concrete Subclass (B)
class B(A):
def show(self):
print("Hello")
B is a subclass of A that implements the show() method.
Since B provides an implementation for show(), it can be instantiated.
Creating an Object and Calling show()
B().show()
B() creates an instance of class B.
.show() calls the show() method in B, which prints "Hello".
Since show() only prints but does not return anything, there is no None printed.
Final Output
Hello
0 Comments:
Post a Comment