Empowering Your Ethereum Contracts with Solidity Inheritance

Our aim in this article is to explore Solidity inheritance and how it can be utilized for making effective and efficient smart contracts.

Solidity allows smart contracts to inherit from each other, allowing multiple contracts to become one.

In contract terms, the contract whose features other contracts inherit is called a base contract, while the contract which inherits the features is called a derived contract.



What is Solidity Inheritance?

Inheritance is a programming concept that allows a new class, called the child class, to inherit properties and methods from an existing class, called the parent class.

In Solidity, contracts can be used as classes, and inheritance allows new contracts to inherit properties and methods from parent contracts.

Derivative contracts can access all non-private members, including state variables and internal methods. However, it is not permitted to use this.

It is permissible to override functions as long as their signatures remain the same. Compilation will fail if the output parameters are different.

A super contract’s function can be called with a super keyword or with a super contract’s name.

When multiple inheritances are present, function calls that use the super keyword invoke the majority of the functions in the derived contract.


Solidity Inheritance Types

Solidity provides different types of inheritance. Follow are its types:

TypesOverview
Single inheritanceA contract inherits from only one parent contract.
Multiple inheritanceA contract inherits from multiple parent contracts.
Hierarchical inheritanceA contract can be both a child and a parent contract, creating a hierarchical inheritance structure.
Linear inheritanceA type of multiple inheritance where the inheritance graph is a linear chain, with each contract only inheriting from one parent contract.
Hybrid inheritanceA combination of linear and hierarchical inheritance, where contracts inherit from multiple parent contracts, but also form a hierarchy.

Single Inheritance

Solidity single inheritance is a process of inheriting one derived contract from a parent contract in order to have access to its state variables and methods.

The example below shows the working phenomenon of single inheritance :

Example: 

pragma solidity ^0.8.0;// This is the Parent Method //contract SolidityTest {string public mrx_ample="Mr.Examples";uint public solidity_developer_age=21;function get_Name(string memory mrx) public pure returns(string memory){return mrx; }function get_Average(uint mrx1,uint mrx2,uint mrx3,uint mrx4,uint mrx5) public pure returns(uint){uint sum=(mrx1+mrx2+mrx3+mrx4+mrx5)/5;return sum; }function get_Product(uint mrx,uint ample) public pure returns(uint){ return mrx*ample;} }// This is the inherited contract with have access to the variables and functions of the parent contract //contract SolidityTest1 is SolidityTest{function inherited_Name() public pure returns(string memory){return get_Name("Mr.Examples"); }function inherited_Average() public pure returns(uint){return get_Average(1,2,3,4,5); }function inherited_Product() public pure returns(uint){return get_Product(3,5); }function inherit_value_from_Parent() public view returns(string memory){return mrx_ample;} function inherite_developer_age() public view returns(uint){return solidity_developer_age; }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Multiple Inheritance

A type of inheritance in which a child class can have more than one parent is known as Multiple inheritance.

This allows the child class to access the attributes and functions from both parents, making code reusable and short.

For example, consider the following code:

Example: 

pragma solidity ^0.8.0;// This is the Parent Method //contract SolidityTest {uint public sum=10+20; uint public difference=20-10; uint public product=10*20; uint public divide=20/10;}contract SolidityTest1{string public mrx1="Mr"; string public mrx2="."; string public mrx3="Examples"; }contract SolidityTest2 is SolidityTest,SolidityTest1{// Here we obtain the values from parent number 1 that is SolidityTestfunction get_Sum() public view returns(uint){return sum; }function get_Difference() public view returns(uint){return difference; }function get_Product() public view returns(uint){return product; }function get_Divide() public view returns(uint){return divide; }// Here we obtain the values from parent number 2 that is SolidityTest1function get_String1() public view returns(string memory){ return mrx1; }function get_String2() public view returns(string memory){ return mrx2; }function get_String3() public view returns(string memory){ return mrx3; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Hierarchical – Multi-level Inheritance

This is very similar to single inheritance, except that there are levels of relationships between a parent and child.

Child contracts derived from parents act as parents for the contracts that derive from them.

The following code will explain the concept of multilevel inheritance:

Example: 

pragma solidity ^0.8.0;// This is the Parent Method //contract SolidityTest {string public mrx_ample="Mr.Examples";uint public solidity_developer_age=21;function get_Name(string memory mrx) public pure returns(string memory){return mrx; }function get_Average(uint mrx1,uint mrx2,uint mrx3,uint mrx4,uint mrx5) public pure returns(uint){uint sum=(mrx1+mrx2+mrx3+mrx4+mrx5)/5;return sum; }function get_Product(uint mrx,uint ample) public pure returns(uint){ return mrx*ample;} }// This is the inherited contract with have access to the variables and functions of the parent contract //contract SolidityTest1 is SolidityTest{function inherited_Name() public pure returns(string memory){return get_Name("Mr.Examples"); }function inherited_Average() public pure returns(uint){return get_Average(1,2,3,4,5); }function inherited_Product() public pure returns(uint){return get_Product(3,5); }function inherit_value_from_Parent() public view returns(string memory){return mrx_ample;} function inherite_developer_age() public view returns(uint){return solidity_developer_age; }}// Here we initialize another class that extends from the already extended class SolidityTest1contract SolidityTest2 is SolidityTest1{function multilevel_Obtain_Name() public pure returns(string memory){return get_Name("Examples");}function multilevel_Obtain_Average() public pure returns(uint){return get_Average(6,3,5,6,8);}function multilevel_Obtain_Product() public pure returns(uint){return get_Product(4,7);}function multilevel_value_from_Parent() public view returns(string memory){return mrx_ample;} function multilevel_value_developer_age() public view returns(uint){return solidity_developer_age; }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Conclusion

Solidity inheritance allows you to reuse code and build more sophisticated and complex smart contracts.
Due to its support for multiple inheritance and abstract contracts, you can create complex inheritance structures to meet their specific needs.
Inheritance should be used judiciously, however, and inheritance hierarchies should not be overly complex or hard to maintain.
Solidity inheritance enables you and other developers to write smart contracts that are easier to read, test, and maintain due to their modularity, reusability, and clean syntax.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *