Inheritance in Python

Inheritance in Python

13 mins read5.3K Views Comment
Updated on Nov 18, 2022 17:32 IST

Inheritance is a fundamental facet of object-oriented programming languages, including Python. This article will explore the concept of inheritance in Python in detail.



What is Inheritance in Python?

Inheritance in Python is referred to as the ability where the child class (derived class) inherits or acquires all the properties and behaviors from the parent class (base class).

In object-oriented programming, inheritance is largely used to improve code reuse, by that I mean the new class can automatically use all the attributes and methods of the old class without having to copy the code again. As a result, you can declare what is unique to your new class, which will override the old class’s behaviour. The old class is called superclass, parent, or base class and the class that inherits from a parent (or super) class is called a subclass, child, or derived class.

Let’s decipher this with the help of an example. You might have heard of the popular Spanish thriller series La Casa de Papel (aka Money Heist). Well, the series showcases a professor and his group of robbers as they indulge in nerve-wracking heists and take hostages along the way with their signature red jumpsuits and Dalí masks. It’s hard to imagine a thriller without its main characters. These main characters can be categorized like you have a professor, a gang of robbers, then there are law enforcement officers, politicians, etc. Now, creating a separate class for each of these individuals is pointless.


Instead, you can have a superclass called MoneyHeist_Charecters and use this as a parent class for the Robber class, Law Enforcement Officers class. The parent class can have common properties and behaviors shared by all the child classes. In addition, each child class can have its own properties and classes.

What is Programming What is Python
What is Data Science What is Machine Learning

When to Use Inheritance?

Although the definition of inheritance is very simple, it can become a complicated, tangled mess when used unwisely. The most common and beneficial use of inheritance is when you want to perform differential programming. That is when you want to reuse the entire interface and implementation from the superclass, but with a few tweaks and enhancements.

Well, I guess that’s enough theory! Let’s try to grasp the concept by actually implementing it. Let’s suppose that we’re creating an inventory application for a pet sanctuary. Here, we can name different kinds of objects such as parrots, cats, dogs, hamsters, rabbits, etc. Now, does it make to write code for each cat, each dog, and so on?

Programming Online Courses and Certification Python Online Courses and Certifications
Data Science Online Courses and Certifications Machine Learning Online Courses and Certifications

Types of Inheritance in Python

In Python, we have different types of inheritance that we can implement in different situations. Let’s discuss each of these in detail with help of examples.

Single Inheritance in Python

Single inheritance is the simplest of inheritance models. In this type, a child class inherits from a single parent class as shown below.

  1. #A program to demonstrate single inheritance
  3. # Base or parent class
  4. # Same as Pet(object)
  5. class Pet:
  7. #constructor
  8. def __init__ ( self , pet_type , name , bdate ):
  9. self. pet_type = pet_type
  10. self. name = name
  11. self. bdate = bdate
  13. #method to print details
  14. def details ( self ):
  15. print ( "I am pet" )
  17. # Derived or child class - inherits base class Pet
  18. class Cat (Pet ):
  20. #constructor
  21. def __init__ ( self , pet_type , name , bdate ):
  22. self. name = "Grey " + name
  23. self. pet_type = pet_type
  25. def sounds ( self , sound ):
  26. return sound
  28. pet1 = Pet ( 'cat' , 'Tiffany' , '2019-07-08' )
  29. pet2 = Cat ( 'cat' , 'Gatsby' , '2018-07-08' )
  30. pet1. details ( ) #calls parent class version of details()
  31. pet2. details ( ) #calls parent class version of details()
  32. print (pet2. name , "is a" ,pet2. pet_type , "and it always runs around with" ,pet2. sounds ( "attention-seeking meows" ) )

To summarize this example:

  • There is a subclass called Cat that inherits the attributes and methods of the base class Pet.
  • In addition, the class Cat also has its own method, sounds
  • We have used special Python object initialization method __init__()

Okay, I see that there are a few new terms like __init__, self, method overriding, etc. Let’s explore them in detail.

__init__ method

The line pet1 = Pet(‘cat’, ‘Tiffany’, ‘2019-07-08’) from the previous example creates a new instance of Pet class, then initializes its data by calling the class’s __init__ method. The  __init__  function runs automatically when the class is called. Every class that you create can provide an __init__ method that specifies how to initialize an object’s attributes.

  1. #constructor
  2. def __init__ ( self , pet_type , name , bdate ):
  3. self. pet_type = pet_type
  4. self. name = name
  5. self. bdate = bdate

You can actually write your own version of __init__() method, i.e. override it.

‘self’ in Python

Most often the very first argument of a class method is self. The argument self just points to the instance of our class that we’re currently working with. From Python’s perspective it’s just a convention, I mean the name self has absolutely no special meaning. It simply binds the arguments passed with the object’s attributes.

Method Overriding

