DEV Community

Cover image for Inheritance, Override and Virtual In Solidity.
Bright-Lumen
Bright-Lumen

Posted on

Inheritance, Override and Virtual In Solidity.

Since I started learning solidity, I often compare the syntax of Solidity to that of typescript, I use the concept of typescript to understand concepts in Solidity. Inheritance in Solidity programming allows a new contract to be based on an existing contract, known as the parent or base contract. The child contract can inherit all the properties, variables, and functions without necessarily trying the redefine the parent contract. Inheritance in solidity reduces code repetition and improves code maintainability.
How does it then work?
As typescripts make use of the extends keyword for an inheritance, so also there is a keyword for inheriting the property of another contract with solidity.

We will create a file called, addNumber.sol and insert these codes inside the file.

SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;

contract AddNumber {
  uint256 number;

function AddNewNumber (uint256 _number) public {
   number = _number;
}

function retrieveNewNumber () public view returns (uint256){
   return number;
}


}

Enter fullscreen mode Exit fullscreen mode

In the above-written code, we have been able to write a simple solidity contract. First, we declared the license identifier of our code which enables the exchange of software package meta-data between different tools and modifications. Then the next line specifies the version of the solidity compiler we are going to use to compile codes.

So we created a contract of AddNumber and declared a uint256 variable, which is initialized to Zero. Then a function to store and retrieve the variables stored.

To implement inheritance, we will go ahead to create a file called addExtraNumbers.sol(child contract) which will inherit both variables and functions of the parent contract(AddNumber).

SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;

// this contract will inherit AddNumber contract function

import "./addNumber.sol";

contract AddExtraNumbers is AddNumber {
 function AddNewNumber (uint256 _number) public {
   number = _number;
}


// we have been able to automatically access the 
// _**AddNewNumber**_ function.

}

Enter fullscreen mode Exit fullscreen mode

In the above code block, we created AddExtraNumbers contract and imported AddNumber contract using the import keyword.

With the is keyword, it signifies that, AddExtraNumbers should inherit the properties and function of AddNumber.

Congratulations you have been able to implement Inheritance in Solidity.

But what if we want to make modifications or edit the inherited functions inside the contract? Say we want to add five to every new number that will be added.

contract AddExtraNumbers is AddNumber {
 function AddNewNumber (uint256 _number) public {
   number = _number + 5;
 }
}
Enter fullscreen mode Exit fullscreen mode

If we compile addExtraNumbers.sol we will encounter errors relating to:

TypeError: Overriding function is missing "override" specifier.
 --> contracts/addExtraNumbers.sol:5:4:
  |
5 |    function AddNewNumber(uint256 _number) public   {
  |    ^ (Relevant source part starts here and spans across multiple lines).
Note: Overridden function is here:
  --> contracts/addNumber.sol:14:5:
   |
14 |     function AddNewNumber(uint256 _number) public   {
   |     ^ (Relevant source part starts here and spans across multiple lines).

Enter fullscreen mode Exit fullscreen mode

This indicates that we are making attempts to modify the function we inherited without the knowledge of the parent contract, more or less overriding the children of the parent contract. To avoid these errors, we need to add new keywords of override and virtual. The virtual keyword will be added to the function in the parent contract to specify that the function can be specified.

//_contracts/addNumber.sol_:


contract AddNumber {
  uint256 number;

function AddNewNumber (uint256 _number) public virtual {
   number = _number;
}

function retrieveNewNumber () public view returns (uint256){
   return number;
}

}

Enter fullscreen mode Exit fullscreen mode

Adding virtual keyword allows any inherited contracts to access and modify the function. If we compile addExtraNumbers.sol we will still get the same error. Remember the override keyword earlier stated, we will need to add that to our inherited contract function.

//_contracts/addExtraNumbers.sol_;

 contract AddExtraNumbers is AddNumber  {
 function AddNewNumber (uint256 _number) public override {
   number = _number + 5;
 }
}

Enter fullscreen mode Exit fullscreen mode

Adding the override keyword to the inherited function will inform the compiler that it has the right to override the inherited function with a virtual keyword.

To a large extent, Inheritance, virtual and override all works together,

I hope you understand this walkthrough please leave your thoughts in the comment section, let's interact.

Top comments (0)