Previously, we discussed about objects and classes in Python and how you can implement them. In this article, we’ll take it a little further by learning about inheritance in Python. Inheritance is one of the three building blocks of object-oriented programming with polymorphism and encapsulation being the other two.

Inheritance is a process that allows a class to reuse member variables and methods of another class. The class that inherits member variables and methods from another class is called a derived class or child class and the class that is being inherited by a child class is called a base class or a parent class.

In this article, you will learn about inheritance in Python with the help of examples. You will also study how to pass values from child class constructors to parent class constructors.

Basic Example of Inheritance in Python

Let’s take a look at a very simple example of inheritance in Python:

class X:
    
    def print_text(self):
        self.var = 10
        print("This is parent class")

class Y(X):
    pass

The script above defines a class X with one method print_text() and an instance variable var. The class Y inherits the class X. The class Y doesn’t contain any member variable or method. It is evident from the example above that to inherit a class, you need to pass the parent class name inside parentheses that follow the child’s class name. Since the class Y now inherits the class X, the class Y can access the print_text() method and the var variable by default.

Let’s call the print_text() method and print the value of the var variable of the Y class using an object of the class X:

x = X()
x.print_text()
print(x.var)

Here is the output:

This is parent class
10

The output shows that the child class X is able to access the parent class Y's methods and variables. It is important to mention that only the public members of a class can be inherited by the child class. The protected members can be inherited in case if both the parent and child classes are in the same package while the private members of a class cannot be inherited. To study more about public, protected, and private members, see this article.

Advanced Example of Inheritance

This section contains a more advanced example of inheritance in Python.

Let’s define a class Animal, with child classes Cat, Butterfly and Fish:

class Animal:
    
    def animal_details(self):
        self.name = "Animal"
        self.category = "Mammal"
        self.age = 0
    
    def show_animal_details(self):
        print("The name of parent class is ", self.name)
    

The Animal class contains three instance variables name, category and age, defined inside an instance method animal_details(). The Animal class also contains an instance method show_animal_details() which prints the name of the animal.

Next, we define Cat, Butterfly and Fish classes that inherit the Animal class.

class Cat(Animal):
    
    def cat_details(self):
        self.number_of_paws = 4
        print("A cat has", self.number_of_paws , "pawns")
        
class Butterfly(Animal):
    
    def butterfly_details(self):
        self.number_of_wings = 8
        print("A butterfly has", self.number_of_wings, "wings")

class Fish(Animal):
    def fish_details(self):
        self.number_of_fins = 4
        print("A fish has", self.number_of_fins, "fins")

The Cat class has its own method cat_details() and a member variable number_of_paws, which means that in addition to having access to the name, category and age member variables of the parent Animal class, the Cat class has its own set of member variables.

Similarly, the Butterfly and Fish classes have the methods butterfly_details() and fish_details(), respectively. The butterfly_details() method prints the value of the number_of_wings variable of the Butterfly class whereas the fish_details() method prints the value of the

The basic idea of inheritance is that the methods and variables that are common in multiple child classes are included in the parent class. The methods and variables that are specific and not common among child classes are added in respective child classes.

For example, since not all animals have paws, therefore the variable number_of_paws has not been added to the parent Animal class, rather it is added to the child Cat class. Similarly, the variable number_of_wings and number_of_fins are intrinsic to respectively Butterfly and Fish classes, therefore these variables are added in the respective child classes and not in the Parent Animal class.

The following script creates objects of the Cat, Fish and Butterfly classes and access the parent and child class methods and variables:

cat = Cat()
cat.animal_details()
cat.show_animal_details()
print(cat.category)
print(cat.age)
cat.cat_details()

Here is the output of the script above:

The name of parent class is  Animal
Mammal
0
A cat has 4 pawns
butterfly = Butterfly()
butterfly.animal_details()
butterfly.show_animal_details()
print(butterfly.category)
print(butterfly.age)
butterfly.butterfly_details()

Output:

The name of parent class is  Animal
Mammal
0
A butterfly has 8 wings
fish = Fish()
fish.animal_details()
fish.show_animal_details()
print(fish.category)
print(fish.age)
fish.fish_details()

Output:

The name of parent class is  Animal
Mammal
0
A fish has 4 fins

You can see that the child classes have successfully accessed the parent class methods and variables.

Accessing Parent Class Constructors via Child Classes

There is one problem with the Cat, Butterfly, and Fish classes that we defined in the previous section. In all the three cases i.e. when we create the objects of Cat, Butterfly, and Fish classes and call the show_animal_details() method. The method always prints the animal name as animal, the category as mammals, and the age as 0. We want that for Cat class object, the name should be cat, and the category should be mammal. Similarly, for Butterfly class object the category should be insect rather than mammal. Similarly, the name of the Fish class object should be fish. We can use constructors to implement this logic.

Let’s define the Animal class with a constructor:

class Animal:
    
    def __init__(self, name, category, age):
        self.name = name
        self.category = category
        self.age = age
    
    def show_animal_details(self):
        print("The name of animal is ", self.name)
        print("The category of animal is ", self.category)
        print("The age of animal is ", self.age)
    

In the script above, the Animal class constructor initializes the name, category and age variables. The show_animal_details() method prints the value of these variables.

Next, we define Cat, Butterfly and Fish classes with constructors. The parent class constructors can be accessed from within a child class using the super() object. Look at the following script for a better understanding.

class Cat(Animal):
    
    def __init__(self, name, category, age, paws):
        super().__init__(name, category, age)
        self.number_of_paws = paws
        print("A cat has", self.number_of_paws , "paws")

The Cat class constructor accepts 4 arguments: name, category, age and paws. The first three arguments are passed to the parent class constructor via the super() object, while the fourth argument i.e paws is used to initialize the child class variable number_of_paws.

Similarly, the Butterfly and Fish class also pass arguments to the parent Animal class constructor as shown below:

class Butterfly(Animal):
    
    def __init__(self, name, category, age, paws):
        super().__init__(name, category, age)
        self.number_of_wings = paws
        print("A butterfly has", self.number_of_wings, "wings")
        
class Fish(Animal):
    
    def __init__(self, name, category, age, paws):
        super().__init__(name, category, age)
        self.number_of_fins = paws
        print("A fish has", self.number_of_fins, "fins")
        

Now while creating objects of the Cat, Fish and Butterfly classes, you can pass the values for the name, category and age variables of the parent class. You can see the values for the name, category and age variables of the child class objects via the show_animal_details() function as shown below:

cat = Cat("cat","mammal",5, 4)
cat.show_animal_details()

Here is the output:

A cat has 4 paws
The name of animal is  cat
The category of animal is  mammal
The age of animal is  5

Similarly, the following script creates Butterfly and Fish class objects and prints the values for their name, category, and age variables.

fish = Fish("fish","bony_fish", 10, 4)
fish.show_animal_details()
butterfly = Butterfly("butterfly","insect", 10, 4)
butterfly.show_animal_details()

The output of the above script is as follows:

A fish has 4 fins
The name of animal is  fish
The category of animal is  bony_fish
The age of animal is  10
A butterfly has 4 wings
The name of animal is  butterfly
The category of animal is  insect
The age of animal is  10

Wrapping Up

Inheritance is one of the most useful concepts in object-oriented programming. Inheritance fosters code reusability and organization. The article explains the concept of inheritance in Python with the help of examples. The article also shows how you can initialize parent class variables by calling the parent class constructors from child classes.

If you wish to learn more about Python, make sure to check out our collection of Python tutorials.