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 {}
0

2 Answers 2

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

2
  • 3
    For completeness link to the solidity documentation solidity.readthedocs.io/en/develop/…
    – Ismael
    Jul 14, 2017 at 4: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, 2021 at 1: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.