Solidity tutorial 6 "Abstract Contracts"

Abstract Contracts

Contract functions can lack an implementation as in the following example
(note that the function declaration header is terminated by ;)

contract feline { 
    function utterance() returns (bytes32);

Such contracts cannot be compiled (even if they contain implemented functions alongside non-implemented functions), but they can be used as base contracts.

contract Cat is feline {  
    function utterance() returns (bytes32) { 
        return "miaow";

If a contract inherits from an abstract contract and does not implement all non-implemented functions by overriding, it will itself be abstract.

Visibility Specifiers

Functions and storage variables can be specified as being public, internal or private, where the default for functions is public and internal for storage variables.
In addition, functions can also be specified as external.

External functions are part of the contract interface and they can be called from other contracts and via transactions.
An external function f cannot be called internally.
f() does not work, but this.f() works.

Furthermore, all function parameters are immutable.

  • Public
    Public functions are part of the contract interface and can be either called internally or via messages.
    For public storage variables, an automatic accessor function (see below) is generated.
  • Inherited
    Those functions and storage variables can only be accessed internally.
  • Private
    Private functions and storage variables are only visible for the contract they are defined in and not in derived contracts.
contract c {function f(uint a) private returns (uint b) {
     return a + 1; 

  function setData(uint a) inherited {
        data = a;
        uint public data;

Other contracts can call to retrieve the value of data in storage, but are not able to call f.
Contracts derived from c can call setData to alter the value of data (but only in their own storage).

Accessor Functions

The compiler automatically creates accessor functions for all public state variables.

The contract given below will have a function called data that does not take any arguments and returns a uint, the value of the state variable data.
The initialization of state variables can be done at declaration.

contract test { 
    uint public data = 42;

The next example is a bit more complex:

contract complex {  
    struct Data { 
        uint a; 
        bytes3 b; 
        mapping(uint => uint) map;
  mapping(uint => mapping(bool => Data[])) public data;

It will generate a function of the following form:

function data(uint arg1, bool arg2, uint arg3) returns (uint a, bytes3 b){
      a = data[arg1][arg2][arg3].a;
      b = data[arg1][arg2][arg3].b; 

Note that the mapping in the struct is omitted because there is no good way to provide the key for the mapping.

Next Lesson

Summary of Lessons



Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?