Ethernaut-Fallout — A few words about constructor

Eszymi
Coinmonks
5 min readAug 19, 2022

--

Constructor is a special function declared using constructor keyword. It’s not an obligatory function which is called just one, and never again. The moment of call is when our smart contract is created. Constructor gives us a possibility to initialize state variable then. After a constructor code executed, the final code is deployed to blockchain. This code include public functions and code reachable through public functions. Constructor code or any internal method used only by constructor are not included in final code.

Creating a constructor

Creating constructor function is easy. Let’s see.

Example of using of constructor.

When we are declaring constructor, we use only this keyword — no name of this constructor or something other. Also, we have to add access modifier (I’ll be talking about them later). The constructor could be empty or have any input variable (like in our example).

Every contract can have only one constructor. Writing more constructor give us an error during the compiling. But what is interesting, if we don’t define constructor, in the contract will be a default constructor.

Default constructor

Constructor in Inheritance

More fun is when we want to create a contract which will inherit from other contract, which require parameters to its constructor. We can give no parameters and our contract will has to be an abstract contract (this also will be explained later). Or we can pass every needed parameters. And here we again have two options:

  1. Direct Initialization: Our contract to initialize parent’s constructor use hard code method like we see in example.
Example of direct initialization

When we define that ChildContract inherits from ParentContract we immediately pass the value for ParentContract’s constructor.

2. Indirect Initialization: Initializing of ParentContract’s variable by its constructor is going inside ChildContract.

Example of indirect initialization

By this method, we can decide of parameters for ParentContract when we deploy the ChildContract. This method is more flexible.

Initializing of ParentContract, in the same time, by the direct and indirect ways is not allowed.

Visibility for functions and state variables

How was said, when we want to declare constructor we have to add access modifier. A constructor can be either public or internal.

But why have we use this words, and what are they? Solidity knows two kinds of function calls: internal ones that do not create an actual EVM call (also called a “message call”) and external ones that do. Because of that, there are four types of visibility for functions and state variables.

  • External : External functions are part of the contract interface, which means they can be called from other contracts and via transactions. An external function f cannot be called internally (i.e. f() does not work, but this.f() works). External functions are sometimes more efficient when they receive large arrays of data, because the data is not copied from calldata to memory.
  • Public : Public functions are part of the contract interface and can be either called internally or via messages. For public state variables, an automatic getter function is generated. It’s mean the compiler generate a function called like the variable that does not take any arguments and returns the value of the state of this variable.
  • Internal : Those functions and state variables can only be accessed internally (i.e. from within the current contract or contracts deriving from it), without using this.
  • Private : Private functions and state variables are only visible for the contract they are defined in, and not in derived contracts.

Information from this paragraph come from solidity docs.

Behave of public constructor is exactly like this described on the beginning of this post. When a constructor is internal, the whole contract is abstract.

Abstract contract

I like how is it described in tutorialspoint.

Abstract Contract is one which contains at least one function without any implementation. Such a contract is used as a base contract. Generally, an abstract contract contains both implemented as well as abstract functions. Derived contract will implement the abstract function and use the existing functions as and when required.

In case, a derived contract is not implementing the abstract function then this derived contract will be marked as abstract.

Abstract Contracts cannot be compiled. They can however be used as base contracts from which other contracts can inherit from.

Let’s look on the example.

Using of abstract contract

AbstractContract contains function without body. We know what it needs on the input and what will be on the output. We know also that this function is public. But nothing more except magic word ‘virtual’. This word informs Solidity that this function will be overrides by other, with the same name and with the ‘override’ modifier.

The abstract and virtual keywords signify that the associated code will be implemented or overridden elsewhere in the code. The main difference is that abstract applies to contracts and virtual applies to functions.

I hope you find this post useful. If you have any idea, how could I make my posts better, let my know. I am always ready to learn. You can connect with me on LinkedIn and Telegram.

If you would like to talk with me about this or any other topic I wrote, feel free. I’m open to conversation.

Happy learning!

New to trading? Try crypto trading bots or copy trading

--

--