What is inheritance and types of inheritance in Python?

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 class

Example

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):
  pass
Note: 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 0
Now 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:Example
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)

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:

Get SalesforceĀ Answers

Pramod Kumar Yadav is from Janakpurdham, Nepal. He was born on December 23, 1996, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India.He is director of Appan Bazaar Pvt.Ltd., as teacher in Tridev Eng. School , and an Educational Consultant, and he is currently working as a developer and Digital Marketer.



Leave a Comment