Code Explanation:
1️⃣ Defining Class A
class A:
Explanation
A base class A is created.
It contains a method show().
2️⃣ Method in Class A
def show(self, x=1):
Explanation
Method show takes a parameter x.
Default value of x is 1.
3️⃣ Printing Value
print(x)
Explanation
Prints the value of x.
4️⃣ Defining Class B (Inheritance)
class B(A):
Explanation
Class B inherits from class A.
It can use and override methods of A.
5️⃣ Overriding Method in Class B
def show(self, x=2):
Explanation
show() is overridden in class B.
Default value of x is now 2.
6️⃣ Calling Parent Method Using super()
super().show(x)
Explanation
Calls the show() method of class A.
Passes value of x from class B.
7️⃣ Creating Object
b = B()
Explanation
Creates an instance b of class B.
8️⃣ Calling Method
b.show()
Explanation
Calls show() from class B.
Since no argument is passed:
x = 2 (default of class B)
Then:
super().show(2)
9️⃣ Execution in Class A
Parent method receives:
x = 2
Prints:
2
๐ค Final Output
2

0 Comments:
Post a Comment