0
Why is the answer to these 2?
What is the result of this code? class A: def method(self): print(1) class B(A): def method(self): print(2) B().method()
4 Answers
+ 1
it looks like class B is a subclass of class A and method overloading was used to make class B's output for the parent method 2 instead of 1.
+ 1
Why my answer got downvote? Maybe something is wrong? Please tell me so I can correct it, maybe I got wrong in some parts. Thanks
+ 1
2
- 1
Because the child class has also its "method" function or method. Hence, it will override the method of the Super or Parent class
- - - -Child class without the function "method" - - - -
class A:
def method(self):
print(1)
class B(A):
pass
B().method()
>> 1
#This code's output is 1 because the Child class don't have the function "method" thus the Parent Class' method is executed because no similar function overrides it.