So far, we have looked at the class Cat which inherited all the attributes and behaviors of parent class Pet. In addition, the class Cat also created its own unique method, that is specific to the derived class. However, the child class can create its own version of the inherited attributes and methods by a technique called overriding. We call this method overriding.

Let’s try to understand that with the help of an example.

  1. #A python program demonstrating method overriding
  3. # Base or parent class
  4. # Same as Pet(object)
  5. class Pet:
  7. #constructor
  8. def __init__ ( self , pet_type , name , bdate ):
  9. self. pet_type = pet_type
  10. self. name = name
  11. self. bdate = bdate
  13. #method to print details
  14. def details ( self ):
  15. print ( "I am pet" )
  17. class Dog (Pet ):
  18. def __init__ ( self , pet_type , name , bdate , breed ):
  19. super ( ). __init__ (pet_type , name , bdate )
  20. self. breed = breed
  22. def sounds ( self , sound ):
  23. return sound
  25. def details ( self ):
  26. print ( 'I am' , self. name , ',a' , self. breed )
  28. pet1 = Pet ( 'Dog' , 'Tiffiny' , '2019-07-08' )
  29. pet2 = Dog ( 'Dog' , 'Toby' , '2018-07-08' , 'bull dog' )
  30. pet3 = Dog ( 'Dog' , 'Max' , '2018-07-08' , 'Tibetan Mastiff' )
  32. pet1. details ( ) #invokes the parent version of details()
  33. pet2. details ( ) #invokes class Dog version of details()
  34. print (pet3. name , "is a" ,pet3. breed , ", plays well with other dogs, & never" ,pet3. sounds ( "growls" ) , "at kids" )

The Dog class overrides a parent method Details and creates its own version. The line pet2 = Dog(‘Dog’, ‘Toby’, ‘2018-07-08’, ‘bull dog’), invokes the class Dog version of details().

The super() method

You can override any method of the parent class, including __init__(). In the above example, notice that __init__ method is defined in both classes. When this happens, the method in the child class overrides the method in the parent class. Basically, by overriding, we are trying to extend the definition of the class rather than simply replacing it.

In simple terms, you can still use the parent class version of the method and append to it the additional details from the child class. This can be achieved using built-in-function super(). By using the function super(), we are basically bringing a parent method into the child method to make use of it. The super() in the below example gets the definition of Pet, the parent class.

  1. #Python code snippet demonstrating super() method
  2. class Dog (Pet ):
  3. def __init__ ( self , pet_type , name , bdate , breed ):
  4. super ( ). __init__ (pet_type , name , bdate )
  5. self. breed = breed

Now that you are clear on these important topics related to inheritance, let’s go back to discussing different types of inheritance.

Multiple Inheritance in Python

In multiple inheritances, the derived (child class) class inherits from more than one base (parent class) class. Here’s another interesting multiple inheritances in Python example.

  1. #Python program demonstrating multiple inheritance
  3. #Base Class 1
  4. class Pets:
  5. def domesticated ( self ):
  6. return "Legalized Pet"
  8. #Base Class 2
  9. class WildAnimals:
  10. def tamed ( self ):
  11. return "Is a wild animal"
  13. #Derived Class (Child class)
  14. class exotic_pets (WildAnimals , Pets ):
  15. pass
  17. Tiger = exotic_pets ( )
  18. print (Tiger. tamed ( ) , "and" , Tiger. domesticated ( ) )

Multilevel Inheritance in Python

In a multilevel inheritance model a derived class inherits another derived class.

  1. #Python program demonstrating multilevel inheritance
  3. #Base Class
  4. class Pet:
  6. #constructor
  7. def __init__ ( self , name ):
  8. self. name = name
  10. #method to print name
  11. def getName ( self ):
  12. return self. name
  14. #Derived Class
  15. class Mammal (Pet ):
  17. def warm_blooded ( self ):
  18. return True
  20. #Derived Class
  21. class Cat (Mammal ):
  23. #constructor
  24. def __init__ ( self , name , pet_type , breed ):
  25. Mammal. __init__ ( self , name )
  26. self. pet_type = pet_type
  27. self. breed = breed
  29. def getDetails ( self ):
  30. return "is a " + self. breed + " " + self. pet_type
  32. def sounds ( self , sound ):
  33. return sound
  35. pet3 = Cat ( 'Max' , 'Cat' , 'Siberian' )
  36. print (pet3. getName ( ) , pet3. getDetails ( ) )
  37. print ( "Is" , pet3. getName ( ) , "warm blooded?" , pet3. warm_blooded ( ) )
Top 110+ Python Interview Questions and Answers
Top 110+ Python Interview Questions and Answers
Python is a widely-used general-purpose, object-oriented, high-level programming language. It is used to create web applications, and develop websites and GUI applications. The popularity of the language is due more
Pandas Interview Questions for Data Scientists
Top 9 NumPy Interview Questions
Top 9 NumPy Interview Questions
In this article, we will discuss basic and intermediate NumPy Interview Questions in most simplified ways with example.

