Code Explanation:
1. Defining Class A
class A:
Creates a base class named A
Inherits from object by default
๐น 2. Defining Constructor of A
def __init__(self):
print("A")
__init__ is the constructor
Runs when an object of A is created
Prints "A"
๐ Important:
This constructor runs only if it is explicitly called.
๐น 3. Defining Class B (Inheritance)
class B(A):
B inherits from class A
So B has access to methods of A
But inheritance does not automatically call constructors
๐น 4. Defining Constructor of B
def __init__(self):
print("B")
B overrides the constructor of A
This constructor replaces A.__init__ for objects of B
Prints "B"
๐น 5. Creating an Object of B
B()
What happens internally:
Python creates an object of class B
Looks for __init__ in B
Finds B.__init__
Executes it
Prints "B"
Stops (does NOT call A.__init__)
๐ A.__init__ is never called here.
✅ Final Output
B

0 Comments:
Post a Comment