Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 4 of 10 · Object-Oriented Programming (OOP)
Interview question

What are the different types of inheritance? Inheritance के कितने types हैं?

Answer
TypeDescriptionExample
SingleOne parent, one childclass Dog(Animal)
MultipleMultiple parents, one childclass Car(Vehicle, Fuel)
MultilevelChain: A->B->Cclass Puppy(Dog(Animal))
HierarchicalOne parent, multiple childrenDog, Cat, Bird from Animal
HybridCombination of typesMix of above types
# Single Inheritance
class Animal:
    pass

class Dog(Animal):
    pass

# Multiple Inheritance
class Vehicle:
    pass

class Fuel:
    pass

class Car(Vehicle, Fuel):
    pass

# Multilevel Inheritance
class Animal:
    pass

class Dog(Animal):
    pass

class Puppy(Dog):
    pass

# Hierarchical Inheritance
class Animal:
    pass

class Dog(Animal):
    pass

class Cat(Animal):
    pass

class Bird(Animal):
    pass

# Diamond Problem (with Multiple Inheritance)
class A:
    def method(self):
        print('A method')

class B(A):
    def method(self):
        print('B method')

class C(A):
    def method(self):
        print('C method')

class D(B, C):
    pass

d = D()
d.method()  # B method (C3 Linearization)
print(D.__mro__)  # Shows MRO
Single: एक parent, एक child
Multiple: Multiple parents, एक child
Multilevel: Chain A->B->C
Hierarchical: एक parent, multiple children
Hybrid: Combination

Was this answer clear?