Python Multiple Inheritance

1) Simple Multiple Inheritance

Create:

class A:
    def greet(self): print("Hello from A")
class B:
    def greet2(self): print("Hello from B")

Make class C(A, B) and call both methods.


2) Override Same Method

Define both A and B with a method show(), then inherit in C(A, B) and call show() — observe which one runs (MRO).


3) Constructor Inheritance

Define:

class A:
    def __init__(self): print("Init A")
class B:
    def __init__(self): print("Init B")

Make C(A, B) and instantiate it; observe what prints.


4) Use super() with Multiple Inheritance

Modify 3 so C.__init__() calls super().__init__() and observe order.


5) Inherit Attributes

Give class A an attribute x = 10 and class B y = 20. Make C(A, B) and print both.


6) Method from One Parent

In class A define a_method(), in class B define b_method(). Inherit in C(A, B) and call both.


7) Mixed Parents

Make class A with arithmetic methods, class B with string methods. Class C(A, B) should combine both.


8) Diamond Inheritance Pattern

Create:

Give all a msg() method and print from D.


9) Call Specific Parent Method

In multiple inheritance class C, call only parent B’s method even if overridden.


10) Shared Attribute Conflict

Both parents have value = 5 and value = 10 respectively. In child, print resolved value.


11) Inherit Constructors With Parameters

Make:

Combine in child so both initialize.


12) Combine Method Results

Parent A returns 10, B returns 20. Child method adds them.


13) Parent Method Calls

In class C(A, B), override a method and inside it call both parents’ versions explicitly.


14) Multiple Inheritance With Utility Classes

Make a Logger class, an Entity class, and a child User(Logger, Entity) that uses both.


15) Method Resolution Order (MRO) Check

Print the MRO of class C(A, B, object) using:


16) Parent With Default Values

Parent A initializes name, parent B initializes age. Child inherits both attributes and prints them.


17) Conflict Resolution With super()

Parents override same method; child uses super() to unify behavior.


18) Inherit Behavior & Add New

Create parents Mover and Speaker. Child Robot(Mover, Speaker) then also adds unique method.


19) Simple Multi-Level + Multiple Inherit

Create class X, Y, Z. Make W(X, Y) and then P(W, Z). Call a method from all.


20) Validate Combined Functionality

Create parent Validator, parent Formatter, child Field(Validator, Formatter) and demonstrate calling both parent functionalities.


📝 Concepts Practiced

Concept
Why It Matters

Multiple parent classes

Combining behavior

MRO (Method Resolution Order)

Python’s inheritance lookup

Constructors with multiple parents

Handling initialization

Overriding & calling parent methods

Managing conflicts

super() usage

Simplifying multi-inherit calls


💡 Starter Template


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated