No. Answer is D. B and C are subclass of A. Calling self.b in any of these class is a cas of polymorphism and the object on which a() will be applied is self from B and C respectively. So the output is BC.
class A:
def a (self):
print("A",end=' ')
def b (self):
self.a()
class B(A):
def a (self):
print("B",end=' ')
def do (self):
self.b()
class C(A):
def a (self):
print("C",end=' ')
def do (self):
self.b()
B().do()
C().do()
Output -> B C
Ans is D
I really highly reccommend anyone confused by this to step through the code in a debugger. You have to remember that the self variables are refrences to the actual objects( like obj1 = B() and obj2 = C()).
When you try to access any object's entity, Python will try to (in this order):
find it inside the object itself;
find it in all classes involved in the object's inheritance line from bottom to top;
This applies everytime we get sent to another class looking for the function. It will always check if its in the original object, that is calling the function originally, first(because it is refrencing the object by using the self keyword), before moving onto the class it is inherited from.
If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden.
In other words
A method of a parent class gets overridden by simply defining a method with the same name in the child class.
If a method is overridden in a class, the original method can still be accessed, but we have to do it by calling the method directly with the class name.
A constructor is always implicitly called. In case is missing definition of the constructor inside the class, a default constructor is invoked (which need not be defined).
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
BenKa
Highly Voted 4 years, 3 months agoFR99
Highly Voted 4 years, 1 month agoseaverick
Most Recent 10 months agoTheFivePips
11 months, 3 weeks agoValcon_doo_NoviSad
1 year, 1 month agomacxsz
2 years, 7 months agorocky48
2 years, 8 months agoBacky
2 years, 9 months agoTestPyth
2 years, 10 months agoNiteshSingh
3 years, 1 month agosadako11
3 years, 11 months ago[Removed]
4 years, 3 months agoAshitAdhikari
3 years, 4 months ago[Removed]
4 years, 3 months agowi11
3 years, 7 months ago