What is inheritance in Python?
Inheritance is a concept where one class accesses the methods and properties of another class. The parent class is the class holding inherited from, also called a base class. The child class is the class that inherits from another class, so it’s also called the derived class.Types of inheritance in Python?
There are five types of inheritance in Python and those are given below:- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance:
- Hybrid Inheritance
How to create a Parent Class?
Any class often a parent class, so that the syntax is that the same as making the other classExample
Let’s Create a category named Student, with first name and last name properties, and a print name method:class Student: def __init__(self, first_name, last_name): self.firstname = first_name self.lastname = last_name def displayname(self): print(self.firstname, self.lastname) #Use the Person categoryto makeassociate degree object, so execute the printname method: x = Student("Pramod", "Yadav") x.displayname()
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py Pramod Yadav Process finished with exit code 0
How to Create a Sub Class?
To create a class that inherits the practicality from another class, send the parent class as a parameter once making the child class:Example:
Create a class named Boy, which is able to inherit the properties and strategies from the Student class:class Boy(Student): passNote: Use the pass keyword after you don’t wish to feature the other properties or strategies in the class.
Now the Boy class has identical properties and methods like as Student class.
Example
Use the Boy class to make an associate degree object, so execute the displayname method:x = Boy("Amresh", "Mahato") x.displayname()
Source Code:
class Student: def __init__(self, first_name, last_name): self.firstname = first_name self.lastname = last_name def displayname(self): print(self.firstname, self.lastname) class Boy(Student): pass x = Boy("Amresh", "Mahato") x.displayname()
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py Amresh Mahato Process finished with exit code 0
Add the __init__() function
When you add the __init__() function, the child class will have not longer inherit the parent’s __init__() function.Note: The child’s __init__() function overrides the inheritance of the parent’s __init__() function.To have the inheritance of the parent’s __init__() function, add a cry to the parent’s __init__() functionExampleAdd the __init__() function to the class:class Student: def __init__(self, first_name, last_name): #add properties, etc.When you add the __init__() operate, the kid category can now not inherit the parent’s __init__() operate.
Note: The child’s __init__() operate overrides the inheritance of the parent’s __init__() operate.
To keep the inheritance of the parent’s __init__() operate, add a decision to the parent’s __init__() function:
Example
class Boy(Student): def __init__(self, first_name, last_name): Student.__init__(self, first_name, last_name)
Source code:
class Student: def __init__(self, first_name, last_name): self.firstname = first_name self.lastname = last_name def displayname(self): print(self.firstname, self.lastname) class Boy(Student): def __init__(self, first_name, last_name): Student.__init__(self, first_name, last_name) x = Boy("Pramod", "Yadav") x.displayname()Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py Pramod Yadav Process finished with exit code 0Now we’ve with success added the __init__() operate, and unbroken the inheritance of the parent category, and that we square measure able to add practicality within the __init__()function.
Use the super() Function:
Python even has a super() function which will build the child class inherit all the strategies and properties from its parent:Exampleclass Student: def __init__(self, first_name, last_name): self.firstname = first_name self.lastname = last_name def displayname(self): print(self.firstname, self.lastname) class Boy(Student): def __init__(self, first_name, last_name): super().__init__(first_name, last_name) x = Boy("Pramod", "Yadav") x.displayname()Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py Pramod Yadav Process finished with exit code 0
By exploiting the super() function, you are don’t have to use the name of the parent part, it’ll mechanically inherit the strategies and properties from its parent.
Add Properties:
Example
Add a property known as graduationyear to the Boy class:class Student: def __init__(self, first_name, last_name): self.firstname = first_name self.lastname = last_name def displayname(self): print(self.firstname, self.lastname) class Boy(Student): def __init__(self, first_name, last_name): super().__init__(first_name, last_name) self.graduationyear = 2021 x = Boy("Pramod", "Yadav") print(x.graduationyear)Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py 2021 Process finished with exit code 0
In the example below, the year 2021 has to be a variable and passed into the student class once making boy objects. To do so, add another parameter within the __init__() function:
Example
Add a year parameter, and pass the proper year once making objects:class Student: def __init__(self, first_name, last_name): self.firstname = first_name self.lastname = last_name def displayname(self): print(self.firstname, self.lastname) class Boy(Student): def __init__(self, first_name, last_name,year): super().__init__(first_name, last_name) self.graduationyear = year x = Boy("Pramod", "Yadav",2021) print(x.graduationyear)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py 2021 Process finished with exit code 0
Add Methods:
Example
Add a method known as welcome to the Boy class:class Student: def __init__(self, first_name, last_name): self.firstname = first_name self.lastname = last_name def displayname(self): print(self.firstname, self.lastname) class Boy(Student): def __init__(self, first_name, last_name,year): super().__init__(first_name, last_name) self.graduationyear = year def welcome(self): print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear) x = Boy("Pramod", "Yadav",2021) x.welcome()Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py Welcome Pramod Yadav to the class of 2021 Process finished with exit code 0
Recommended Post:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers