Understanding the benefits of super() overriding __init__
What functionality are we missing within the subclass that we get using the super() override of
Chris FreemanTreehouse Moderator 59,892 Points
Good question. But there might be some misunderstanding in how you phrased it. The function
super() does not "override" the
__init__(). Rather, it executes the
__init__() of the parent class within the current subclass execution of
super() call is not included in the
subclass.__init__() method, the
subclass.__init__() completely overrides the
parent.__init__() method preventing the parent's
__init__() from executing. This can be a legitimate useage provide, there is nothing needed from the parent's initialization code.
It some cases the subclass does not need to modify the actions of the
parent.__init__() at all. In this case, the subclass does not include an
__init__() method. This will cause the parent's
__init__() method to be called automatically.
Once the method
__init__() is added to the subclass, the question becomes "Is there anything needed in the parent's
__init__() method?" If yes, the a
super() call is needed. The next question becomes "Does the subclass need to initial items before or after (or both) the parent's
__init__() method runs?" If subclass needs to initialize items before the parent,
super() is called last. If subclass needs to initialize items after the parent,
super() is called first. If both,
super() is called in the middle of the
subclass.__init__() method code.
If you need more help, please post back. Good luck!
This is very good explaining