Sobes.tech
Junior

What types of inheritance exist in object-oriented programming?

sobes.tech AI

Answer from AI

  • Single Inheritance: A class inherits from one parent class.

    class Parent:
        pass
    
    class Child(Parent): # Child class inherits from Parent
        pass
    
  • Multiple Inheritance: A class inherits from multiple parent classes.

    class Parent1:
        pass
    
    class Parent2:
        pass
    
    class Child(Parent1, Parent2): # Child class inherits from Parent1 and Parent2
        pass
    
  • Multilevel Inheritance: A class inherits from another class, which in turn inherits from another class.

    class Grandparent:
        pass
    
    class Parent(Grandparent): # Parent inherits from Grandparent
        pass
    
    class Child(Parent): # Child inherits from Parent
        pass
    
  • Hierarchical Inheritance: Multiple classes inherit from one parent class.

    class Parent:
        pass
    
    class Child1(Parent): # Child1 inherits from Parent
        pass
    
    class Child2(Parent): # Child2 inherits from Parent
        pass
    
  • Hybrid Inheritance: A combination of two or more of the above types of inheritance.

    class A:
        pass
    
    class B(A): # Multilevel: B inherits from A
        pass
    
    class C(A): # Hierarchical: C inherits from A
        pass
    
    class D(B, C): # Multiple: D inherits from B and C
        pass