Python Inheritance Tutorial
Python Inheritance Tutorial, In this tutorial, we will learn What is inheritance in Python and Types of inheritance in Python. Here, You will also learn Python inheritance Syntax, example of python inheritance which are helpful for any Python developers. Are you looking for the information of Python inheritance tutorial or Are you dreaming to become to certified Pro Python Developer, then stop just dreaming, get your Python certification course from India’s Leading Python training institute.
Inheritance provides reusability & supports transitivity. It offers faster development time, easier maintenance and easy to extend and represents real world relationships. Do you want to know about what is inheritance in Python and Types of Inheritance in Python, then just follow the below mentioned Python Inheritance Tutorial for Beginners from Prwatech and take advanced Python training like a Pro from today itself under 10+ years of hands-on experienced Professionals.
To get a better understanding of this concept, let’s start Inheritance in Python Tutorial.
What is Inheritance in Python?
Inheritance is the ability of one class to run or inherit the properties from another class.Inheritance is the fundamental feature of OOPs which varieties the functionality of current class by adding new features. The benefits of inheritance are:
It represents real-world relationships well. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it. Mean the reusability of code is possible.
It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.Inheritance is used to define a new class having all the properties of the old existing class.
The parent class is the class being inherited from, also called the superclass or the base class.The child class is the class that inherits from another class, also called subclass, or derived class.
Create a Child Class
A child class needs to identify its parent class. This can be done by writing the parent class name in the definition of the child class.
print(‘xyz is parent class’)
print(‘abc is child class of xyz’)
xyz is parent class
abc is child class of xyz
Here abc is the child class or subclass and xyz is parent class or superclass. Now let’s see one more example which will show how the attributes and functions can be inherited by child class from parent class.
Example of Python inheritance
Let’s consider a ‘Sports’ category is parent class and ‘Shoes’ is child or subclass in following example.
def __init__(self, model, color, cost):
self.__model = model
self.__color = color
self.__cost = cost
# __model,__color,__costt are private to Sports class
# It is accessible outside the class Sports
self.__color = color
self.__cost = cost
def __init__(self, model,color,cost,brand):
# We have to call parent constructor to set parameters excluding brand.
super().__init__(model, color, cost)
self.__brand = brand
returnself.getModel() + ” of brand “+ self.__brand + ” in ” + self.getcolor() + ” color for ” + self.getcost() + ” ”
# In method getInfo we can call getmodel(), getcolor(),getcost() as they are accessible in the child class through inheritance.
sh1 = Shoes(“Shoes”, “Black”, “Rs.3699″,”Xyz123”)
sh2= Shoes(“Shoes”, “Black-red”, “Rs.4599”, “Xyz345”)
Shoes of brand Xyz123 in Black color for Rs.3699
Shoes of brand Xyz345 in Black-red color for Rs.4599.
Use of Super() :
The super() method permits to access the inherited methods which links to a class object.In above example, we’ve used the super() method in the constructor of the subclass i.e. child class ‘Shoes’. It is appealing the function of the base class ‘Sports’.
Working of super():
As we know in above example subclass ‘Shoes’ has no any properties defined such as color , cost and model. But these are available in ‘Sports’. We can access those properties for child class Shoes with help of super() function. Shoes has no method getModel() but it is accessible via Shoes class.
Types of Inheritance in python
Single inheritance:If subclass inherits from only one super class, it is called as single inheritance. (As stated in above example)
Multiple inheritance: When a subclass inherits from multiple parent classes, it is called as multiple inheritance.Python has provision for multiple inheritance. We have to specify all parent classes in bracket as list with comma separation.
Multiple inheritance Example
self.test1 = “Python”
self.test2 = “Tutorials”
# Calling constructors of super1 and super2 classes
obj = multiinher()