Skip to content
Advertisement

How to let the constructor in the subclass inherit the constructor or base class?

I have the following code segment, which is shown as follows along with its output

JavaScript

I would like to let the subclass, e.g. Employee, can also inherit the functionality implemented in the constructor of AbstractClass. In specific, the print('the salary information') I added:

JavaScript

Advertisement

Answer

You need to pass the CURRENT class to super, not the parent. So:

JavaScript

Python will figure out which parent class to call.

When you do that, it won’t work, because AbstractClass.__init__ requires an additional parameter that you aren’t providing.

FOLLOWUP

It has been correctly pointed out that this will suffice:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement