Smart Contract Inheritance

Adeosun Elijah Oluwaseun
EthereumNigeria
Published in
2 min readMar 26, 2024

What came to your mind when you heard about “Smart contract inheritance”? It is understandable for your mind to wander about or even being thrown off balance. Yet, it is easy and fun to understand how to compose smart contracts.

Although, my intention is not to introduce smart contract to you since you might have read a thing or two about it. For instance, you must have heard that “ A smart contract is a computer program that runs on a blockchain and executes the terms of an agreement or contract”. So, today, I will be informing you about “smart contract inheritance.”

WHAT IS SMART CONTRACT INHERITANCE?

I can describe it as joining a new smart contract with an existing one. When a smart contract developer is about to write a new smart contract, he can decide to import previous contract into a new one so that the new contract will inherit the features of the old contract. To understand this better, I will be writing two contracts and then make the last contract to inherit the new.

The name of this contract is “SimpleStorage”

SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage{
uint256 myNumber;
mapping(string =>uint256) public nameTomyNumber;
struct People{
uint256[ ] public myNumberList
People [ ] public people;
fuunction store(uint256 myNumber) public{
myNumber = _myNumber;
}
function retrieve ( ) public view returns (uint256) {
return myNumber;
}
function addPerson(string memory name, uint256 _myNumber ){
people.push(people(_mynumber, name: _name));
}
}
}

The above is a simple way to write a smart contract that can perform many functions including the store and retrieve functions and the name of the contract is “SimpleStorage”.

Now, if you want to write another smart contract that will have the features of the old one and additional codes, you can do the following;

SPDX-License_Identifier: MIT
pragma solidity ^0.8.0;
import "/.SimpleStorage.sol";
contract ExtraStorage is SimpleStorage

This will automatically import the codes from the first contract named “SimpleStorage” into the new contract name “ExtraStorage” and the code “import “/.SimpleStorage.sol”” will make the new smart Contract inherit the features in the old contract named “SimpleStorage”.

In the next article, I will be explaining the basic features of a web3 smart Contract. Until then,

HAPPY WEEKEND!

--

--