Inheritance in Python

Inheritance in Python

We know that working in the IT sector is teamwork. You will probably have to start from where other developers have left. There could also be a good chance that the methods or functions you want to create are already available in another module developed by another programmer. In such cases, instead of creating a new method and increasing the load on memory, you could inherit the properties from another class and use its methods and attributes. This concept is called Inheritance in the world of programming.

Like other programming languages, Python also allows you to use the concept of Inheritance to have a parent class and let a child class use all its attributes and methods. Without further ado, let us dive into a program to get this concept into our heads.

#Python program to demonstrate Inheritance
#Declaring the parent class
class Human:
  def __init__(self, name):
    self.name = name
  #to return the name
  def getName(self):
    return self.name
  #check if this Human is a student
  def isStudent(self):
    return False

#child class
class Student(Human):
  #check if Human is a student, we return True here
  def isStudent(self):
    return True

stu1 = Human("Student 1") #Object of Human
print(stu1.getName(), stu1.isStudent())
stu2 = Student("Student 2") #Object of Employee
print(stu2.getName(),stu2.isStudent())
Output

Student 1 False
Student 2 True

In the program above, the class Human is the parent class, and the class Student is a child class. You can notice that the child class has inherited the properties of the parent class as we mentioned ‘class Student(Human)‘ in the program.

The behaviour of Constructors in Inheritance

Just like the methods and variables, the constructors in the parent class are also available to the child class objects.

#Program to show that the parent constructor
#will be available to the child class as well
class Corporation:
  def __init__(self):
    self.assets = 7000000
  
  def displayAssets(self):
    print("Corporation's assets = {}".format(self.assets))

class Office(Corporation):
  pass #let us not add anything here
  #this class is empty

ofc = Office()
ofc.displayAssets()
Output

Corporation's assets = 7000000

In the program mentioned above, you can see that we didn’t add any constructor or instance methods in the child class. However, the child class instance has still made use of the instance method and the constructor available in the parent class. This shows that the constructors are also extended to the child class by default.

How to override the constructors and methods from the parent class? 

When a developer defines a constructor in the child class, the constructor available in the parent class will not be available to the child class. In such cases, only the constructor available in the child class can be accessed by the child class object. The child class constructor making the parent class constructor unavailable is called constructor overriding.

Likewise, if we have a method in the child class precisely with the same name as a method from the parent class, the method from the parent class becomes unavailable to the object of the child class. This is called method overriding. Let us look at a program to understand this concept better. 

class Teacher:
  def __init__(self):
    self.qualification = "Graduate"
  
  def displayQualification(self):
    print("Teacher's Qualification is: {}".format(self.qualification))

class Student(Teacher):
  def __init__(self):
    self.qualification = "Student"
  
  def displayQualification(self):
    print("Child's qualification is: {}".format(self.qualification))
  
student1 = Student()
student1.displayQualification()
Output

Child's qualification is: Student

The Teacher is the parent class, and the Student is the child class in the above program. We have a constructor and a method with the same name in parent and child classes. But when we created an object or instance for the child class Student and called the method displayQualification(), it called only the method in the child class and not the method in the parent class. The same happened with the constructor method as well. The output displayed the qualification from the child class’s constructor and not the parent class. This concept is called overriding, and the child class methods have overridden both the constructor and instance methods of the parent class.

How can we overcome this situation? What if we wanted to call the parent class’s constructor as well? There’s a way in Python to do that. We use the super() method to achieve this.

The super() method in Python

In Python, we have an inbuilt method called super(), which calls the methods or constructors from the parent class. The program mentioned above shows that the child class overrides the constructors and methods from the parent class with the same name. However, using the super() method, we can refer to the constructor or methods from the parent class as well. Let us find out how it can be achieved using the super() method in the below program.

#Python program to show super() method
class Grandpa:
  def __init__(self, assets=0):
    self.assets = assets
  def displayAssets(self):
    print("Grandpa's property: {}".format(self.assets))

class Father(Grandpa):
  def __init__(self, assets1=0, assets=0):
    super().__init__(assets)
    self.assets1 = assets1
  def displayAssets(self):
    print("Total property of Father: {}".format(self.assets1+self.assets))

#create an instance for Father and display property of Father
f = Father(15000,35000)
f.displayAssets()
Output

Total property of Father: 50000

You can notice in the program above that we used the super() method in the constructor of the Father class (which is the child class in this program) to call the parent class constructor. Please find below the format that needs to be followed to use the super() method in different scenarios.

super().__init__() #calling constructor of parent class
super().__init__(parameters) #calling constructor of parent class and passing an parameter
super().method() #calling parent class method
Scroll to Top