Diamond problem 00:00:00 Make a prediction You will distinguish a duplicate shared base from an ambiguous override and trace how a cooperative hierarchy reaches a base once. Left and Right both inherit Base. Child explicitly invokes both parent initializers, and both parents call Base. How many times can Base initialize? Twice in that non-cooperative design. In a coherent Python hierarchy each initializer delegates once to the next class in the method resolution order, so the shared base need only run once. 00:00:35 The hill with four houses Two footpaths fork up a hill, pass two houses and rejoin at a bakery. Visiting the bakery at the end of each separate route means making the same delivery twice. Make one itinerary that covers both houses and the shared stop once. The diamond is the rejoined path. A resolution order gives a route; cooperative calls must actually follow it for shared work to happen once. Draw it and you see the name. Animal on top, Flying and Walking below, and Bat at the bottom with both as parents. Two ways lead from Bat to Animal, and the picture is a diamond. Without a rule the program is ambiguous. With a silent rule it may do something nobody chose. With two copies of the state, one object holds two truths: flying uses one energy, walking uses the other, and the bat never gets tired. 00:01:33 Visit the shared base only once A child inherits Left and Right, and both inherit Base. Calling both parent initializers by name can initialize Base twice. In a cooperative Python hierarchy, each initializer calls super once and uses compatible arguments. The method resolution order determines which class comes next. Trace the actual order for the child and count base initialization. A single visit shows why super means next in the order, not always my direct parent. Log an initialization event into a list as construction passes through the two branches. In a cooperative method chain each implementation delegates once. Observe how many calls reach the shared base; calling both parents manually can visit it twice even when the object has only one logical identity. 00:02:22 Distinguish lookup from storage In C++, a diamond with two ordinary inheritance paths can create two base subobjects. Virtual inheritance can share that base subobject. It does not decide which of two conflicting overrides should represent the child's behaviour. The most derived class may still need to resolve the method explicitly. Store ten in the left base subobject and twenty in the right. Ordinary C++ inheritance allows both values to exist. Virtual inheritance can give both paths the same base storage, yet two branches can still define competing methods. Storage sharing and method selection are separate questions. Sharing a C++ virtual base can address duplicate base state. It does not choose between two contradictory behaviours supplied by different branches. If independent capabilities do not need shared implementation, keep the types as interfaces or compose collaborators instead of building a stateful diamond. C++ iostream. istream and ostream derive from ios virtually, and iostream derives from both: one shared ios, as cppreference explains. The best known diamond in production. Python. The tutorial says that all cases of multiple inheritance show a diamond, because every class inherits from object. The dynamic order makes sure that each parent is called only once. 00:03:54 Try an interview follow-up You design a class library. How do you keep users out of diamonds? I reduce the need for diamonds by keeping common interfaces small and placing state in explicitly owned components. Before adding a default method, I check existing classes implementing both branches. Where a C plus plus design really needs a single shared base subobject, I decide and test the virtual inheritance arrangement explicitly. A copier inherits scanner and printer behavior, and both derive from a device with a serial number. Does this copier contain one device identity or two? Trace which serial number each path sees under your language's inheritance rules. State whether you need one shared ancestor, an explicit override, or delegated components. 00:04:48 What to take with you Two questions: which method, and how many copies. C++ virtual inheritance answers the second. The first still needs an override when both branches have one. Python puts the classes in one line (C3) and super walks it, so the ancestor runs once. The order of parents is then part of the meaning.