Code Explanation:
Defining Class A
class A:
This creates a class named A.
It will act as a base (parent) class.
Method Inside Class A
def get(self):
return "A"
Defines a method get inside class A.
When get() is called on an object of class A, it will return the string "A".
Defining Class B That Inherits from A
class B(A):
Creates a new class B that inherits from A.
That means B automatically gets all methods and attributes of A (unless overridden).
Overriding get in Class B
def get(self):
return "B"
Class B defines its own version of the get method.
This overrides the get method from class A.
Now, when get() is called on a B object, it returns "B" instead of "A".
Defining Class C That Inherits from B
class C(B):
pass
Class C is defined and it inherits from B.
The pass statement means no new methods or attributes are added in C.
So C simply uses whatever it gets from B (and indirectly from A).
Creating an Object of C and Calling get
print(C().get())
C() creates a new object of class C.
.get() calls the get method on that object.
Python looks for get method in this order (MRO):
First in C → none
Then in B → found def get(self): return "B"
So it uses B’s version of get, which returns "B".
Final printed output:
B
Final Answer:
B


0 Comments:
Post a Comment