Inheritance In Python


What is Inheritance in Python?


Inheritance is one of the most important and powerful features of object-oriented programming. Inheritance refers to the property in which a class can inherit properties of another class that is the derived class can access all the properties, functions, and data members of the base class from which it has been inherited. To compare the feature with real-time, children get characteristics and behaviors inherited from their parents. Inheritance in Python is provided for the same purpose as explained.



Advantages of Inheritance:


  • Reusability of code – since one can inherit from the base class, the efficiency of the code increases and the size of the code also decreases instead of rewriting the code.
  • It forms a chain process i.e. if a child class is inherited from the base class, and if a new class is inherited from the child class, then all the properties of the base class will also be present in the new class.

Syntax:

Class base_class:
     Body of base_class
Class child_class (base_class):
     Body of child_class

Example

class Parent:
     def __init__(self , fname, fage):
          self.firstname = fname
          self.age = fage
     def view(self):
         print(self.firstname , self.age)
class Child(Parent):
     def __init__(self , fname , fage):
          Parent.__init__(self, fname, fage)
          self.lastname = "Machine Learning"
     def view(self):
          print(self.firstname ," came ",  self.age , " years ago. Today, python is the base for" , self.lastname)
object = Child("Python" , '28')
object.view()

Output

Python came  28  years ago. Today, python is the base for Machine Learning

Types of inheritance:


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

Single inheritance


In single inheritance, only one child class can be inherited from the base class. Not more than one class can be used for inheritance.

Syntax:

Class base_class:
     Body of base_class
Class child_class (base_class):
     Body of child_class

Example

class Base:
     def __init__(self , fname, fage):
          self.firstname = fname
          self.age = fage
     def view(self):
         print(self.firstname , self.age)
class Child(Base):
     def __init__(self , fname , fage):
          Base.__init__(self, fname, fage)
          self.lastname = "Machine Learning"
     def view(self):
          print(self.firstname ," is ",  self.age , " years old and he is a pro in " , self.lastname)
object = Child("Raj" , '21')
object.view()

Output

Raj  is  21  years old and he is a pro in  Machine Learning

Multiple inheritance


In multiple inheritance, a child class can be inherited from more than one base class. Unlike single inheritance, there is no restriction on the number of base classes for inheriting the properties.

Syntax:

Class Base1():
   Body of base1
Class of Base2():
   Body of base2
Class Child (Base1,Base2):
   Body of child

Example

class mom:
    mom_name = ""
    def mom(self):
        print(self.mom_name)
class dad:
    dad_name = ""
    def dad(self):
        print(self.dad_name)
class daughter(mom,dad):
    def parents(self):
        print("Father's name: ",self.dad_name)
        print("Mother's name: ",self.mom_name)
object=daughter()
object.dad_name="raj"
object.mom_name="nandhini"
object.parents()

Output

Father's name:  raj
Mother's name:  nandhini

Multilevel inheritance


The multilevel inheritance is like a chain process. In this type, a class is derived from a base class and it becomes a base class for another class that is another new class is derived from this derived class.

Syntax:

Class Base:
    Body of base class
Class Derived1 (Base):
   Body of derived1 class
Class Derived2 (Derived1):
  Body of derived2 class

Example

class Granddad:
 
    def __init__(self, grandfather_name):
        self.grandfather_name = grandfather_name

class dad(Granddad):
    def __init__(self, father_name, grandfather_name):
        self.father_name = father_name
 
        Granddad.__init__(self, grandfather_name)

class Son(dad):
    def __init__(self,son_name, father_name, grandfather_name):
        self.son_name = son_name
 
        dad.__init__(self, father_name, grandfather_name)
 
    def print_name(self):
        print('Grandfather name :', self.grandfather_name)
        print("Father name :", self.father_name)
        print("Son name :", self.son_name)
 
s1 = Son('Lucky', 'Raj', 'Kamal')
s1.print_name()

Output

Grandfather name : Kamal
Father name : Raj
Son name : Lucky

Hierarchical inheritance


In hierarchical inheritance from a single base class, we can inherit multiple derived classes. There is no restriction on the number of derived classes.

Syntax:

Class Base:
   Body of base class
Class Derived1 (Base) :
   Body of derived1 class
Class Derived2 (Base) :
   Body of derived2 class

Example

class Details:
    def __init__(self):
        self.__id="<No Id>"
        self.__name="<No Name>"
        self.__gender="<No Gender>"
    def setData(self,id,name,gender):
        self.__id=id
        self.__name=name
        self.__gender=gender
    def showData(self):
        print("Id: ",self.__id)
        print("Name: ", self.__name)
        print("Gender: ", self.__gender)

class Employee(Details): #Inheritance
    def __init__(self):
        self.__company="<No Company>"
        self.__dept="<No Dept>"
    def setEmployee(self,id,name,gender,comp,dept):
        self.setData(id,name,gender)
        self.__company=comp
        self.__dept=dept
    def showEmployee(self):
        self.showData()
        print("Company: ", self.__company)
        print("Department: ", self.__dept)
class Doctor(Details): #Inheritance
    def __init__(self):
        self.__hospital="<No Hospital>"
        self.__dept="<No Dept>"
    def setEmployee(self,id,name,gender,hos,dept):
        self.setData(id,name,gender
 self.__hospital=hos
        self.__dept=dept
    def showEmployee(self):
        self.showData()
        print("Hospital: ", self.__hospital)
        print("Department: ", self.__dept)

def main():
    print("Employee Object")
    e=Employee()
    e.setEmployee(1,"Prem Sharma","Male","gmr","excavation")
    e.showEmployee()
    print("\nDoctor Object")
    d = Doctor()
    d.setEmployee(1, "pankaj", "male", "aiims", "eyes")
    d.showEmployee()

if __name__=="__main__":
    main()

Output

Employee Object
Id:  1
Name:  Prem Sharma
Gender:  Male
Company:  gmr
Department:  excavation

Doctor Object
Id:  1
Name:  pankaj
Gender:  male
Hospital:  aiims
Department:  eyes

Hybrid inheritance


The hybrid inheritance is a combination of other types of inheritance. Mostly it is a combination of multilevel and multiple inheritance. This type of inheritance is not much used.

In general, multiple and multilevel inheritance is widely used in real-time problems. Single inheritance is used for simple applications.


Check this video for more:



Video credits– Edureka




Also Read:


10 Tkinter Projects for Beginners

Introduction Python has become one of the fastest-growing programming languages, and its applications are also increasing every day. Tkinter Projects for Beginners are one of the best ways to learn the basics of Python. Tkinter is said to be one of the most straightforward modules to start learning Python as well. This will help absolute…

Continue Reading 10 Tkinter Projects for Beginners

10 Useful Python One-Liners

In this tutorial, we will learn about the best and most powerful Python One-Liners ranging from beginner’s level to advanced level that too with examples for better understanding. Using Python One-Liners in your programs will actually make your code more readable and precise. What exactly are Python One-Liners? One-liners in simple language are single-line codes…

Continue Reading 10 Useful Python One-Liners

20 Python Projects for Resume

About 20 Python Projects for Resume Today, in this article on 20 Python Projects for Resume, we’ll expose you to some of the top beginners to advanced python projects for resumes. In 2022, your best investment will be learning Python through practical projects. Therefore, working on actual Python projects is essential if you wish to…

Continue Reading 20 Python Projects for Resume

3 V’s of Big data

In this article, we will explore the 3 V’s of Big data. Big data is one of the most trending topics in the last two decades. It is due to the massive amount of data that has been produced as well as consumed by everyone across the globe. Major evolution in the internet during the…

Continue Reading 3 V’s of Big data