Python Inheritance
The purpose of this lesson is to provide an understanding of Python inheritance by using examples.
Syntax:
Class BaseClass: {Body} Class DerivedClass(BaseClass): {Body}
Python Inheritance
When we talk about Python inheritance, it allows us to create a class that inherits all its methods and properties from another class.
The parent class is called the base class because it is the class that is being inherited from.
Classes that inherit from another class are called child classes or derived classes.
Python Class Inheritance:
- Class inheritance allows you to reuse code from existing classes. By creating a new class that inherits from a base class, you can inherit attributes and methods from the base class, reducing code duplication and promoting modularity.
- Inheritance enables you to create specialized classes that inherit and extend the functionality of a base class. You can add new attributes and methods to the derived class or override existing ones to provide specialized behavior. This allows for flexible and customizable class hierarchies.
- Inheritance plays a crucial role in achieving polymorphism in Python. Polymorphism allows objects of different classes to be treated interchangeably based on a common interface or shared base class. This promotes code flexibility and simplifies code logic by allowing the same methods to be called on different objects with varying implementations.
- Inheritance helps in organizing code by creating a hierarchical structure of related classes. It allows you to model real-world relationships and abstract common attributes and behaviors into a base class. This promotes code readability, maintainability, and understanding of the underlying structure.
- Python supports multiple inheritance, which means a derived class can inherit from multiple base classes. This feature allows you to combine and inherit features from multiple sources, providing even greater flexibility and code reuse. However, it requires careful management to avoid ambiguity or conflicts in method resolution order.
Creating a Parent Class
When it comes to Python Class inheritance, any class can be a parent class, so the syntax is identical.
Make a class named Address, with city_name and country_name properties, and a print_address:
Python Parent Class Example: 1 
Now make a class named Student_detail, with s_id, s_name and s_age properties, and a print_detail method:
Python Parent Class Example: 2 
Creating a Child Class
When dealing with Python class inheritance, the parent class is passed as a parameter when creating a child class so functionality can be inherited from it.
Make a class called Records that inherits the attributes and functions of the Student_detail class:
Python Child Class Example: 1 
Reminder: When you don’t need to provide the class any new methods or attributes, use the pass keyword.
Therefore, the Student_detail class’s properties and methods are shared by the Records class as well.
Now, Create an object using the Records class, and then call the print_detail function on the object:
Python Child Class Example: 2 
Make a class called Courses that inherits the attributes and functions of the Student_detail class:
Python Child Class Example: 3 
Reminder: When you don’t need to provide the class any new methods or attributes, use the pass keyword.
Therefore, the Student_detail class’s properties and methods are shared by the Courses class as well.
Create an object using the Courses class, and then call the print_detail function on the object:
Python Child Class Example: 4 
Add __init__() Function
As of now, we have developed a child class in Python inheritance that receives properties and methods from its parent.
To the child class, we want to add the __init__() function (instead of the pass keyword).
Reminder: When the class is used to create a new object, the __init__() function is immediately invoked.
Insert the __init__() method to the Records class:
__init__() Function Example: 1 
The child class will no longer inherit the parent’s __init__() function if you insert the __init__() function.
The __init__() method of the child overrides the inheritance of the __init__() method of the parent.
Add a call to the parent’s __init__() method to maintain the inheritance of the parent’s __init__() function:
__init__() Function Example: 2 
__init__() Function Example: 3 
Now that the __init__() function has been properly implemented and the parent class’ inheritance has been maintained, we can start adding functionality to the function.
Using super() Function
When it comes to Python inheritance, there is also a super() function that will allow the child class to inherit all of the methods and properties from its parent:
Super() Function Example: 1 
Super() Function Example: 2 
You do not need to specify the parent element’s name when using the super() function because the child element will immediately take its parent’s methods and properties.
Add Properties
Create a property named cgpa in the Records class:
Python Inheritance Add Property Example: 1 
Create a property named course_name in the Courses class:
Python Inheritance Add Property Example: 2 
When creating records objects, the cgpa 3.67 and course_name is Object Oriented Programming should be a variable included into the Records and Courses class respectively.
You can do this by adding another parameter to the init() method.
When creating objects, provide a cgpa parameter and pass the appropriate cgpa:
Python Inheritance Add Property Example: 3 
When creating objects, provide a course parameter and pass the appropriate course:
Python Inheritance Add Property Example: 4 
Add Methods
Include a function called total_marks in the Records class:
Add Method Example: 1 
Include a function called course_marks in the Courses class:
Add Method Example: 2 
In the child class, you can override the inheritance of the parent method if the method in the child class has the same name as the function in the parent class.
Python Inheritance Benefits
Code reusability is made possible since a child class inherits all the functionalities of its parent class.
Functionality can be inherited once it has been developed. The wheel does not need to be reinvented. As a result, the code is cleaner and easier to maintain.
You can even define your own functionalities for the child class, so only essential functions will be inherited and other required functions will be defined.