17

Hello to everyone. I am looking at multiple inheritance in Solidity. I came across this example within their documentation, but it is not understandable enought for me.

Can someone explane this sentence in more detail:

The reason for this is that C requests X to override A (by specifying A, X in this order), but A itself requests to override X, which is a contradiction that cannot be resolved.

My question is why C requests X to override A, i.e. what this order of inheritance means exactly?

    pragma solidity ^0.4.0;

     contract X {}
     contract A is X {}
     contract C is A, X {}
Sanchit
  • 3,482
  • 1
  • 12
  • 29
branko terzic
  • 473
  • 4
  • 12

2 Answers2

12

After research I found:

With multiple inheritance, there is an issue caused by the Diamond Problem. Solidity solves this problem like Python.

Here are two links describing this problem in detail: https://docs.python.org/2/tutorial/classes.html#instance-objects https://www.python.org/download/releases/2.3/mro/

So, the order of inheritance is important in order to avoid the Diamond problem. Order should be: from classes on the top level of inheritance to the classes on lower levels

branko terzic
  • 473
  • 4
  • 12
  • 3
    For completeness link to the solidity documentation https://solidity.readthedocs.io/en/develop/contracts.html#multiple-inheritance-and-linearization – Ismael Jul 14 '17 at 04:17
  • 3
    Just a small observation to avoid pitfalls: in Python the base classes are searched from left to right. In Solidity is right to left. – shamisen Nov 10 '21 at 01:19
7

Best explained in the solidity docs on Multiple Inheritance and Linearization:

You have to list the direct base contracts in the order from “most base-like” to “most derived”. Note that this order is the reverse of the one used in Python.

Another simplifying way to explain this is that when a function is called that is defined multiple times in different contracts, the given bases are searched from right to left (left to right in Python) in a depth-first manner, stopping at the first match. If a base contract has already been searched, it is skipped.

Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143