Course Content

Multilevel Inheritance | Python Tutorials For Absolute Beginners In Hindi #62

In Tutorial#60, we learned inheritance, and in the previous tutorial of this course i.e., tutorial#61 , we leaned multiple inheritance. These topics are very important and crucial for us to understand today's topic, which is Multilevel inheritance.

 

Figure 1: Multiple Inheritance

Multiple inheritance and Multilevel inheritance are very similar concepts. If you have a complete understanding of multiple inheritance than understanding, multilevel will take no time. The minimum number of classes for Multilevel inheritance is the same as for multiple inheritance i.e., three. 

What is Multilevel Inheritance in Python?

In multilevel inheritance, a class that is already derived from another class is derived by a third class. So in this way, the third class has all the other two former classes' features and functionalities. The syntax looks something like this:

class Parent1:
    pass
class Derived1(Parent1):
    pass
class Derived2(Derived1):
    pass

Now let us dive into the priority brought by the ordering of the class. Suppose that we are looking for a constructor or a value for any variable. Our program will first check our current class i.e., Derived2, for it. If nothing found, then it will move towards Derived1 and in order at last towards Parent1 until it finds the constructor or variable in the way.

If we have the same method or variable in the base and derived class, then the order we discussed above will be followed, and the method will be overridden. Else, if the child class does not contain the same method, then the derived1 class method will be followed by the sequence defined in the paragraph above.

For Example:

class level1:
      def first(self):
            print ('code')

class level2(level1): #inherit level1
      def second(self):
             print ('with')

class level3(level2): #inherit level2
      def third(self):
            print ('harry')

obj=level3()
obj.first()
obj.second()
obj.third()

Rules for Method overriding:-

There are few rules for Method overriding that should be followed:

  • The name of the child method should be the same as parents.
  • Inheritanceshould be there, and we need to derive a child class from a parent class
  • Both of their parameters should be the same.

Multiple inheritance VS. Multilevel inheritance

Multiple inheritance

Multilevel inheritance

  • Multiple Inheritance is where a class inherits from more than one base class.
  • Sometimes,multiple Inheritance makes the system more complex,that’s why it is not widely used.
  • Multiple Inheritance has two class levels; these are base class and derived class.
  • In multilevel inheritance, we inherits from a derived class, making that derived class a base class for a new class.
  • Multilevel Inheritance is widely used. It is easier to handle code when using multilevel inheritance.
  • Multilevel Inheritance has three class levels, which are base class, intermediate class, and derived class.

Advantages of Inheritance

  1. It reduces code redundancy.
  2. Multilevel inheritance provides code reusability.
  3. Using multilevel inheritance, code is easy to manage, and it supports code extensibility by overriding the base class functionality within child classes

Code as described/written in the video


class Dad:
    basketball =6

class Son(Dad):
    dance =1
    basketball = 9
    def isdance(self):
        return f"Yes I dance {self.dance} no of times"

class Grandson(Son):
    dance =6
    guitar = 1

    def isdance(self):
        return f"Jackson yeah!" \
            f"Yes I dance very awesomely {self.dance} no of times"

darry = Dad()
larry = Son()
harry = Grandson()

# print(darry.guitar)

# electronic device
# pocket gadget
# phone



Comments(5)

shiv29 1 month, 3 weeks ago
Error aa raha hai bhai. 

Traceback (most recent call last):
  File "d:\Visual Studio Code Python\.vscode\tuto 62.py", line 21, in <module>
    print(harry.basketball())
TypeError: 'int' object is not callable
PS D:\Visual Studio Code Python>
shiv29 1 month, 3 weeks ago
my bad I called it as a function. Sorry
shiv29 1 month, 3 weeks ago
class Electronic_Device():
    charger = "Wire Charger"
    display = "Tube Display"
    hdmi = 3
    smart = "Yes"
    def edevice_attri(self):
        return f"Electronic device attributes are {self.charger} , {self.display} , {self.hdmi} , {self.smart}"

class Pocket_Gadget(Electronic_Device):
    display = "LCD"
    hdmi = 1
    battery = "Portable"
    def pocket_g(self):
        return f"Pocket gadget attributes are {self.display} , {self.hdmi} , {self.battery}"


class Phone (Pocket_Gadget):
    display = "LED"
    charger = "wireless"
    camera = "80 MP rear, 20 front"
    def phone_attri(self):

        return f"Phone attributes are {self.display} , {self.charger} , {self.camera}"


tv = Electronic_Device()
pazer = Pocket_Gadget
mobile = Phone

print(mobile.hdmi)
print(mobile.smart)
print(tv.edevice_attri())
deva89 1 month, 2 weeks ago
# Multi Level Inheritance

class Grandfather:
  age = 70
  no_of_kids=2

  def __init__(self,fname, lname):
    self.fname = fname
    self.lname = lname

  @property
  def name(self):
    return self.fname+" "+self.lname

class Father(Grandfather):
  age =50
  no_of_kids=1

  def __init__(self, fname, ffname, flname):
    self.fname = fname
    print("***", self.fname)
    self.ffname = ffname
    self.flname = flname
    super().__init__(self.ffname, self.flname)

  @property
  def name(self):
    print("---"+self.fname)
    return self.fname+" "+self.ffname+" "+self.flname


class Son(Father):

  age =15

  def __init__(self, fname, ffname, flname):
    self.fname = fname
    self.ffname = ffname
    self.flname = flname
    super().__init__(self.ffname, self.flname)
    self.no_of_siblings = super().no_of_kids -1

  @property
  def name(self):
    print("%%%"+self.fname)
    return self.fname+" "+self.ffame+" "+self.flname


if __name__ == '__main__':
  gObj = Grandfather("larry", "Blake")
  print(Grandfather.age)
  print(gObj.name)
  print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
  fObj = Father("Dave","John","Blake")
  print(dir(fObj))
  print(fObj.name)
  
-------------------------------------------------------
Output : 
70
larry Blake
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
*** Dave
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'ffname', 'flname', 'fname', 'lname', 'name', 'no_of_kids']
---John
John John Blake (**** Wrong : Dave John Blake  *** Why Issue)
deva89 1 month, 2 weeks ago
# Multi Level Inheritance

class Grandfather:
  age = 70
  no_of_kids=2

  def __init__(self,fname, lname):
    self.fname = fname
    self.lname = lname

  @property
  def name(self):
    return self.fname+" "+self.lname

class Father(Grandfather):
  age =50
  no_of_kids=1

  def __init__(self, fname, ffname, flname):
    self.fname = fname
    print("***", self.fname)
    self.ffname = ffname
    self.flname = flname
    super().__init__(self.ffname, self.flname)

  @property
  def name(self):
    print("---"+self.fname)
    return self.fname+" "+self.ffname+" "+self.flname


class Son(Father):

  age =15

  def __init__(self, fname, ffname, gfname, glname):
    self.fname = fname
    self.ffname = ffname
    self.gfname = gfname
    self.glname = glname
    super().__init__(self.ffname, self.gfname, self.glname)
    self.no_of_siblings = super().no_of_kids -1

  @property
  def name(self):
    print("%%%"+self.fname)
    return self.fname+" "+self.ffname+" "+self.glname

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here