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.
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.
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()
There are few rules for Method overriding that should be followed:
Multiple inheritance |
Multilevel inheritance |
|
|
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
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())
# 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)
# 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
No downloadable resources for this video. If you think you need anything, please post it in the QnA!
Any Course related announcements will be posted here