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__() function Example Add the __init__() function to the class:

class Student:
  def __init__(self, first_name, last_name):
#add properties, etc.
When you add the __init__() operatethe 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 categoryand 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() functionyou are don’t have to use the name of the parent partit’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 Janakpur Dham, Nepal. He was born on December 23, 1994, 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. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment