Python Inheritance

Inheritance

An important concept in object oriented programming is class inheritance. when a class is created based on an existing class, and the new class inherits the attributes and methods from the existing class. The new class is usually called “child class”, and the existing class is called “parent class”. Inheritance provides code reusability to the program.

The parent class is called the base class and the child class is called derived class. The child class inherits from another class.

Types of Inheritance

There are five types of Inheritance. They are,

  • Single inheritance
  • Multiple inheritance
  • Multilevel inheritance
  • Hierarchical inheritance
  • Hybrid inheritance

Single inheritance

When a child class inherits from only one parent class, then it is known as single inheritance.

Example:

class Teacher:
  def __init__(self, fname, lname):
    self.first_name = fname
    self.last_name = lname

  def printname(self):
    print("The First Name is:", self.first_name, ":", "and the Last Name is:", self.last_name)

class Student(Teacher):
 pass

x = Student("Regu", "Ram")
x.printname()

Output:

The First Name is: Regu : and the Last Name is: Ram

Multiple inheritance

When a child class inherits from multiple parent classes, then it is known as multiple inheritance.

Example:

class parent_class1(object):
 def __init__(self):
  self.str1 = "Python"

class parent_class2(object):
 def __init__(self):
  self.str2 = "Tutorial"

class child_class(parent_class1, parent_class2):
 def __init__(self):
  parent_class1.__init__(self)
  parent_class2.__init__(self)

 def printStrs(self):
  print(self.str1, self.str2)

obj1 = child_class()
obj1.printStrs() 

Output:

Python Tutorial

Multilevel inheritance

When a class inherits on second class and second class further inherits on another class, it is known as multilevel inheritance.

Example:

class Employee1:
    def __init__(self):
        self.__id=0
    def setId(self):
        self.__id=int(input("Enter Id: "))
    def showId(self):
        print("Id: ",self.__id)

class Employee2(Employee1):
    def __init__(self):
        self.__name=""
    def setName(self):
        self.setId()
        self.__name=input("Enter Name: ")
    def showName(self):
        self.showId()
        print("Name: ",self.__name)

class Employee3(Employee2):
    def __init__(self):
        self.__gender=""
    def setGender(self):
        self.setName()
        self.__gender=input("Enter Gender: ")
    def showGender(self):
        self.showName()
        print("Gender: ",self.__gender)


class Employee(Employee3):
    def __init__(self):
        self.__desig=""
    def setEmployee(self):
        self.setGender()
        self.__desig=input("Enter Designation: ")
    def showEmployee(self):
        self.showGender()
        print("Designation: ",self.__desig)

def main():
    emp = Employee()
    emp.setEmployee()
    emp.showEmployee()

if __name__=="__main__":main()

Output:

Enter Id: 101
Enter Name: Regu
Enter Gender: Male
Enter Designation: Admin
Id:  101
Name:  Regu
Gender:  Male
Designation:  Admin

Hierarchical inheritance

When more than one derived classes are created from the same base or parent class, it is known as Hierarchical inheritance.

Example:

class parent_class:
      def my_func1(self):
          print("This is the 1st function")
class child_class1(parent_class):
      def my_func2(self):
          print("This is the 2nd function")
class child_class2(parent_class):
      def my_func3(self):
          print("This is the 3ed function")

obj = child_class1()
obj1 = child_class2()
obj.my_func1()
obj.my_func2()

Output:

This is the 1st function
This is the 2nd function

Hybrid inheritance

Hybrid inheritance combines more than one form of inheritance.

Example:

class parent_class:
     def my_func1(self):
         print("function 1 - Neural Beast - Always deliver more than expected")

class child_class1(parent_class):
     def my_func2(self):
         print("function 2  - Neural Beast - Always deliver more than expected")

class child_class2(parent_class):
     def my_func3(self):
         print("function 3  - Neural Beast - Always deliver more than expected")

class child_class3(parent_class):
     def my_func4(self):
         print("function 4  - Neural Beast - Always deliver more than expected")

obj = child_class2()
obj.my_func1()

Output:

function 1 - Neural Beast - Always deliver more than expected

More Reading

Post navigation

2 Comments

  • A lot of thanks for your whole efforts on this site. Kim really loves setting aside time for research and it’s obvious why. My partner and i know all about the lively mode you convey efficient ideas through your web site and in addition cause participation from other individuals on the area of interest while our own simple princess is in fact starting to learn a great deal. Take advantage of the remaining portion of the year. You are always doing a brilliant job.

Leave a Reply

Your email address will not be published. Required fields are marked *