Skip to main content
1 of 5
Vlad
  • 501
  • 1
  • 5
  • 8

Inherited constructors execution order

I'm reading the section on inherited constructors on the documentation: http://solidity.readthedocs.io/en/develop/contracts.html#arguments-for-base-constructors

The example it gives makes me more confused 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 Derived(uint _y) {
  }
}

contract Derived2 {
  function Derived(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 Derived(uint _y) {
    x = _y;
  }
}

contract Derived2 {
  function Derived(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