What is Inheritance in solidity ?
This article gives you a brief understanding of the concept Inheritance along with some advance features provided by it .
Introduction :
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 . Moreover , you can write multiple contracts and inherit it with single line.
Before diving into the concept , their is a basic terminology which is to be understood . It is the contract from where other contracts inherit the features called base contract and 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.
What is Inheritance in solidity :
In solidity , we can easily get the access to all variables , functions , mappings, struct etc from one smart contract to the other by just using a small keyword called " is " . Lets see it practically .
This is the base or parent contract with some minimal functionality.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract A {
uint public x = 100;
address public owner = msg.sender;
mapping (uint => address) public balances;
function fun1() public pure returns (string memory){
return "I am in contract A" ;
}
function fun2() public pure returns (string memory){
return "I am in contract A" ;
}
function fun3() public pure returns (string memory){
return "I am in contract A";
}
function fun4() public pure returns (string memory){
return "I am in contract A" ;
}
}
Output of the contract A is
Now lets try to get all the functions , variables and mappings present inside the above smart contract to our derived or child contract .
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract B is A { }
That's it, by only using one keyword we now got all the variable and functions in the contract A to B which can be directly accessed .
Output of contract B is
In this way we extract the data present in one contract to other with the help of inheritance . Now lets take it a step further and try to edit the data we get as per our requirement.
How to change the data which is inherited by the derived contract ?
In order to achieve this criteria of editing the data , we just need to remember 2 keywords which make it possible by ease. They are "virtual" and "override" .
Virtual - It is something like giving permission or access to the derived contract from base contract to make some changes for that particular data while inheriting it . Override - It will use the access which was given by the base contract and make changes to the data which is inherited in the derived contract.
Lets see the practical implementation of these keywords.
Now lets try to change the fun3() and fun4() of contract A which was executed prior.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract A {
uint public x = 100;
address public owner = msg.sender;
mapping (uint => address) public balances;
function fun1() public pure returns (string memory){
return "I am in contract A" ;
}
function fun2() public pure returns (string memory){
return "I am in contract A" ;
}
function fun3() public pure virtual returns (string memory){
return "I am in contract A";
}
function fun4() public pure virtual returns (string memory){
return "I am in contract A" ;
}
}
As we can see that the entire contract is same but we have given a keyword virtual to the fun3() and fun4() which we want to change as it gives us the option of changing the data in the derived contract or remain unchanged.
Here the output of this contract A is completely same as before with out any change.
Let us see the code of contract B :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract B is A {
function fun3() public pure override returns(string memory){
return "I am in contract B"
}
function fun4() public pure override returns (string memory){
return "I am in contract B"
}
}
Output of contract B :
Here we can see that the entire functions and variable are inherited same as contract A again but the values returned by fun3() and fun4() are different because we have changed the data in contract B by using the override keyword and made it as per our need and the remaining data remains same due to the lack of permission from contract A for making a change.
Note : If you dont want others to make changes while inheriting your contract then ignore the keyword "virtual".
In this way we make changes to the functions as per our requirement while inheriting from one contract to another .
Conclusion:
In this way we use the property of Inheritance in our smart contracts . It is mainly useful while we are working with the libraries such as OpenZeppelin and also helpful if you are building a contract that is meant to be used by other developers in future .
If you have any questions or suggestions, feel free to reach out to me! 😊
🕊️Twitter : twitter.com/DMohindar
📩 Email : d.mohindar1999@gmail.com
🔗LinkedIn : linkedin.com/in/mohindar-amarnadh-b77a28150