Inheritance is a very important aspect of an object-oriented programming language. When you write a class, you do not always want to start from the beginning. If the class that you are writing is a modified version of some different class then you can use inheritance.
When you inherit from a class, your class would get all of the functionality of the parent class, it will also allow us to add more functionality to it.
When you inherit from an existing class, the existing class is called the base or parent class, and the class that got inherited is called derived or child class.
But why is inheritance so important?
- Classes are used to model real-life conditions, and inheritance plays a major role in it.
- If class B is inherited from class A, then all of the subclasses of B will also inherit from A.
- Inheritance makes you able to reuse the code. When you are working in a team project where each individual writes a part of the code for a bigger model, it becomes important that other team members can use the classes created by you and vice versa.
So how to use inheritance in Python to derive a class?
You can derive a class from another class like this
Now, let us create a parent class to understand this concept with the help of an example.
After defining the __init__() function, we will define the two functions that print text on the output.
Now, let us use the methods that we created above.
To give an example, let us create a class of a laptop. This class will be the child of the class of a Computer. Laptops are specific kinds of computers, so we can derive our laptop class from the computer class written above.
Here is an example of Python Inheritance.
The super() function
When you write the __init__() method for child class in Python inheritance, you have to use super().
It is a special function that is used to connect the parent and child class. This super() inheritance function in Python calls the __init__() method from the parent class that gives all of the attributes and methods to the instance of a base class.
It is called the super() because of the convention of calling the parent class a superclass and the child class a subclass.
Attributes and Methods for Child Class
We can have some attributes and methods that are only specific to the laptop class.
For example, all computers do not come with a pre-installed battery but a laptop does, also, a laptop is a very portable device.
You can add as many methods and attributes as you want to a child class for a high-level specificity. However, you should be careful while adding an attribute or a method that can be added to any computer, that attribute or method should not be added to the laptop class.
When a method from the parent class does not fit in the derived class, you can override it. For doing this, you have to define a method in the derived class with the same name as the method that you want to override from the parent class.
By doing this, Python will ignore the method created in the parent class and will only use the method present in the child class. For example, we have a print_details() method in the Computer class, let us create the same method specific to the Laptop class.
Now if you try to call the print_details() method with the laptop class, Python will only execute the method present in the Laptop class and will ignore the one present in Computer class.
Instances as Attributes
Sometimes, your class can become too lengthy by adding many methods and attributes to it. This can lead to increased file sizes and hard to read code.
To overcome this problem you can break a big class into smaller classes and use them as a way to categorize your different methods present in the main class.
Let us assume that in our Laptop class, you have added too many methods related to battery, now, you can create a different class with the name Battery and add all of the battery-related methods to that class.
Let us understand what is happening here step by step:
- We created a separate class named Battery that will contain all of the battery-related methods of the laptop.
- Here, we have created an instance of the Battery class inside the inherited Laptop class.
Finally, we access the method inside the Battery class using the instance present in the Laptop class.
Types of Inheritance
- Single Inheritance – Only one class is derived from the parent class.
- Multilevel Inheritance – Grandchild and Child relationship between classes.
- Multiple Inheritance – A single class is derived from multiple classes.
- Hybrid Inheritance – Multiple derived classes from a single parent class.
- Hierarchical Inheritance – Mixture of different inheritance types.
So, let us have a look at each of them one by one except Single Inheritance because we have already discussed it above:
You can inherit a class from the parent class, and then again inherit a class from the inherited class. The final class that got inherited will be the grandchild class, and the base class will become the grandparent class.
Now, let us derive another class from the child class –
This class inherits all of the methods from the base class too.
Let’s print everything out, one with default values and one after changing the default values.
As you can see, our grandchild is able to execute methods from the base class.
In Python, inheritance can help us to derive a class from multiple base classes, meaning that the derived class would have all of the attributes and methods of those base classes. Other programming languages like Java do not support multilevel inheritance.
Let us have an example of how to implement multiple inheritances. We are going to use a real-life example of a tablet that can act both as a laptop and a smartphone, sometimes.
We will create the class that will inherit from both the classes
Using objects to print out everything –
Multiple inheritances is a feature that has to be implemented in a careful manner.
For example, suppose we want to inherit a class from two parent classes, and both parents have some methods or attributes with a similar name. If those methods and attributes are not overridden in the derived class then you will have to decide the parent class that will get initialized first.
This type of practice just increases the complexity of the code, so be careful while using multiple inheritances!
For the last two types of inheritances, examples will not be necessary to understand.
You can derive multiple classes from a single base class. This is more like a property of inheritance rather than its sub-category. Hierarchical inheritance is very important for the reusability of your code.
Take every type of inheritance you have learned so far, mix them together. The final product that you will get would be a mixture of every two or more types of inheritance. This is called Hybrid Inheritance.
One of the best practices of Python inheritance is to create a private member of a class. The idea behind this is that you do not always want all of your attributes accessible to the derived classes.
To create a private member in a class, add double underscores as a prefix to the member’s name that you want to hide.
Consider this as an example –
Since we made the attribute private at 1, when we tried to access it, Python gave us an error.