4

Having a simple contract structure like this:

pragma solidity ^0.4.8;

contract A {
  uint public a1;
}

contract B is A {
  uint8 public b1;
}

I can compile it with:

> solc --combined-json "abi,bin" inheritance.sol > ./out/test_output.json

To get the following:

{
  "contracts": {
    "A": {
      "abi": "[{\"constant\":true,\"inputs\":[],\"name\":\"a1\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"}]",
      "bin": "6060604052346000575b608e806100176000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063119552a114603c575b6000565b346000576046605c565b6040518082815260200191505060405180910390f35b600054815600a165627a7a723058205a60899c35d4ed35d09808494352b2a1170b28969925c2aa7e86a45128a569c60029"
    },
    "B": {
      "abi": "[{\"constant\":true,\"inputs\":[],\"name\":\"a1\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"b1\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"type\":\"function\"}]",
      "bin": "606060405234610000575b60d1806100186000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063119552a1146046578063ee495002146066575b6000565b346000576050608c565b6040518082815260200191505060405180910390f35b3460005760706092565b604051808260ff1660ff16815260200191505060405180910390f35b60005481565b600160009054906101000a900460ff16815600a165627a7a72305820c2b3dacd4eb8217e675d9a1ab699d05e0e267d2f98d09a8a81a0b1251e8adfbe0029"
    }
  },
  "version": "0.4.8+commit.60cc1668.Darwin.appleclang"
}

Adding a constructor to the A contract, I am receiving empty bin section for B. Source code:

pragma solidity ^0.4.8;

contract A {
  uint public a1;

  function A (address _a) {
  }
}

contract B is A {

  uint8 public b1;
}

Resulting json:

{
  "contracts": {
    "A": {
      "abi": "[{\"constant\":true,\"inputs\":[],\"name\":\"a1\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"_a\",\"type\":\"address\"}],\"payable\":false,\"type\":\"constructor\"}]",
      "bin": "6060604052346000576040516020806100c4833981016040528080519060200190919050505b5b505b608e806100366000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063119552a114603c575b6000565b346000576046605c565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820b498791e10bedac88be35dd16a0733002f88d24b34bb81fd03d029cda7db4fa00029"
    },
    "B": {
      "abi": "[{\"constant\":true,\"inputs\":[],\"name\":\"a1\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"b1\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"type\":\"function\"}]",
      "bin": ""
    }
  },
  "version": "0.4.8+commit.60cc1668.Darwin.appleclang"
}

Parameterless constructor in A doesn't interfere with bin section of B.

1
  • 2
    A point of note - you also see this behaviour if you have defined an interface in Solidity, but one of your method implementations doesn't match the original interface definitions - e.g. if it's missing a parameter in its definition. Oct 6, 2017 at 8:47

1 Answer 1

2

You cannot construct A if you do not give it a parameter. This will give you a binary for B:

contract A {
  uint public a1;

  function A (address _a) {
  }
}

contract B is A {
  uint8 public b1;

  function B(address _a) A(_a) {
  }
}

This too:

contract A {
  uint public a1;

  function A (address _a) {
  }
}

contract B is A {
  uint8 public b1;

  function B() A(0x0011223344556677889900112233445566778899) {
  }
}

Your Answer

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

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