Skip to main content
5 of 5
edited tags; edited title
Achala Dissanayake
  • 5.8k
  • 15
  • 29
  • 38

What is the execution order of Inherited constructors in solidity?

I'm reading the section on inherited constructors on the documentation here.

The example given confuses me than understanding the concept, and it doesn't explain the most important part, the execution order.

To summarize the documentation, it says in the following example:

pragma solidity ^0.4.0;

contract Base {
  uint x;
  function Base(uint _x) { x = _x; }
}


contract Derived1 is Base(7) {
  function Derived1(uint _y) {
  }
}

contract Derived2 is Base{
  function Derived2(uint _y) Base(_y * _y) {
  }
}

Derived1 inherits Base(7) constructor, and Derived2 uses a modifier-like syntax of Base(_y * _y).

But what it DOESN'T explain is how they are actually executed. Let's take an example

contract Base {
  public uint x;
  function Base(uint _x) { x = _x; }
}


contract Derived1 is Base(7) {
  function Derived1(uint _y) {
    x = _y;
  }
}

contract Derived2 is Base {
  function Derived2(uint _y) Base(_y * _y) {
    x = _y;
  }
}

does the x = _y in each inheritance get executed BEFORE the base constructor? or after?

Normally in any object oriented oriented programming you use notations like super() to explicitly state how the parent constructor will be executed.

In case of objective-c

- (void) init {
  [super init];
  // do something
}

and

- (void) init {
  // do something
  [super init];
}

make a huge difference since the execution order is different. And the //do something part may even utilize the result from [super init].

So how does this thing work in Solidity? If you can, please share the source for the explanation as well. I can't find this on the documentation so I don't know where else I can find this.

Vlad
  • 501
  • 1
  • 5
  • 8