Python Constructors Inheritance

In python, Constructor level inheritance also possible as same way as methods and variables of a parent class. Lets continue this article to examine this feature.

In the previous python programs, we have inherited the Student class from the Teacher class. All the methods and the variables in those methods of the Teacher class (base class) are accessible to the Student class (sub class). Are the constructors of the base class accessible to the sub class or not - is the next question we will answer.

Here, we are taking a super class by the name 'Father' and derived a sub class 'Son' from it. The Father class has a constructor where a variable 'property' is declared and initialized with 800000.00. When Son is created from Father, this constructor is by default available to Son class. When we call the method of the super class using sub class object, it will display the value of the 'property' variable.

Constructors Inheritance

The following is an example of python constructor inheritance:

CopiedCopy Code

#base class constructor is available to sub class 
class Father: 
   def __init__(self): 
      self.property = 800000.00 
   def display_property(self): 
      print('Father\'s property=', self.property) 
class Son(Father): 
   pass #we do not want to write anything in the sub class
   #create sub class instance and display father's property 
s = Son() 
s.display_property()

The conclusion : In Python along with variables and methods,constructors of the super class are also available or accessible to the sub class or child class objects by default.