Hierarchical Inheritance in Python

In Python, if more than one derived class is created from a single base class, we call that hierarchical inheritance. Here’s a simple example:

  1. #Python Program demonstrating hierarchical inheritance
  3. #Base Class
  4. class Pet:
  5. def __init__ ( self , pet_type , name , bdate ):
  6. self. pet_type = pet_type
  7. self. name = name
  8. self. bdate = bdate
  10. def details ( self ):
  11. print ( "I am pet" )
  13. #Derived Class 1
  14. class Cat (Pet ):
  15. def __init__ ( self , pet_type , name , bdate ):
  16. self. name = "Grey " + name
  17. self. pet_type = pet_type
  19. def details ( self ):
  20. print ( 'I am cute pet' , self. pet_type , 'people call me' , self. name )
  22. #Derived Class 2
  23. class Dog (Pet ):
  24. def __init__ ( self , pet_type , name , bdate , breed ):
  25. super ( ). __init__ (pet_type , name , bdate )
  26. self. breed = breed
  28. def sounds ( self , sound ):
  29. return sound
  31. def details ( self ):
  32. print ( 'I am' , self. name , ',a' , self. breed )
  34. pet1 = Pet ( 'cat' , 'Tiffiny' , '2019-07-08' )
  35. pet2 = Cat ( 'cat' , 'Gatsby' , '2018-07-08' )
  36. pet3 = Dog ( 'dog' , 'Toby' , '2018-07-08' , 'bull dog' )
  37. pet4 = Dog ( 'dog' , 'Max' , '2018-07-08' , 'Tibetan Mastiff' )
  39. print (pet1. name )
  40. print (pet2. name , "is a chubby" , pet2. pet_type )
  41. pet2. details ( )
  42. print (pet3. name , "is a" ,pet3. breed , "and it always" ,pet3. sounds ( "growls" ) )
  43. pet4. details ( )

The concept details that we can learn from this example are:

  • There are have 2 subclasses Cat and Dog that inherit the attributes and methods of the base class Pet. In addition they also have their own attributes and methods
  • A child class is simply a specialization of parent class
  • Both Cat and Dog override a parent method Details and create their own version. You can override any method, including __init__(). This is called method overriding
  • The class Dog has a method called sounds, which is not present in its parent class Pet
  • Now you know how to add or override a parent class method. You have two versions of same method. How to call the parent version of that method? In such scenario fucntion super() comes to your aid
    • The super() brings in the definition of parent class Pet into child class
    • The __init__() method automatically takes care of passing the argument self to parent class. All you need is to give optional arguments if there are any
  • In the class Dog, the line self.breed = breed and the instance method sounds is the new code that makes class Dog different from a Pet

Hybrid Inheritance in Python

When inheritance consists of multiple combinations of different inheritance models that we discussed till now, we call that hybrid inheritance.

  1. #Python program demonstrating Hybrid inheritance
  3. #Base Class 1
  4. class Pets:
  5. def pets_info ( self ):
  6. print ( "Legalized Pet" )
  8. class Cat (Pets ):
  9. def cat_info ( self ):
  10. print ( "I am cat" )
  12. class Dog (Pets ):
  13. def dog_info ( self ):
  14. print ( "I am a fiercely loyal dog" )
  16. # Wild_Cat inherits properties of Pets and Cat
  17. class Wild_Cat (Cat , Pets ):
  18. def wildcat_info ( self ):
  19. print ( "A mighty wild cat" )
  21. # create object
  22. w_cat = Wild_Cat ( )
  24. w_cat. pets_info ( )
  25. w_cat. cat_info ( )
  26. w_cat. wildcat_info ( )

Other Special In-built Functions to Check Inheritances  

issubclass( ) 

This function is used to check if a class is derived from a particular base class. If the classes share a parent-child relationship, it returns a Boolean value of True, else it returns False

You can also explore- How to Find the Factorial of a Number Using Python

isinstance( ) 

This function is used to check if an object is an instance of a particular class or any of the classes it has been derived from. This function takes two arguments – the object, and the class we are checking it against. If the object is an instance, it returns a Boolean value of True, else it returns False

You can also explore- Variables in Python

Consider the below example to understand both the predefined functions: 

class parent: 
  def display(self): 
    print("Parent Class Method")class child(parent):  
  def display(self):    
    print("Child Class Method")  
#Check if child is derived from parent 
#Check if parent is derived from child  
X = child()                         
Y = parent()#Check if object X is instance of child  
#Check if object X is instance of parent 
#Check if object Y is instance of child 
#Check if object Y is instance of parent 




That’s it guys! Hope you found this interesting. To summarize, inheritance supports code reusability, by allowing you to create a generic parent class and a more unique child class, which automatically gets access to functionalities of the parent class once it inherits the parent class.

Top Trending Article

Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python

Interview Questions

Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio