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