Inheritance in Python | Python Tutorial
Inheritance in Python
In object-oriented programming, inheritance is the mechanism of deriving new classes from existing ones such as superclass and base class.
Inheritance allows us to define a class that inherits all the methods and properties of another class.
The parent class is the class being inherited from, also called a base class. The child class is the class that inherits from another class, also called derived class.
Characteristics of Inheritance:
- It can be used to represent real-world relationships well.
- It provides reusability of code, i.e don’t need to write the same code, again and again, we can also add more features to a class without modifying it.
- It is transitive, which means if class B inherits from class A, then if class B has any subclass it would automatically inherit from class A.
- It provides readability.
Syntax:
class ParentClass:
Body of Parent class
class DerivedClass(BaseClass):
Body of derived class
Example of Inheritance in Python
To demonstrate the use of inheritance, let take an example.
Code:
#parent class
class Parent:
def function1(self):
print(“this is a parent function”)
#child class
class Child:
def function2(self):
print(“this is child class”)
#object of child class
obj = Child()
#calling function1 using child class object
obj.function1()
#calling function2 using child class object
obj.function2()
Code Explanation:
- First, we have created a parent class and named it as ‘Parent’.
- A function named ‘function1’ is created which takes self as an argument. This function will just print the statement.
Then to create a class that inherits the functionality from another class, we send the parent class as a parameter when creating the child class and named it as
- ‘Child’.
- Here again, we created a function and named it as ‘function2’ This will print the statement written over there when this function will be called.
- Now to call the function we need to create an object of the child class. So we created an object of ‘Child’ class as ‘obj’.
- And at last, using obj and dot operator function of inherited class as well as child class is called.
__init__() Function:
This function is called automatically every time the class s used to create an object. When we define __init__ function for a parent class the child class is not able to inherit the properties of the parent class. That is child class init function will override the parent class init function.
Let’s see through an example code:
#overriding parent class __init__()
class Parent:
def __init__(self, fname, fage):
self.name = fname
self.age = fage
def view(self):
print(self.name, self.age)
class Child(Parent):
def __init__(self, fname, fage):
self.name ="Rohan"
self.lastname ="Malhotra"
self.age= 13
def view(self):
print(self.name,self.lastname, self.age)
obj1 = Child(12, 'Sidharth')
obj1.view()
#output: Rohan Malhotra 13
The child's __init__() function overrides the inheritance of the parent's __init__() function. If we want to inherit the init function of a parent class then there are two ways to do that
1. __init__() function: To keep the inheritance of the parent's __init__() function, add a call to the parent's __init__() function: let’s look at the example code below:
class Parent:
def __init__(self, fname, fage):
self.name = fname
self.age = fage
def view(self):
print(self.name, self.age)
class Child(Parent):
def __init__(self, fname, fage):
Parent.__init__(self,fname,fage)
self.lastname ="Malhotra"
def view(self):
print(self.name,self.lastname, self.age)
obj1 = Child('Sidharth',12)
obj1.view()
#output: Sidharth Malhotra 12
2. Use the super() Function:
Python also has a super() function that will make the child class inherit all the methods and properties from its parent, let’s
look at the example code:
#using super():
class Parent:
def __init__(self, fname, fage):
self.name = fname
self.age = fage
def view(self):
print(self.name, self.age)
class Child(Parent):
def __init__(self, fname, fage):
super().__init__(fname,fage)
#super().__init__()doesn’t take self as parameter
self.lastname ="Malhotra"
def view(self):
print(self.name,self.lastname, self.age)
obj1 = Child('Sidharth',12)
obj1.view()
#output:
Sidharth Malhotra 12
Types of Inheritance in Python:
Simple Inheritance in python:
When inheritance involves a single base class and single child class.
Code:
class Parent:
def func1(self):
print('this is simple inheritance function 1')
class Child(Parent):
def func2(self):
print('this is simple inheritance function 2')
obj= Child()
obj.func1()
#output: this is simple inheritance function 1
Multiple Inheritance in python:
It involves more than one parent class.
Code:
#Multiple Inheritance
class Parent1:
def func1(self):
print('this is multiple inheritance function 1')
class Parent2:
def func3(self):
print('this is multiple inheritance function 3')
class Child(Parent1,Parent2):
def func2(self):
print('this is multiple inheritance function 2')
obj= Child()
obj.func1()
obj.func3()
#output:
#this is multiple inheritance function 1
#this is multiple inheritance function 3
Multilevel Inheritance in python:
When the child class act as the parent class for another class.
Code:
#multilevel
class Parent1:
def func1(self):
print('this is multilevel inheritance function 1')
class Parent2(Parent1):
def func3(self):
print('this is multilevel inheritance function 3')
class Child(Parent2):
def func2(self):
print('this is multilevel inheritance function 2')
obj= Child()
obj.func1()
obj.func3()
#output:this is multilevel inheritance function 1
#this is multilevel inheritance function 3
Hierarchical Inheritance in Python:
It involves more than one derived class from a single base.
Code:
#hierarchical
class Parent:
def func1(self):
print('this is hierarchical inheritance function 1')
class Parent2(Parent):
def func3(self):
print('this is hierarchical inheritance function 3')
class Child(Parent):
def func2(self):
print('this is hierarchical inheritance function 2')
obj = Child()
obj.func1()
obj.func2()
#obj.func3() #This will raise an error AttributeError: 'Child' object has no attribute 'func3'
#So we need to create an object of class parent2() to call the func3.
ob = Parent2()
ob.func3()
#output:
#this is hierarchical inheritance function 1
#this is hierarchical inheritance function 2
#this is hierarchical inheritance function 3
Hybrid Inheritance in Python:
It involves more than one type of inheritance in a single program.
Code:
#hybrid
class Parent:
def func1(self):
print('this is hybrid inheritance function 1')
class Parent2(Parent):
def func3(self):
print('this is hybrid inheritance function 3')
class Parent3():
def func4(self):
print('this is hybrid inheritance function 4')
class Child(Parent,Parent3):
def func2(self):
print('this is hybrid inheritance function 2')
obj = Child()
obj.func1()
obj.func4()
#output:
#this is hybrid inheritance function 1
#this is hybrid inheritance function 4
I hope you like this article, But if you find anything incorrect then please do comments so that I can improve this article.
Thank you so much for reading this article. Happy learning🙂🙂