Hybrid inheritance in python

Previous
Next

Hierarchical and Hybrid Inheritance:

Hierarchical inheritance:

  • An object sharing the functionality to more than one object is called Hierarchical.
  • We need to create object for every child to access the functionality.
class A:
    def m1(self):
        print("A-m1")
        return
    
class B(A):
    def m2(self):
        print("B-m2")
        return

class C(A):
    def m3(self):
        print("C-m3")
        return
    
class D(A):
    def m4(self):
        print("D-m4")
        return

class Inheritance:
    def main():
        d = D()
        d.m4()
        d.m1()
        d.m2()
        return

Inheritance.main()
  • We cannot access the functionality of siblings using one class object.
  • We need to create object separately to all child objects.
class A:
    def m1(self):
        print("A-fun")
        return
    
class B(A):
    def m2(self):
        print("B-fun")
        return

class C(A):
    def m3(self):
        print("C-fun")
        return
    
class D(A):
    def m4(self):
        print("D-fun")
        return

class Inheritance:
    def main():
        b = B()
		b.m1()
		b.m2()
		b.m3() # error 
        return

Inheritance.main()

Hybrid inheritance:

  • The combination of hierarchical and multiple becomes Hybrid inheritance
  • We can access the functionality of Hybrid inheritance as follows
class A:
    def m1(self):
        print("A-m1()")
        return
    
class B(A):
    pass

class C(A):
    def m2(self):
        print("C-m2()")
        return
    
class D(A):
    def m2(self):
        print("D-m2()")
        return
    def m3(self):
        print("D-m3()")
        return

class Inheritance:
    def main():
        obj = D()
		obj.m1()
		obj.m2()
		obj.m3() 
		C.m2(obj)
        return

Inheritance.main()
Previous
Next

Add Comment

Courses Enquiry Form