Junior — Middle
Explain the concept of multiple inheritance and its features in object-oriented programming.
sobes.tech AI
Answer from AI
Multiple inheritance is the ability of a class to inherit properties and methods from more than one parent class at the same time. This allows combining functionalities of different classes into one.
Features of multiple inheritance:
- Enables creating more flexible and reusable components.
- Can lead to conflicts if parent classes have methods or properties with the same names (the "diamond problem").
- In some languages (like Java), multiple class inheritance is not supported, but interfaces can address some of these issues.
Example in Python:
class A:
def method(self):
print("Method from A")
class B:
def method(self):
print("Method from B")
class C(A, B):
pass
c = C()
c.method() # Will print "Method from A" due to inheritance order