Python Inheritance

In python, inheritance is the main concept of Object-Oriented Programming (OOP), and it is useful to inherit all the properties and methods from one class (parent/base class) to another class (child/subclass).

 

The inheritance in python will enable us to extend the behavior of a particular class by inheriting all the properties and methods from other classes based on our requirements.

 

In python inheritance process, the class that is being inherited is called a base class or parent class, and the class that inherits from another class is called a child class or derived class.

Python Inheritance Syntax

To implement the inheritance process in python, you need to define the child class with parentheses ( ) by including the base class.

 

Following is the syntax of implementing the inheritance in python.

 

class Base_Class:
   #Base/Parent Class Statements

class Child_Class(Base_Class):
   #Child/Derived Class Statements

If you observe the above syntax, we defined a new child/derived class (Child_Class) by inheriting the properties from the base/parent class (Base_Class). The child class will have all the base class features and the required new properties and methods.

 

In python, the main feature of inheritance is code reusability. The inheritance process will help us to reuse the same base class features in different classes without repeating the same code. 

Python Inheritance Example

Following is the example of implementing the inheritance in python by defining the child class that inherits properties from its parent class.

 

#Base/Parent Class
class user:
   def __init__(self, uname, uloc):
     print("Base Class Constructor")
     self.name = uname
     self.location = uloc

   def userdetails(self):
     print("Name: {}, Location: {}".format(self.name, self.location))

#Child/Derived Class
class Employees(user):
   pass

e1 = Employees("Suresh", "Hyderabad")
e1.userdetails()

If you observe the above python example, we created a base class (user) with constructor (__init__) and userdetails() method. After that, we created an empty child class (Employee) by inheriting the base class (user) and accessing all the base class (user) properties and methods with an instance of child class (Employee).

 

As discussed in python classes and objects, the self keyword will refer to the current instance of a class, and the pass statement helps create an empty class.

 

The above python inheritance example will return the result as shown below.

 

Base Class Constructor
Name: Suresh, Location: Hyderabad

If you observe the above result, the base class (user) constructor (__init__()) has been executed automatically whenever an instance is created for the child class (Employee).

Python Constructor in Child Class

In the above example, we created a constructor using the __init__() function in a base class. Suppose if you create a constructor (__init__) in the child class that will not inherit the parent class constructor.

 

Following is the example of creating the constructor in the child/derived class in python.

 

#Base/Parent Class
class user:
   name = "Suresh"
   location = "Hyderabad"
   def __init__(self, uname, uloc):
     print("Base Class Constructor")
     self.name = uname
     self.location = uloc

   def userdetails(self):
     print("Name: {}, Location: {}".format(self.name, self.location))

#Child/Derived Class
class Employees(user):
   def __init__ (self, uname, uloc):
     print("Child Class Constructor")

e1 = Employees("Rohini", "Guntur")
e1.userdetails()

The above python inheritance example will return the result as shown below.

 

Child Class Constructor
Name: Suresh, Location: Hyderabad

If you observe the above result, the child/derived class constructor overrides the parent class constructor and invokes only the child class constructor.

 

If you want to invoke/inherit parent/base class constructor (__init__), you need to call the base class constructor (__init__) in the child class constructor (__init__) as shown below.

 

#Base/Parent Class
class user:
   name = "Suresh"
   location = "Hyderabad"
   def __init__(self, uname, uloc):
     print("Base Class Constructor")
     self.name = uname
     self.location = uloc

   def userdetails(self):
     print("Name: {}, Location: {}".format(self.name, self.location))

#Child/Derived Class
class Employees(user):
   def __init__ (self, uname, uloc):
     print("Child Class Constructor")
     user.__init__(self, uname, uloc)

e1 = Employees("Rohini", "Guntur")
e1.userdetails()

If you observe the above example, we call the parent class (user) constructor (__init__) inside the child class constructor. 

The above python inheritance example will return the result as shown below.

 

Child Class Constructor
Base Class Constructor
Name: Rohini, Location: Guntur

If you observe the above result, both parent and child class constructors have been invoked based on our requirements.

Super() Function in Python

In python, the super() function is useful to inherit all the base class properties and methods in the child class.

 

In the above example, we used the parent class name to inherit the base class constructor (__init__). Instead, if you use the super() function automatically, it will inherit all the parent class properties and methods.

 

Following is the example of using the super() function in python.

 

#Base/Parent Class
class user:
   name = "Suresh"
   location = "Hyderabad"
   def __init__(self, uname, uloc):
     print("Base Class Constructor")
     self.name = uname
     self.location = uloc

   def userdetails(self):
     print("Name: {}, Location: {}".format(self.name, self.location))

#Child/Derived Class
class Employees(user):
   def __init__ (self, uname, uloc):
     print("Child Class Constructor")
     super().__init__(uname, uloc)

e1 = Employees("Rohini", "Guntur")
e1.userdetails()

If you observe the above example, instead of the parent class name, we used the super() function in the child class to inherit all the methods and properties from the parent class.

 

The above python super() function example will return the result as shown below.

 

Child Class Constructor
Base Class Constructor
Name: Rohini, Location: Guntur

Overriding in Python

In the above examples, we inherited the parent/base class methods and used them in child classes. If required, you can also modify the functionality of base class methods in child class by creating the methods with the same name and signature to perform different tasks.

 

In python, if you override a base class method in child class by creating the method with same name and signature, we will call it method overriding.

 

Following is an example of implementing the method overriding in python.

 

#Base/Parent Class
class user:

   def getinfo(self):
     print("Learn Python")

#Child/Derived Class
class Employees(user):

   def getinfo(self):
     print("Welcome to Tutlane")

e1 = Employees()
e1.getinfo()

The above python method overriding example will return the result as shown below.

 

Welcome to Tutlane

In python, you can also implement multiple inheritance and multi-level inheritance based on your requirements. In the next chapters, you will learn more about it.