0

Solidity: Constructors and Inheritance.

Constructors play a similar role in smart contracts as they do in regular object oriented programing; they are used to create the instance of an object and to execute any custom logic required for object initialization. Constructors are executed upon contact creation and the contract is deployed to the blockchain after the constructor has been executed.

There can be only one constructor per smart contract, they can be either public or internal (if internal, then the smart contract has to be marked as abstract). Constructors are usually utilized for initialization purposes and they are optional, if a constructor is not set, then a default constructor will be assumed, that is: constructor() public {}

Inheritance, in general, allows an object to inherit certain variables, functions or behaviors from another object. In Solidity, it let’s you create an smart contract based on another contract. Inheritance can be single or multilevel and all contracts involved in the inheritance chain are combined into a single smart contract when deployed into the blockchain.



Constructor

Let’s consider the following example, here the constructor is used to initialize the flag variable, do note that constructors can also receive parameters and, in this case, it is receiving a boolean parameter. This means that right before deploying the contract, the constructor will be executed and the value of flag will be set to whatever value is sent as parameter whoever is deploying the contract.

 contract fooContract {
    bool flag;

    constructor (bool _flag) {
        flag = _flag;
    }
}


Inheritance

As mentioned above, inheritance allows programmers to create objects based on other objects and inherit their behavior. In the below example we can see that the contract fooContract inherits from the contract fooOwner, this is specified in the declaration of fooContract by the use of the is statement followed by the name of the contract that will be inherited from (contract fooContract is fooOwned means that fooContract inherits from fooOwned).

contract fooOwned {
    address owner;
    constructor() {
        owner = msg.sender;
    }
}

contract fooContract is fooOwned {
    function fooFunction() public view {
        if (msg.sender == owner) {
            // Logic
        }
    }
}

Constructors are an important part of inheritance as they can be used for initialization purposes. When a smart contract inherits from another smart contract, the constructor o of the second can be invoked either directly of indirectly.

In the below example, the constructor of fooOwned is directly invoked:

contract fooOwned {
    uint256 val;
    constructor(uint256 _val) { 
        val = _val; 
    }
}

contract fooContract is fooOwned (7) {
    // Logic
}

In the below example, the constructor of fooOwned is indirectly invoked:

contract fooOwned {
    uint256 val;
    constructor(uint256 _val) { 
        val = _val; 
    }
}

contract fooContract is fooOwned {
    constructor(uint256 _val) fooOwned (_val) {
        // Logic
    }
}


Abstract Contracts

A contract is abstract when at least one of the define function does not have an implementation, meaning that only the function signature is specified. In the below example, the contract fooContract is marked as abstract because function fooAbastractFunction lacks implementation (and thus it’s marked as virtual). Notice that, unlike fooAbastractFunction the function fooFunction does have an implementation.

abstract contract fooContract {
    function fooAbstractFunction() public virtual returns (bool) ;
    
    function fooFunction() public view {
        // Logic
    }
}


Interfaces

Interfaces are similar to abstract contracts in the sense that functions lack their implementation, the difference is that on interfaces none of the declared functions can have an implementation unlike abstract contracts).

interface fooInterface {
    function fooFunction1() external returns (bool) ;
    function fooFunction2() external returns (uint256 _val) ;
}

contract fooContract is fooInterface {
    function fooFunction1() override public returns (bool) {
        // Logic
    }
    
    function fooFunction2() override public returns (uint256 _val) {
        // Logic
    }
}


<< Prev

Next >>