Inheritance In Solidity

Solidity provides an inheritance between smart contracts. This article talks about the essentials of inheritance in Solidity and its usage.

Inheritance In Solidity
 
Inheritance is the most important feature of object-oriented programming. It is used to decouple the code, reduce the dependency, and increase the re-usability of existing code. Solidity provides inheritance between smart contracts; you can write multiple contracts and inherit into a single one. The contract from where other contracts inherit the features called base contract; the contract which inherits the features known as a derived contract—in a slightly different language, people often referred it as a parent and child contract, respectively.
 
In Solidity, inheritance scope is limited with public and internal access modifiers, meaning that only public and internal properties of a base contract are available to the derived contract.
 
The Solidity compiler compiles all base contracts code into a single contract and that single contract goes to the blockchain.
 

How to use inheritance in Solidity?

 
Solidity has the “is” keyword to inherit the base contract to the derived contract. 
  1. contract baseContract {  
  2.     //parent contract  
  3. }  
  4. contract derivedContract is baseContract {  
  5.     //child contract  
  6. }  
Example
  1. pragma solidity >=0.4.22 <0.6.0;
  2.   
  3. contract parent{  
  4.     uint internal id;  
  5.     function setValue(uint _value) public{  
  6.         id = _value;  
  7.     }  
  8. }  
  9. contract child is parent{    
  10.     function getValue() public view returns(uint){  
  11.         return id;  
  12.     }  
  13. }  
To check if the parent contract code compiles into a child contract or not, deploy the parent contract first and then child contract. You’ll see the difference; parent contract only contains the method "setValue", whereas, child contract contains both the method of its own and of the parent contract. 
 
 Inheritance in Solidity
 
Solidity supports different types of inheritance
  • Single Inheritance
  • Multi-level Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance

Single Inheritance

 
Single or single-level inheritance helps to use variables and functions of the base contract to the derived contract. 
 
Inheritance In Solidity
 
Let’s change our previous code snippet and try to understand the usage of the single inheritance. 
  1. pragma solidity >=0.4.22 <0.6.0;  
  2.   
  3. contract parent{  
  4.       
  5.     uint internal id;  
  6.       
  7.     function setValue(uint _value) external {  
  8.         id = _value;  
  9.     }  
  10. }  
  11.   
  12. contract child is parent{  
  13.       
  14.     function getValue() external view returns(uint) {  
  15.         return id;  
  16.     }  
  17. }  
  18.   
  19. contract caller {  
  20.     child cc = new child();   
  21.       
  22.     function testInheritance(uint _input) public returns (uint) {  
  23.         cc.setValue(_input);  
  24.         return cc.getValue();  
  25.     }  
  26. }  
Things to notice -
  • Functions setValue and getValue are defined as external.
  • Added third contract "caller" to call functions of the base and parent contract.
  • Created an object of the child contract named "cc".
  • Added method "testInheritance", using by we will set and get the value of the id variable.
Now, let's jump to the Remix IDE, paste our example code, and deploy the "caller" contract. Once it is deployed, you should see the screen as below.
 
Simple Inheritance in Remix IDE
 
Add value—15 in the upper screen—to the method “testInheritance”, hit the method, and check the terminal, you should see the same value that you passed to the method. Here, the value of the variable “id” has been updated, and that we have accessed it from the child contract.
 

Multi-level Inheritance

 
Multi-level inheritance is similar to the single level, but the difference is, it has levels of parent-child relationships. The child contract—derived from a parent contract—also acts as a parent contract of another contract. Take a look at the following diagram: 
 
Inheritance In Solidity
 
Contract A is the parent of B, and contract B is the parent of contract C. 
 
Example
  1. pragma solidity >=0.4.22 <0.6.0;  
  2.   
  3. contract A {  
  4.     function getAValue() external pure returns(string memory){  
  5.         return "contract A is called";  
  6.     }  
  7. }  
  8.   
  9. contract B is A {  
  10.       
  11.     function getBValue() external pure returns(string memory){  
  12.         return "contract B is called";  
  13.     }  
  14. }  
  15.   
  16. contract C is B {  
  17.       
  18.     function getCValue() external pure returns(string memory){  
  19.         return "contract C is called";  
  20.     }  
  21. }  
  22.   
  23.   
  24. contract caller {  
  25.   
  26.     C contractC = new C();   
  27.       
  28.     function testInheritance() public view returns (string memory, string memory, string memory) {  
  29.         return (contractC.getAValue(), contractC.getBValue(), contractC.getCValue());  
  30.     }  
  31. }     
Here in this example, we have called functions of contracts A, B, and C from the “caller”. The execution of the above code will give the following results:
 
Inheritance In Solidity
 

Hierarchical Inheritance

 
In hierarchical inheritance, the base contract has over one derived contract. This inheritance is useful when one common functionality is used in multiple places. The following diagram is displayed the type hierarchical, where a single contract is a parent of multiple child contracts.
 
Inheritance In Solidity
 
Example:
  1. pragma solidity >=0.4.22 <0.6.0;  
  2.   
  3. contract A {  
  4.     function getAValue() external pure returns(string memory){  
  5.         return "contract A is called";  
  6.     }  
  7. contract B is A {  
  8.       
  9. contract C is A {  
  10.       
  11. }     
  12. contract caller {        
  13.     B contractB = new B();  
  14.     C contractC = new C();   
  15.       
  16.     function testInheritance() public view returns (string memory, string memory) {  
  17.         return (contractB.getAValue(), contractC.getAValue());  
  18.     }  
  19. }  
Output
 
 Inheritance In Solidity
 

Multiple Inheritance 

 
Unlike C# and Java, Solidity supports multiple-inheritance. The single contract can be inherited from more than one contract. Here, in the following diagram, shown is the multiple-inheritance. 
 
Inheritance In Solidity
 
Example
  1. pragma solidity >=0.4.22 <0.6.0;  
  2.   
  3. contract A {  
  4.   
  5.     uint internal a;  
  6.   
  7.     function getA(uint _value) external {  
  8.         a = _value;  
  9.     }  
  10. }  
  11.   
  12. contract B {  
  13.   
  14.     uint internal b;  
  15.   
  16.     function getB(uint _value) external{  
  17.         b = _value;  
  18.     }  
  19. }  
  20.   
  21. contract C is A, B {  
  22.   
  23.     function getValueOfSum() external view returns(uint) {  
  24.         return a + b;  
  25.     }  
  26. }  
  27.   
  28. contract caller {  
  29.   
  30.     C contractC = new C();  
  31.   
  32.     function testInheritance() public returns(uint) {  
  33.         contractC.getA(10);  
  34.         contractC.getB(20);  
  35.         return contractC.getValueOfSum();  
  36.     }  
  37. }  
Execution of the above code will give an output as expected "30", you can see that in the terminal window as follows,
 
Inheritance In Solidity
 

Summary

 
In this article, we have seen the following points,
  • Usage of the “is” keyword in Solidity
  • Types of inheritance support in Solidity
  • How to use a single, multi-level, hierarchical and multiple inheritance.
In the next article, we’ll discuss another most important topic—Interface in Solidity. Stay tuned!