Skip to main content
Engineering LibreTexts

13.5: Multiple Inheritance and Mixin Classes

  • Page ID
    117610
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\ket}[1]{\left| #1 \right>}\)
    \(\newcommand{\bra}[1]{\left< #1 \right|}\)
    \(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
    \(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
    \(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
    \(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
    \(\newcommand{\expect}[1]{\langle #1 \rangle}\)
    \(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)
    Learning Objectives

    By the end of this section you should be able to

    • Construct a class that inherits from multiple superclasses.
    • Identify the diamond problem within multiple inheritance.
    • Construct a mixin class to add functionality to a subclass.

    Multiple inheritance basics

    Multiple inheritance is a type of inheritance in which one class inherits from multiple classes. A class inherited from multiple classes has all superclasses listed in the class definition inheritance list. Ex: class SubClass(SuperClass_1, SuperClass_2).

    Checkpoint: Multiple inheritance organization
    Concepts in Practice: Implementing multiple inheritance

    Consider the program:

        class A:
          def __init__(self, a_attr=2):
            self.a_attr = a_attr
    
        class B:
          def __init__(self, b_attr=4):
            self.b_attr = b_attr
    
        class C(A, B):
          def __init__(self, a_attr=5, b_attr=10, c_attr=20):
            A.__init__(self, a_attr)
            B.__init__(self, b_attr)
            self.c_attr = c_attr
    
        b_inst = B(2)
        c_inst = C(1, 2)
    
    Concepts in Practice \(\PageIndex{1}\): Implementing multiple inheritance

    What is the value of c_inst.a_attr?

    1. 1
    2. 5
    3. Error
    Answer

    a. 1 is the argument passed to C's __init__() for a_attr.

    Concepts in Practice \(\PageIndex{2}\): Implementing multiple inheritance

    What is the value of c_inst.c_attr?

    1. 2
    2. 20
    3. Error
    Answer

    b. An argument is not passed to C's __init__() for c_attr, so c_attr is assigned with the default value, 20 .

    Concepts in Practice \(\PageIndex{3}\): Implementing multiple inheritance

    What is the value of b_inst.a_attr?

    1. 2
    2. 4
    3. Error
    Answer

    a. 2 maps to b_attr in B' s __init__().

    The diamond problem and mixin classes

    Multiple inheritance should be implemented with care. The diamond problem occurs when a class inherits from multiple classes that share a common superclass. Ex: Dessert and BakedGood both inherit from Food, and ApplePie inherits from Dessert and BakedGood.

    3d40b48aeed34279eb1ef89cb44abbe640f81037
    Figure 13.2 The diamond problem. If both Dessert and Baked Good override a Food method, the overridden Food method that Apple Pie inherits is ambiguous. Thus, diamond shaped inheritance should be avoided.

    Mixin classes promote modularity and can remove the diamond problem. A mixin class:

    • Has no superclass
    • Has attributes and methods to be added to a subclass
    • Isn't instantiated (Ex: Given MyMixin class, my_inst = MyMixin() should never be used.)

    Mixin classes are used in multiple inheritance to add functionality to a subclass without adding inheritance concerns.

    Concepts in Practice: Creating a mixin class

    The following code isn't correct, as not all plants are carnivorous. Follow the programmer through the programmer's edits to improve the program. Note: Rose, Pitcher, and VenusFlyTrap represent plants that all photosynthesize. Pitcher and VenusFlyTrap represent plants that are also carnivorous and can eat.

    A pass statement is used in Python to indicate that code is to be written later and prevents certain errors that would result if no code was written or a comment was used instead.

        class Plant:
          def photosynth(self):
            print("Photosynthesizing")
    
          def eat(self):
            print("Eating")
    
        class Rose(Plant):
          pass
    
        class Pitcher(Plant):
          pass
        
        class VenusFlyTrap(Plant):
          pass
    
    Concepts in Practice \(\PageIndex{4}\): Creating a mixin class

    Which edit is appropriate?

    1. Move eat() to a different class.
    2. Remove Plant as a superclass.
    Answer

    a. eat() only applies to carnivorous plants, which Pitcher and VenusFlyTrap represent.

    Concepts in Practice \(\PageIndex{5}\): Creating a mixin class

    The programmer edits the code as follows. How can the program be improved?

    class Plant:
      def photosynth(self):
        print("Photosynthesizing")
    
    class Rose(Plant):
      pass
    
    class Pitcher(Plant):
      def eat(self):
        print("Eating")
    
    class VenusFlyTrap(Plant)
      def eat(self):
        print("Eating")
    
    1. Move photosynth() into Rose.
    2. Remove redundancy of eat().
    Answer

    b. eat() can be moved into a mixin class included in Pitcher's and VenusFlyTrap's inheritance list.

    Concepts in Practice \(\PageIndex{6}\): Creating a mixin class

    The programmer edits the code to create a mixin class containing eat(). Which class name, replacing X, is most appropriate for the mixin?

    class Plant:
      def photosynth(self):
        print("Photosynthesizing")
    
    class X:
      def eat(self):
        print("Eating")
    
    class Rose(Plant):
      pass
    
    class Pitcher(Plant, X):
      pass
    
    class VenusFlyTrap(Plant, X)
      pass
    
    1. CarnivClass
    2. CarnivMixin
    Answer
    1. b. Good practice is to have a mixin class name end in "Mixin" to identify the class's purpose. The final implementation is:
    class Plant:
      def photosynth(self):
        print("Photosynthesizing")
    
    class CarnivMixin:
      def eat(self):
        print("Eating")
    
    class Rose(Plant):
      pass
    
    class Pitcher(Plant, CarnivMixin):
      pass
    
    class VenusFlyTrap(Plant, CarnivMixin)
      pass
    Try It: Fixing the diamond problem with a mixin class

    The program below represents multiple inheritance and has the diamond problem. Edit the program to use a mixin class, OnlineMixin. OnlineMixin:

    • Replaces OnlineShop.
    • Is a superclass of FoodFast.
    • Has one method, process_online(), which prints "Connecting to server" and is called by FoodFast's process_order().

    The output should match:

        Processing meal order
        Connecting to server
        Enjoy your FoodFast order
    Interactive Code
    class Business:
        def process_order(self):
            print("Processing order")
    
    class Restaurant(Business):
        def process_order(self):
            print("Processing meal order")
    
    class OnlineShop(Business): # Change to mixin class
        def process_order(self):
            print("Processing online order")
    
    class FoodFast(Restaurant, OnlineShop):
        def process_order(self):
            super().process_order()
            # Call mixin method
            print("Enjoy your FoodFast order")
    
    franchise_1 = FoodFast()
    franchise_1.process_order()
    
    
     
    Answer

    class Business:
    def process_order(self):
    print("Processing order")

    class Restaurant(Business):
    def process_order(self):
    print("Processing meal order")

    class OnlineMixin():
    def process_online(self):
    print("Connecting to server")

    class FoodFast(Restaurant, OnlineMixin):
    def process_order(self):
    super().process_order()
    self.process_online()
    print("Enjoy your FoodFast order")

    franchise_1 = FoodFast()
    franchise_1.process_order()


    This page titled 13.5: Multiple Inheritance and Mixin Classes was last modified on Fri, 24 Jul 2026 08:49:30 GMT and is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.